-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Ensure that emitter calls callbacks #18284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
9815359
7d931bb
7d56deb
5b44de1
aae877d
8f775dc
bca3b09
15e2663
b33dbec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -406,6 +406,14 @@ namespace ts { | |
| setWriter(/*output*/ undefined); | ||
| } | ||
|
|
||
| // TODO: Should this just be `emit`? | ||
| // See https://github.com/Microsoft/TypeScript/pull/18284#discussion_r137611034 | ||
| function emitIfPresent(node: Node | undefined) { | ||
| if (node) { | ||
| pipelineEmitWithNotification(EmitHint.Unspecified, node); | ||
| } | ||
| } | ||
|
|
||
| function emit(node: Node) { | ||
| pipelineEmitWithNotification(EmitHint.Unspecified, node); | ||
| } | ||
|
|
@@ -451,6 +459,7 @@ namespace ts { | |
| case EmitHint.SourceFile: return pipelineEmitSourceFile(node); | ||
| case EmitHint.IdentifierName: return pipelineEmitIdentifierName(node); | ||
| case EmitHint.Expression: return pipelineEmitExpression(node); | ||
| case EmitHint.MappedTypeParameter: return pipelineEmitMappedTypeParameter(cast(node, isTypeParameterDeclaration)); | ||
| case EmitHint.Unspecified: return pipelineEmitUnspecified(node); | ||
| } | ||
| } | ||
|
|
@@ -465,6 +474,12 @@ namespace ts { | |
| emitIdentifier(<Identifier>node); | ||
| } | ||
|
|
||
| function pipelineEmitMappedTypeParameter(node: TypeParameterDeclaration): void { | ||
| emit(node.name); | ||
| write(" in "); | ||
| emit(node.constraint); | ||
| } | ||
|
|
||
| function pipelineEmitUnspecified(node: Node): void { | ||
| const kind = node.kind; | ||
|
|
||
|
|
@@ -898,9 +913,9 @@ namespace ts { | |
| function emitParameter(node: ParameterDeclaration) { | ||
| emitDecorators(node, node.decorators); | ||
| emitModifiers(node, node.modifiers); | ||
| writeIfPresent(node.dotDotDotToken, "..."); | ||
| emitIfPresent(node.dotDotDotToken); | ||
| emit(node.name); | ||
| writeIfPresent(node.questionToken, "?"); | ||
| emitIfPresent(node.questionToken); | ||
| emitWithPrefix(": ", node.type); | ||
| emitExpressionWithPrefix(" = ", node.initializer); | ||
| } | ||
|
|
@@ -918,7 +933,7 @@ namespace ts { | |
| emitDecorators(node, node.decorators); | ||
| emitModifiers(node, node.modifiers); | ||
| emit(node.name); | ||
| writeIfPresent(node.questionToken, "?"); | ||
| emitIfPresent(node.questionToken); | ||
| emitWithPrefix(": ", node.type); | ||
| write(";"); | ||
| } | ||
|
|
@@ -927,7 +942,7 @@ namespace ts { | |
| emitDecorators(node, node.decorators); | ||
| emitModifiers(node, node.modifiers); | ||
| emit(node.name); | ||
| writeIfPresent(node.questionToken, "?"); | ||
| emitIfPresent(node.questionToken); | ||
| emitWithPrefix(": ", node.type); | ||
| emitExpressionWithPrefix(" = ", node.initializer); | ||
| write(";"); | ||
|
|
@@ -937,7 +952,7 @@ namespace ts { | |
| emitDecorators(node, node.decorators); | ||
| emitModifiers(node, node.modifiers); | ||
| emit(node.name); | ||
| writeIfPresent(node.questionToken, "?"); | ||
| emitIfPresent(node.questionToken); | ||
| emitTypeParameters(node, node.typeParameters); | ||
| emitParameters(node, node.parameters); | ||
| emitWithPrefix(": ", node.type); | ||
|
|
@@ -947,9 +962,9 @@ namespace ts { | |
| function emitMethodDeclaration(node: MethodDeclaration) { | ||
| emitDecorators(node, node.decorators); | ||
| emitModifiers(node, node.modifiers); | ||
| writeIfPresent(node.asteriskToken, "*"); | ||
| emitIfPresent(node.asteriskToken); | ||
| emit(node.name); | ||
| writeIfPresent(node.questionToken, "?"); | ||
| emitIfPresent(node.questionToken); | ||
| emitSignatureAndBody(node, emitSignatureHead); | ||
| } | ||
|
|
||
|
|
@@ -1035,10 +1050,8 @@ namespace ts { | |
|
|
||
| function emitTypeLiteral(node: TypeLiteralNode) { | ||
| write("{"); | ||
| // If the literal is empty, do not add spaces between braces. | ||
| if (node.members.length > 0) { | ||
| emitList(node, node.members, getEmitFlags(node) & EmitFlags.SingleLine ? ListFormat.SingleLineTypeLiteralMembers : ListFormat.MultiLineTypeLiteralMembers); | ||
| } | ||
| const flags = getEmitFlags(node) & EmitFlags.SingleLine ? ListFormat.SingleLineTypeLiteralMembers : ListFormat.MultiLineTypeLiteralMembers; | ||
| emitList(node, node.members, flags | ListFormat.NoSpaceIfEmpty); | ||
| write("}"); | ||
| } | ||
|
|
||
|
|
@@ -1094,13 +1107,16 @@ namespace ts { | |
| writeLine(); | ||
| increaseIndent(); | ||
| } | ||
| writeIfPresent(node.readonlyToken, "readonly "); | ||
| if (node.readonlyToken) { | ||
| emit(node.readonlyToken); | ||
| write(" "); | ||
| } | ||
|
|
||
| write("["); | ||
| emit(node.typeParameter.name); | ||
| write(" in "); | ||
| emit(node.typeParameter.constraint); | ||
| pipelineEmitWithNotification(EmitHint.MappedTypeParameter, node.typeParameter); | ||
| write("]"); | ||
| writeIfPresent(node.questionToken, "?"); | ||
|
|
||
| emitIfPresent(node.questionToken); | ||
| write(": "); | ||
| emit(node.type); | ||
| write(";"); | ||
|
|
@@ -1148,7 +1164,7 @@ namespace ts { | |
|
|
||
| function emitBindingElement(node: BindingElement) { | ||
| emitWithSuffix(node.propertyName, ": "); | ||
| writeIfPresent(node.dotDotDotToken, "..."); | ||
| emitIfPresent(node.dotDotDotToken); | ||
| emit(node.name); | ||
| emitExpressionWithPrefix(" = ", node.initializer); | ||
| } | ||
|
|
@@ -1159,33 +1175,22 @@ namespace ts { | |
|
|
||
| function emitArrayLiteralExpression(node: ArrayLiteralExpression) { | ||
| const elements = node.elements; | ||
| if (elements.length === 0) { | ||
| write("[]"); | ||
| } | ||
| else { | ||
| const preferNewLine = node.multiLine ? ListFormat.PreferNewLine : ListFormat.None; | ||
| emitExpressionList(node, elements, ListFormat.ArrayLiteralExpressionElements | preferNewLine); | ||
| } | ||
| const preferNewLine = node.multiLine ? ListFormat.PreferNewLine : ListFormat.None; | ||
| emitExpressionList(node, elements, ListFormat.ArrayLiteralExpressionElements | preferNewLine); | ||
| } | ||
|
|
||
| function emitObjectLiteralExpression(node: ObjectLiteralExpression) { | ||
| const properties = node.properties; | ||
| if (properties.length === 0) { | ||
| write("{}"); | ||
| const indentedFlag = getEmitFlags(node) & EmitFlags.Indented; | ||
| if (indentedFlag) { | ||
| increaseIndent(); | ||
| } | ||
| else { | ||
| const indentedFlag = getEmitFlags(node) & EmitFlags.Indented; | ||
| if (indentedFlag) { | ||
| increaseIndent(); | ||
| } | ||
|
|
||
| const preferNewLine = node.multiLine ? ListFormat.PreferNewLine : ListFormat.None; | ||
| const allowTrailingComma = currentSourceFile.languageVersion >= ScriptTarget.ES5 ? ListFormat.AllowTrailingComma : ListFormat.None; | ||
| emitList(node, properties, ListFormat.ObjectLiteralExpressionProperties | allowTrailingComma | preferNewLine); | ||
| const preferNewLine = node.multiLine ? ListFormat.PreferNewLine : ListFormat.None; | ||
| const allowTrailingComma = currentSourceFile.languageVersion >= ScriptTarget.ES5 ? ListFormat.AllowTrailingComma : ListFormat.None; | ||
| emitList(node, node.properties, ListFormat.ObjectLiteralExpressionProperties | allowTrailingComma | preferNewLine); | ||
|
|
||
| if (indentedFlag) { | ||
| decreaseIndent(); | ||
| } | ||
| if (indentedFlag) { | ||
| decreaseIndent(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1286,7 +1291,8 @@ namespace ts { | |
| emitTypeParameters(node, node.typeParameters); | ||
| emitParametersForArrow(node, node.parameters); | ||
| emitWithPrefix(": ", node.type); | ||
| write(" =>"); | ||
| write(" "); | ||
| emit(node.equalsGreaterThanToken); | ||
| } | ||
|
|
||
| function emitDeleteExpression(node: DeleteExpression) { | ||
|
|
@@ -1364,13 +1370,13 @@ namespace ts { | |
|
|
||
| emitExpression(node.condition); | ||
| increaseIndentIf(indentBeforeQuestion, " "); | ||
| write("?"); | ||
| emit(node.questionToken); | ||
| increaseIndentIf(indentAfterQuestion, " "); | ||
| emitExpression(node.whenTrue); | ||
| decreaseIndentIf(indentBeforeQuestion, indentAfterQuestion); | ||
|
|
||
| increaseIndentIf(indentBeforeColon, " "); | ||
| write(":"); | ||
| emit(node.colonToken); | ||
| increaseIndentIf(indentAfterColon, " "); | ||
| emitExpression(node.whenFalse); | ||
| decreaseIndentIf(indentBeforeColon, indentAfterColon); | ||
|
|
@@ -1382,7 +1388,8 @@ namespace ts { | |
| } | ||
|
|
||
| function emitYieldExpression(node: YieldExpression) { | ||
| write(node.asteriskToken ? "yield*" : "yield"); | ||
| write("yield"); | ||
| emit(node.asteriskToken); | ||
| emitExpressionWithPrefix(" ", node.expression); | ||
| } | ||
|
|
||
|
|
@@ -1662,7 +1669,9 @@ namespace ts { | |
| function emitFunctionDeclarationOrExpression(node: FunctionDeclaration | FunctionExpression) { | ||
| emitDecorators(node, node.decorators); | ||
| emitModifiers(node, node.modifiers); | ||
| write(node.asteriskToken ? "function* " : "function "); | ||
| write("function"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Edit: I was incorrect. Apparently emit does not have this guard.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get a test failure in the test |
||
| emitIfPresent(node.asteriskToken); | ||
| write(" "); | ||
| emitIdentifierName(node.name); | ||
| emitSignatureAndBody(node, emitSignatureHead); | ||
| } | ||
|
|
@@ -2068,9 +2077,7 @@ namespace ts { | |
| function emitJsxExpression(node: JsxExpression) { | ||
| if (node.expression) { | ||
| write("{"); | ||
| if (node.dotDotDotToken) { | ||
| write("..."); | ||
| } | ||
| emitIfPresent(node.dotDotDotToken); | ||
| emitExpression(node.expression); | ||
| write("}"); | ||
| } | ||
|
|
@@ -2128,13 +2135,12 @@ namespace ts { | |
| emitTrailingCommentsOfPosition(statements.pos); | ||
| } | ||
|
|
||
| let format = ListFormat.CaseOrDefaultClauseStatements; | ||
| if (emitAsSingleStatement) { | ||
| write(" "); | ||
| emit(statements[0]); | ||
| } | ||
| else { | ||
| emitList(parentNode, statements, ListFormat.CaseOrDefaultClauseStatements); | ||
| format &= ~(ListFormat.MultiLine | ListFormat.Indented); | ||
| } | ||
| emitList(parentNode, statements, format); | ||
| } | ||
|
|
||
| function emitHeritageClause(node: HeritageClause) { | ||
|
|
@@ -2384,7 +2390,7 @@ namespace ts { | |
|
|
||
| function emitParametersForArrow(parentNode: FunctionTypeNode | ArrowFunction, parameters: NodeArray<ParameterDeclaration>) { | ||
| if (canEmitSimpleArrowHead(parentNode, parameters)) { | ||
| emit(parameters[0]); | ||
| emitSingleElementList(parameters); | ||
| } | ||
| else { | ||
| emitParameters(parentNode, parameters); | ||
|
|
@@ -2395,6 +2401,17 @@ namespace ts { | |
| emitList(parentNode, parameters, ListFormat.IndexSignatureParameters); | ||
| } | ||
|
|
||
| function emitSingleElementList(list: NodeArray<Node>) { | ||
|
||
| Debug.assert(list.length === 1); | ||
| if (onBeforeEmitNodeArray) { | ||
| onBeforeEmitNodeArray(list); | ||
| } | ||
| emit(list[0]); | ||
| if (onAfterEmitNodeArray) { | ||
| onAfterEmitNodeArray(list); | ||
| } | ||
| } | ||
|
|
||
| function emitList(parentNode: Node, children: NodeArray<Node>, format: ListFormat, start?: number, count?: number) { | ||
| emitNodeList(emit, parentNode, children, format, start, count); | ||
| } | ||
|
|
@@ -2427,7 +2444,7 @@ namespace ts { | |
| if (format & ListFormat.MultiLine) { | ||
| writeLine(); | ||
| } | ||
| else if (format & ListFormat.SpaceBetweenBraces) { | ||
| else if (format & ListFormat.SpaceBetweenBraces && !(format & ListFormat.NoSpaceIfEmpty)) { | ||
| write(" "); | ||
| } | ||
| } | ||
|
|
@@ -2568,12 +2585,6 @@ namespace ts { | |
| } | ||
| } | ||
|
|
||
| function writeIfPresent(node: Node, text: string) { | ||
| if (node) { | ||
| write(text); | ||
| } | ||
| } | ||
|
|
||
| function writeToken(token: SyntaxKind, pos: number, contextNode?: Node) { | ||
| return onEmitSourceMapOfToken | ||
| ? onEmitSourceMapOfToken(contextNode, token, pos, writeTokenText) | ||
|
|
@@ -2584,7 +2595,7 @@ namespace ts { | |
| if (onBeforeEmitToken) { | ||
| onBeforeEmitToken(node); | ||
| } | ||
| writeTokenText(node.kind); | ||
| write(tokenToString(node.kind)); | ||
| if (onAfterEmitToken) { | ||
| onAfterEmitToken(node); | ||
| } | ||
|
|
@@ -3107,6 +3118,9 @@ namespace ts { | |
| NoTrailingNewLine = 1 << 16, // Do not emit a trailing NewLine for a MultiLine list. | ||
| NoInterveningComments = 1 << 17, // Do not emit comments between each node | ||
|
|
||
| NoSpaceIfEmpty = 1 << 18, // If the literal is empty, do not add spaces between braces. | ||
| SingleElement = 1 << 19, | ||
|
|
||
| // Precomputed Formats | ||
| Modifiers = SingleLine | SpaceBetweenSiblings | NoInterveningComments, | ||
| HeritageClauses = SingleLine | SpaceBetweenSiblings, | ||
|
|
@@ -3118,7 +3132,7 @@ namespace ts { | |
| IntersectionTypeConstituents = AmpersandDelimited | SpaceBetweenSiblings | SingleLine, | ||
| ObjectBindingPatternElements = SingleLine | AllowTrailingComma | SpaceBetweenBraces | CommaDelimited | SpaceBetweenSiblings, | ||
| ArrayBindingPatternElements = SingleLine | AllowTrailingComma | CommaDelimited | SpaceBetweenSiblings, | ||
| ObjectLiteralExpressionProperties = PreserveLines | CommaDelimited | SpaceBetweenSiblings | SpaceBetweenBraces | Indented | Braces, | ||
| ObjectLiteralExpressionProperties = PreserveLines | CommaDelimited | SpaceBetweenSiblings | SpaceBetweenBraces | Indented | Braces | NoSpaceIfEmpty, | ||
| ArrayLiteralExpressionElements = PreserveLines | CommaDelimited | SpaceBetweenSiblings | AllowTrailingComma | Indented | SquareBrackets, | ||
| CommaListElements = CommaDelimited | SpaceBetweenSiblings | SingleLine, | ||
| CallExpressionArguments = CommaDelimited | SpaceBetweenSiblings | SingleLine | Parenthesis, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just call this emitMappedTypeParameter.