Skip to content

Commit ff3b458

Browse files
fix(47158): Removes comments when line variable declaration (microsoft#47407)
Co-authored-by: Jake Bailey <[email protected]>
1 parent 78818e0 commit ff3b458

8 files changed

+87
-3
lines changed

Diff for: src/compiler/emitter.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -3105,7 +3105,7 @@ namespace ts {
31053105
emit(node.name);
31063106
emit(node.exclamationToken);
31073107
emitTypeAnnotation(node.type);
3108-
emitInitializer(node.initializer, node.type ? node.type.end : node.name.end, node, parenthesizer.parenthesizeExpressionForDisallowedComma);
3108+
emitInitializer(node.initializer, node.type?.end ?? node.name.emitNode?.typeNode?.end ?? node.name.end, node, parenthesizer.parenthesizeExpressionForDisallowedComma);
31093109
}
31103110

31113111
function emitVariableDeclarationList(node: VariableDeclarationList) {
@@ -5331,6 +5331,10 @@ namespace ts {
53315331
commentsDisabled = false;
53325332
}
53335333
emitTrailingCommentsOfNode(node, emitFlags, commentRange.pos, commentRange.end, savedContainerPos, savedContainerEnd, savedDeclarationListContainerEnd);
5334+
const typeNode = getTypeNode(node);
5335+
if (typeNode) {
5336+
emitTrailingCommentsOfNode(node, emitFlags, typeNode.pos, typeNode.end, savedContainerPos, savedContainerEnd, savedDeclarationListContainerEnd);
5337+
}
53345338
}
53355339

53365340
function emitLeadingCommentsOfNode(node: Node, emitFlags: EmitFlags, pos: number, end: number) {

Diff for: src/compiler/factory/emitNode.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,16 @@ namespace ts {
279279
getOrCreateEmitNode(node).flags |= EmitFlags.IgnoreSourceNewlines;
280280
return node;
281281
}
282-
}
282+
283+
/* @internal */
284+
export function setTypeNode<T extends Node>(node: T, type: TypeNode): T {
285+
const emitNode = getOrCreateEmitNode(node);
286+
emitNode.typeNode = type;
287+
return node;
288+
}
289+
290+
/* @internal */
291+
export function getTypeNode<T extends Node>(node: T): TypeNode | undefined {
292+
return node.emitNode?.typeNode;
293+
}
294+
}

Diff for: src/compiler/transformers/ts.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -2227,12 +2227,16 @@ namespace ts {
22272227
}
22282228

22292229
function visitVariableDeclaration(node: VariableDeclaration) {
2230-
return factory.updateVariableDeclaration(
2230+
const updated = factory.updateVariableDeclaration(
22312231
node,
22322232
visitNode(node.name, visitor, isBindingName),
22332233
/*exclamationToken*/ undefined,
22342234
/*type*/ undefined,
22352235
visitNode(node.initializer, visitor, isExpression));
2236+
if (node.type) {
2237+
setTypeNode(updated.name, node.type);
2238+
}
2239+
return updated;
22362240
}
22372241

22382242
function visitParenthesizedExpression(node: ParenthesizedExpression): Expression {

Diff for: src/compiler/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6846,6 +6846,7 @@ namespace ts {
68466846
helpers?: EmitHelper[]; // Emit helpers for the node
68476847
startsOnNewLine?: boolean; // If the node should begin on a new line
68486848
snippetElement?: SnippetElement; // Snippet element of the node
6849+
typeNode?: TypeNode; // VariableDeclaration type
68496850
}
68506851

68516852
/* @internal */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [emitOneLineVariableDeclarationRemoveCommentsFalse.ts]
2+
let a = /*[[${something}]]*/ {};
3+
let b: any = /*[[${something}]]*/ {};
4+
let c: { hoge: boolean } = /*[[${something}]]*/ { hoge: true };
5+
let d: any /*[[${something}]]*/ = {};
6+
let e/*[[${something}]]*/: any = {};
7+
8+
9+
//// [emitOneLineVariableDeclarationRemoveCommentsFalse.js]
10+
var a = /*[[${something}]]*/ {};
11+
var b = /*[[${something}]]*/ {};
12+
var c = /*[[${something}]]*/ { hoge: true };
13+
var d /*[[${something}]]*/ = {};
14+
var e /*[[${something}]]*/ = {};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/compiler/emitOneLineVariableDeclarationRemoveCommentsFalse.ts ===
2+
let a = /*[[${something}]]*/ {};
3+
>a : Symbol(a, Decl(emitOneLineVariableDeclarationRemoveCommentsFalse.ts, 0, 3))
4+
5+
let b: any = /*[[${something}]]*/ {};
6+
>b : Symbol(b, Decl(emitOneLineVariableDeclarationRemoveCommentsFalse.ts, 1, 3))
7+
8+
let c: { hoge: boolean } = /*[[${something}]]*/ { hoge: true };
9+
>c : Symbol(c, Decl(emitOneLineVariableDeclarationRemoveCommentsFalse.ts, 2, 3))
10+
>hoge : Symbol(hoge, Decl(emitOneLineVariableDeclarationRemoveCommentsFalse.ts, 2, 8))
11+
>hoge : Symbol(hoge, Decl(emitOneLineVariableDeclarationRemoveCommentsFalse.ts, 2, 49))
12+
13+
let d: any /*[[${something}]]*/ = {};
14+
>d : Symbol(d, Decl(emitOneLineVariableDeclarationRemoveCommentsFalse.ts, 3, 3))
15+
16+
let e/*[[${something}]]*/: any = {};
17+
>e : Symbol(e, Decl(emitOneLineVariableDeclarationRemoveCommentsFalse.ts, 4, 3))
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/compiler/emitOneLineVariableDeclarationRemoveCommentsFalse.ts ===
2+
let a = /*[[${something}]]*/ {};
3+
>a : {}
4+
>{} : {}
5+
6+
let b: any = /*[[${something}]]*/ {};
7+
>b : any
8+
>{} : {}
9+
10+
let c: { hoge: boolean } = /*[[${something}]]*/ { hoge: true };
11+
>c : { hoge: boolean; }
12+
>hoge : boolean
13+
>{ hoge: true } : { hoge: true; }
14+
>hoge : true
15+
>true : true
16+
17+
let d: any /*[[${something}]]*/ = {};
18+
>d : any
19+
>{} : {}
20+
21+
let e/*[[${something}]]*/: any = {};
22+
>e : any
23+
>{} : {}
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @removeComments: false
2+
3+
let a = /*[[${something}]]*/ {};
4+
let b: any = /*[[${something}]]*/ {};
5+
let c: { hoge: boolean } = /*[[${something}]]*/ { hoge: true };
6+
let d: any /*[[${something}]]*/ = {};
7+
let e/*[[${something}]]*/: any = {};

0 commit comments

Comments
 (0)