Skip to content

Simplify mergeEmitNode #59613

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

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
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
21 changes: 4 additions & 17 deletions src/compiler/factory/nodeFactory.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {
__String,
AccessorDeclaration,
addRange,
append,
appendIfUnique,
ArrayBindingElement,
ArrayBindingPattern,
ArrayLiteralExpression,
Expand Down Expand Up @@ -422,7 +420,6 @@ import {
TemplateMiddle,
TemplateSpan,
TemplateTail,
TextRange,
ThisExpression,
ThisTypeNode,
ThrowStatement,
Expand Down Expand Up @@ -7449,13 +7446,13 @@ function mergeEmitNode(sourceEmitNode: EmitNode, destEmitNode: EmitNode | undefi
// `leadingComments` are concatenated with any existing leading comments on the destination
if (leadingComments) {
// We use `.slice()` in case `destEmitNode.leadingComments` is pushed to later
destEmitNode.leadingComments = addRange(leadingComments.slice(), destEmitNode.leadingComments);
destEmitNode.leadingComments = leadingComments.slice();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I worry this results in a different inconsistency now. Before, we would attempt to merge the emit information for the old node into anything set on the new node prior to calling setOriginalNode. Now we will conditionally overwrite some properties but not others.

const oldNode = ...;
addSyntheticLeadingComment(oldNode, SyntaxKind.SingleLineCommentTrivia, "foo");

const newNode = ...;
addSyntheticLeadingComment(oldNode, SyntaxKind.SingleLineCommentTrivia, "bar");
addSyntheticTrailingComment(oldNode, SyntaxKind.SingleLineCommentTrivia, "baz");

setOriginalNode(newNode, oldNode);

// before
getSyntheticLeadingComments(newNode).length; // 2 (foo and bar)
getSyntheticTrailingComments(newNode).length; // 1 (baz)

// after
getSyntheticLeadingComments(newNode).length; // 1 (foo)
getSyntheticTrailingComments(newNode).length; // 1 (baz)

After this change we end up with oldNode's leading comments but newNodes trailing comments, which seems more confusing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I certainly am not confident in this PR, but we do need something for #59332.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally considered using appendIfUnique to avoid duplicating but also to concatenate. Would you think that's a good fix?

}

// `trailingComments` are concatenated with any existing trailing comments on the destination
if (trailingComments) {
// We use `.slice()` in case `destEmitNode.trailingComments` is pushed to later
destEmitNode.trailingComments = addRange(trailingComments.slice(), destEmitNode.trailingComments);
destEmitNode.trailingComments = trailingComments.slice();
}

// `commentRange` overwrites the destination
Expand All @@ -7470,7 +7467,7 @@ function mergeEmitNode(sourceEmitNode: EmitNode, destEmitNode: EmitNode | undefi

// `tokenSourceMapRanges` are merged with the destination
if (tokenSourceMapRanges) {
destEmitNode.tokenSourceMapRanges = mergeTokenSourceMapRanges(tokenSourceMapRanges, destEmitNode.tokenSourceMapRanges!);
destEmitNode.tokenSourceMapRanges = tokenSourceMapRanges.slice();
}

// `constantValue` overwrites the destination
Expand All @@ -7483,9 +7480,7 @@ function mergeEmitNode(sourceEmitNode: EmitNode, destEmitNode: EmitNode | undefi

// `helpers` are merged into the destination
if (helpers) {
for (const helper of helpers) {
destEmitNode.helpers = appendIfUnique(destEmitNode.helpers, helper);
}
destEmitNode.helpers = helpers.slice();
}

// `startsOnNewLine` overwrites the destination
Expand Down Expand Up @@ -7517,11 +7512,3 @@ function mergeEmitNode(sourceEmitNode: EmitNode, destEmitNode: EmitNode | undefi

return destEmitNode;
}

function mergeTokenSourceMapRanges(sourceRanges: (TextRange | undefined)[], destRanges: (TextRange | undefined)[]) {
if (!destRanges) destRanges = [];
for (const key in sourceRanges) {
destRanges[key] = sourceRanges[key];
}
return destRanges;
}
Comment on lines -7521 to -7527
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read this function and it sort of scared me; what if the test has more elements than the source? Then I replaced it and nothing changed, so I kept going...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sourceRanges and destRanges are holey arrays, so length doesn't actually matter. It probably should just be a Map<number, TextRange>, to be honest.