-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Simplify mergeEmitNode #59613
Conversation
@typescript-bot perf test this faster |
function mergeTokenSourceMapRanges(sourceRanges: (TextRange | undefined)[], destRanges: (TextRange | undefined)[]) { | ||
if (!destRanges) destRanges = []; | ||
for (const key in sourceRanges) { | ||
destRanges[key] = sourceRanges[key]; | ||
} | ||
return destRanges; | ||
} |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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.
@jakebailey Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
A lot of the complexity in |
function mergeTokenSourceMapRanges(sourceRanges: (TextRange | undefined)[], destRanges: (TextRange | undefined)[]) { | ||
if (!destRanges) destRanges = []; | ||
for (const key in sourceRanges) { | ||
destRanges[key] = sourceRanges[key]; | ||
} | ||
return destRanges; | ||
} |
There was a problem hiding this comment.
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.
@@ -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(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
The issue that drove this PR is not so much an issue with |
I found the actual underlying problem in #59332 in |
@armanio123 and I noticed in #59332 that simply copying
leadingComments
/trailingComments
and force overwriting (rather than concatenating) fixes the comment duplication problem from #59332.Reading things further, it the function already force copies over the other node's information (flags, comment ranges, etc). Out of interest, I tried removing some of the other "complicated" merging operations and found that they also worked fine when just regular slices.
Maybe this extra simplification is valid?