Skip to content
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

fix(47158): Removes comments when line variable declaration #47407

Merged
merged 9 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3102,7 +3102,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 ? node.type.end : node.name.emitNode?.typeNode ? node.name.emitNode.typeNode.end : node.name.end, node, parenthesizer.parenthesizeExpressionForDisallowedComma);
islandryu marked this conversation as resolved.
Show resolved Hide resolved
}

function emitVariableDeclarationList(node: VariableDeclarationList) {
Expand Down Expand Up @@ -5325,6 +5325,10 @@ namespace ts {
commentsDisabled = false;
}
emitTrailingCommentsOfNode(node, emitFlags, commentRange.pos, commentRange.end, savedContainerPos, savedContainerEnd, savedDeclarationListContainerEnd);
const typeNode = getTypeNode(node);
if(typeNode){
islandryu marked this conversation as resolved.
Show resolved Hide resolved
emitTrailingCommentsOfNode(node, emitFlags, typeNode.pos, typeNode.end, savedContainerPos, savedContainerEnd, savedDeclarationListContainerEnd);
}
}

function emitLeadingCommentsOfNode(node: Node, emitFlags: EmitFlags, pos: number, end: number) {
Expand Down
12 changes: 11 additions & 1 deletion src/compiler/factory/emitNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,14 @@ namespace ts {
getOrCreateEmitNode(node).flags |= EmitFlags.IgnoreSourceNewlines;
return node;
}
}

export function setTypeNode<T extends Node>(node: T, type: TypeNode): T {
const emitNode = getOrCreateEmitNode(node);
emitNode.typeNode = type;
return node;
}

export function getTypeNode<T extends Node>(node: T): TypeNode | undefined {
return node.emitNode?.typeNode;
}
islandryu marked this conversation as resolved.
Show resolved Hide resolved
}
6 changes: 5 additions & 1 deletion src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2209,12 +2209,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){
islandryu marked this conversation as resolved.
Show resolved Hide resolved
setTypeNode(updated.name, node.type);
}
return updated;
}

function visitParenthesizedExpression(node: ParenthesizedExpression): Expression {
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6835,6 +6835,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 */
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4525,6 +4525,8 @@ declare namespace ts {
* Moves matching emit helpers from a source node to a target node.
*/
function moveEmitHelpers(source: Node, target: Node, predicate: (helper: EmitHelper) => boolean): void;
function setTypeNode<T extends Node>(node: T, type: TypeNode): T;
function getTypeNode<T extends Node>(node: T): TypeNode | undefined;
}
declare namespace ts {
function isNumericLiteral(node: Node): node is NumericLiteral;
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4525,6 +4525,8 @@ declare namespace ts {
* Moves matching emit helpers from a source node to a target node.
*/
function moveEmitHelpers(source: Node, target: Node, predicate: (helper: EmitHelper) => boolean): void;
function setTypeNode<T extends Node>(node: T, type: TypeNode): T;
function getTypeNode<T extends Node>(node: T): TypeNode | undefined;
}
declare namespace ts {
function isNumericLiteral(node: Node): node is NumericLiteral;
Expand Down
Original file line number Diff line number Diff line change
@@ -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}]]*/ = {};
Original file line number Diff line number Diff line change
@@ -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))

Original file line number Diff line number Diff line change
@@ -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
>{} : {}

Original file line number Diff line number Diff line change
@@ -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 = {};