diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 9e5b39bd7a8..746d5653370 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -3105,7 +3105,7 @@ namespace ts { emit(node.name); emit(node.exclamationToken); emitTypeAnnotation(node.type); - emitInitializer(node.initializer, node.type ? node.type.end : node.name.end, node, parenthesizer.parenthesizeExpressionForDisallowedComma); + emitInitializer(node.initializer, node.type?.end ?? node.name.emitNode?.typeNode?.end ?? node.name.end, node, parenthesizer.parenthesizeExpressionForDisallowedComma); } function emitVariableDeclarationList(node: VariableDeclarationList) { @@ -5331,6 +5331,10 @@ namespace ts { commentsDisabled = false; } emitTrailingCommentsOfNode(node, emitFlags, commentRange.pos, commentRange.end, savedContainerPos, savedContainerEnd, savedDeclarationListContainerEnd); + const typeNode = getTypeNode(node); + if (typeNode) { + emitTrailingCommentsOfNode(node, emitFlags, typeNode.pos, typeNode.end, savedContainerPos, savedContainerEnd, savedDeclarationListContainerEnd); + } } function emitLeadingCommentsOfNode(node: Node, emitFlags: EmitFlags, pos: number, end: number) { diff --git a/src/compiler/factory/emitNode.ts b/src/compiler/factory/emitNode.ts index 6c207994331..19ebfd53ba2 100644 --- a/src/compiler/factory/emitNode.ts +++ b/src/compiler/factory/emitNode.ts @@ -279,4 +279,16 @@ namespace ts { getOrCreateEmitNode(node).flags |= EmitFlags.IgnoreSourceNewlines; return node; } -} \ No newline at end of file + + /* @internal */ + export function setTypeNode(node: T, type: TypeNode): T { + const emitNode = getOrCreateEmitNode(node); + emitNode.typeNode = type; + return node; + } + + /* @internal */ + export function getTypeNode(node: T): TypeNode | undefined { + return node.emitNode?.typeNode; + } +} diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index f8cec499a8f..5be57ec2976 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -2227,12 +2227,16 @@ namespace ts { } function visitVariableDeclaration(node: VariableDeclaration) { - return factory.updateVariableDeclaration( + const updated = factory.updateVariableDeclaration( node, visitNode(node.name, visitor, isBindingName), /*exclamationToken*/ undefined, /*type*/ undefined, visitNode(node.initializer, visitor, isExpression)); + if (node.type) { + setTypeNode(updated.name, node.type); + } + return updated; } function visitParenthesizedExpression(node: ParenthesizedExpression): Expression { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 54936e787bc..bff06f7bf54 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -6846,6 +6846,7 @@ namespace ts { helpers?: EmitHelper[]; // Emit helpers for the node startsOnNewLine?: boolean; // If the node should begin on a new line snippetElement?: SnippetElement; // Snippet element of the node + typeNode?: TypeNode; // VariableDeclaration type } /* @internal */ diff --git a/tests/baselines/reference/emitOneLineVariableDeclarationRemoveCommentsFalse.js b/tests/baselines/reference/emitOneLineVariableDeclarationRemoveCommentsFalse.js new file mode 100644 index 00000000000..46cbed2dfda --- /dev/null +++ b/tests/baselines/reference/emitOneLineVariableDeclarationRemoveCommentsFalse.js @@ -0,0 +1,14 @@ +//// [emitOneLineVariableDeclarationRemoveCommentsFalse.ts] +let a = /*[[${something}]]*/ {}; +let b: any = /*[[${something}]]*/ {}; +let c: { hoge: boolean } = /*[[${something}]]*/ { hoge: true }; +let d: any /*[[${something}]]*/ = {}; +let e/*[[${something}]]*/: any = {}; + + +//// [emitOneLineVariableDeclarationRemoveCommentsFalse.js] +var a = /*[[${something}]]*/ {}; +var b = /*[[${something}]]*/ {}; +var c = /*[[${something}]]*/ { hoge: true }; +var d /*[[${something}]]*/ = {}; +var e /*[[${something}]]*/ = {}; diff --git a/tests/baselines/reference/emitOneLineVariableDeclarationRemoveCommentsFalse.symbols b/tests/baselines/reference/emitOneLineVariableDeclarationRemoveCommentsFalse.symbols new file mode 100644 index 00000000000..b7b8f9346fb --- /dev/null +++ b/tests/baselines/reference/emitOneLineVariableDeclarationRemoveCommentsFalse.symbols @@ -0,0 +1,18 @@ +=== tests/cases/compiler/emitOneLineVariableDeclarationRemoveCommentsFalse.ts === +let a = /*[[${something}]]*/ {}; +>a : Symbol(a, Decl(emitOneLineVariableDeclarationRemoveCommentsFalse.ts, 0, 3)) + +let b: any = /*[[${something}]]*/ {}; +>b : Symbol(b, Decl(emitOneLineVariableDeclarationRemoveCommentsFalse.ts, 1, 3)) + +let c: { hoge: boolean } = /*[[${something}]]*/ { hoge: true }; +>c : Symbol(c, Decl(emitOneLineVariableDeclarationRemoveCommentsFalse.ts, 2, 3)) +>hoge : Symbol(hoge, Decl(emitOneLineVariableDeclarationRemoveCommentsFalse.ts, 2, 8)) +>hoge : Symbol(hoge, Decl(emitOneLineVariableDeclarationRemoveCommentsFalse.ts, 2, 49)) + +let d: any /*[[${something}]]*/ = {}; +>d : Symbol(d, Decl(emitOneLineVariableDeclarationRemoveCommentsFalse.ts, 3, 3)) + +let e/*[[${something}]]*/: any = {}; +>e : Symbol(e, Decl(emitOneLineVariableDeclarationRemoveCommentsFalse.ts, 4, 3)) + diff --git a/tests/baselines/reference/emitOneLineVariableDeclarationRemoveCommentsFalse.types b/tests/baselines/reference/emitOneLineVariableDeclarationRemoveCommentsFalse.types new file mode 100644 index 00000000000..539366d9626 --- /dev/null +++ b/tests/baselines/reference/emitOneLineVariableDeclarationRemoveCommentsFalse.types @@ -0,0 +1,24 @@ +=== tests/cases/compiler/emitOneLineVariableDeclarationRemoveCommentsFalse.ts === +let a = /*[[${something}]]*/ {}; +>a : {} +>{} : {} + +let b: any = /*[[${something}]]*/ {}; +>b : any +>{} : {} + +let c: { hoge: boolean } = /*[[${something}]]*/ { hoge: true }; +>c : { hoge: boolean; } +>hoge : boolean +>{ hoge: true } : { hoge: true; } +>hoge : true +>true : true + +let d: any /*[[${something}]]*/ = {}; +>d : any +>{} : {} + +let e/*[[${something}]]*/: any = {}; +>e : any +>{} : {} + diff --git a/tests/cases/compiler/emitOneLineVariableDeclarationRemoveCommentsFalse.ts b/tests/cases/compiler/emitOneLineVariableDeclarationRemoveCommentsFalse.ts new file mode 100644 index 00000000000..5305e4f5498 --- /dev/null +++ b/tests/cases/compiler/emitOneLineVariableDeclarationRemoveCommentsFalse.ts @@ -0,0 +1,7 @@ +// @removeComments: false + +let a = /*[[${something}]]*/ {}; +let b: any = /*[[${something}]]*/ {}; +let c: { hoge: boolean } = /*[[${something}]]*/ { hoge: true }; +let d: any /*[[${something}]]*/ = {}; +let e/*[[${something}]]*/: any = {};