Uh oh!
There was an error while loading. Please reload this page.
Simplify mergeEmitNode - #59613
Conversation
Jake Bailey (jakebailey)
commented
Aug 12, 2024
TypeScript Bot (@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.
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.
sourceRanges and destRanges are holey arrays, so length doesn't actually matter. It probably should just be a Map<number, TextRange>, to be honest.
TypeScript Bot (typescript-bot)
commented
Aug 13, 2024
Jake Bailey (@jakebailey) Here they are:tscComparison Report - baseline..pr
System info unknown Hosts
Scenarios
Developer Information: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Ron Buckton (rbuckton)
commented
Aug 13, 2024
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.
sourceRanges and destRanges are holey arrays, so length doesn't actually matter. It probably should just be a Map<number, TextRange>, to be honest.
| 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.
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.
constoldNode= ...;addSyntheticLeadingComment(oldNode,SyntaxKind.SingleLineCommentTrivia,"foo");constnewNode= ...;addSyntheticLeadingComment(oldNode,SyntaxKind.SingleLineCommentTrivia,"bar");addSyntheticTrailingComment(oldNode,SyntaxKind.SingleLineCommentTrivia,"baz");setOriginalNode(newNode,oldNode);// beforegetSyntheticLeadingComments(newNode).length;// 2 (foo and bar)getSyntheticTrailingComments(newNode).length;// 1 (baz)// aftergetSyntheticLeadingComments(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.
I certainly am not confident in this PR, but we do need something for #59332.
There was a problem hiding this comment.
I originally considered using appendIfUnique to avoid duplicating but also to concatenate. Would you think that's a good fix?
Ron Buckton (rbuckton)
commented
Aug 15, 2024
The issue that drove this PR is not so much an issue with |
Ron Buckton (rbuckton)
commented
Aug 15, 2024
I found the actual underlying problem in #59332 in |
Armando Aguirre (@armanio123) and I noticed in #59332 that simply copying
leadingComments/trailingCommentsand 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?