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-23 09:33:41 -08:00
committed by GitHub
co-authored by Jake Bailey
parent 78818e0390
commit ff3b458714
8 changed files with 87 additions and 3 deletions
+5 -1
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) {
+13 -1
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;
}
}
+5 -1
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 {
+1
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 */