Repository navigation
Preserve text ranges on updated and cloned API nodes - #65649
Andrew Branch (andrewbranch) wants to merge 10 commits into
Conversation
Generated update functions and updateSourceFile now copy the original node's pos/end onto the updated node, so getSynthesizedDeepClone keeps ranges too. This fixes shebangs being dropped when printing an updated SourceFile. getSynthesizedDeepClone(node, false, emitContext) suppresses leading/trailing comments via emit flags while keeping ranges. Also visit JSDoc parameter/property tags in source order according to isNameFirst. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…ents getSynthesizedDeepClone now always preserves text ranges. Comment suppression is opted into separately via the emit context. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
Critical printer regressions can emit invalid mixed nullish/logical expressions after AST updates and discard interior comments.
2 open issues
What changed in this PR
This PR preserves source text ranges on updated and deeply cloned TypeScript API nodes, allowing the Go printer to retain shebangs and source comments when printing modified syntax trees.
Changes:
- Preserve original ranges in generated
update*helpers and deep clones. - Replace the clone helpers’
includeTriviaparameter with explicit outer-comment suppression throughEmitContext. - Add range and printer regression tests, and correct JSDoc child visitation order.
| File | Description |
|---|---|
tools/scripts/tsc/generate-ts-ast.ts |
Generate range-preserving update helpers. |
packages/typescript/test/sync/ast.test.ts |
Test preserved ranges and JSDoc visitation order. |
packages/typescript/test/sync/api.test.ts |
Add synchronous printer regression tests. |
packages/typescript/test/async/api.test.ts |
Add asynchronous printer regression tests. |
packages/typescript/src/ast/visitor.ts |
Visit JSDoc names and types in source order. |
packages/typescript/src/ast/factory.generated.ts |
Apply range preservation to generated factories. |
packages/typescript/src/ast/clone.ts |
Preserve clone ranges and remove includeTrivia. |
packages/typescript/src/api/printer.ts |
Add explicit outer-comment suppression. |
Review effort: Balanced
Stop descending once a child no longer shares its parent's start/end, so comments inside delimiters are kept. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Client factory nodes are now marked NodeFlags.Synthesized, and the printer treats operands with that flag like range-less ones when deciding whether mixed ?? and ||/&& need parentheses. Updated nodes keep their source ranges, so the range alone no longer identifies factory-created operands. Parsed code prints unchanged. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
Critical comment-preservation regressions remain: outer-comment suppression removes interior function-body comments, and synthesized clones lose token-level source comments.
3 open issues
1 resolved since last review
Review effort: Balanced
Update and clone functions now record the node they were created from. printNode sends the server identity of each node's parsed original so the printer can find comments next to tokens in deep clones. Comment suppression now only walks through nodes without source positions. Emit flags on a positioned node already hide descendants' comments at that position, and walking further hid comments before a function body's closing brace. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
Comment suppression crosses synthesized block boundaries and incorrectly removes comments inside emitted braces; delimiter-aware propagation and a regression test are needed.
1 open issue
3 resolved since last review
Review effort: Balanced
…xt-ranges # Conflicts: # packages/typescript/test/async/api.test.ts # packages/typescript/test/sync/api.test.ts
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
API printing mishandles UTF-16 ranges and replacement-list trailing commas, causing non-ASCII trailing comments to be lost and final array holes to be dropped.
2 open issues
1 resolved since last review
Review effort: Balanced
Positions from the client are UTF-16 offsets, but the printer reads comments from the source text by UTF-8 byte offset, so comments after non-ASCII text were missed. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🔵 Needs a closer look
Human assessment is needed because findings omitted from the published review identify a critical TSX printing regression and original-node associations that cannot be cleared.
No open issues 🎉
2 resolved since last review
Review effort: Balanced


In Strada, the
update*factory helpers andgetSynthesizedDeepClonecopy the original node'sposandendonto the new node. In TS 7, they instead delegated directly tocreate*, returning synthesized nodes withpos: -1, end: -1.Because the Go printer uses node text ranges to look up trivia in the original source text, dropping ranges on updated and cloned nodes had several observable effects when printing modified ASTs:
Shebangs were dropped after
updateSourceFile. The printer skips shebang emission on synthesizedSourceFileroots (pos < 0), so updating any statement in a file starting with#!/usr/bin/env nodesilently lost the shebang:Updated statements lost their surrounding comments. Updating a child inside a statement gave the enclosing statement
pos: -1, end: -1, causing leading and trailing source comments on that statement to disappear on print:Suppressing outer comments on clones
In Strada,
getSynthesizedDeepClone(node, /*includeTrivia*/ false)setsEmitFlags.NoLeadingComments/EmitFlags.NoTrailingCommentsdown the clone's first- and last-child chains (while keeping text ranges intact, so inner comments like the one infoo(/* arg */ 1)survive).In TS 7, emit flags live in an
EmitContextrather than on nodes, so the clone function can't set them by itself. Instead of threading anEmitContextthroughgetSynthesizedDeepClone, this PR splits the two jobs:getSynthesizedDeepCloneandgetSynthesizedDeepClonesalways keep text ranges, and theirincludeTriviaparameter is removed.emitContext.suppressLeadingAndTrailingComments(node)handles the comment suppression. It works on any node, not just clones.