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

Co-authored-by: Jake Bailey <5341706+jakebailey@users.noreply.github.com>
This commit is contained in:
islandryu 2022-02-24 02:33:41 +09:00 committed by GitHub
parent 78818e0390
commit ff3b458714
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 87 additions and 3 deletions

View File

@ -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) {

View File

@ -279,4 +279,16 @@ namespace ts {
getOrCreateEmitNode(node).flags |= EmitFlags.IgnoreSourceNewlines;
return node;
}
}
/* @internal */
export function setTypeNode<T extends Node>(node: T, type: TypeNode): T {
const emitNode = getOrCreateEmitNode(node);
emitNode.typeNode = type;
return node;
}
/* @internal */
export function getTypeNode<T extends Node>(node: T): TypeNode | undefined {
return node.emitNode?.typeNode;
}
}

View File

@ -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 {

View File

@ -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 */

View File

@ -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}]]*/ = {};

View File

@ -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))

View File

@ -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
>{} : {}

View File

@ -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 = {};