You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Keep formatting fragments out of source-file statement lists - #65491
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
The shortened formatting range excludes emitted trailing comments, causing semicolons to remain despite SemicolonPreferenceRemove; the full printed-text range must be preserved and covered by a regression test.
This PR keeps printed formatting fragments separate from source-file statement lists, preserving valid AST structure in the compiler and language server.
Changes:
Create synthetic source files as text and line-map contexts, with empty statement lists.
Update formatting callers and assign parent pointers to all positioned children.
Add formatting tests for non-statement fragments.
File
Description
tsc/internal/printer/syntheticfile.go
Remove fragments from synthetic source-file statements.
tsc/internal/printer/changetrackerwriter.go
Set parent pointers for every child.
tsc/internal/ls/completions.go
Update snippet formatting to use the revised helper.
tsc/internal/ls/change/trackerimpl.go
Format positioned fragments separately from source-file context.
tsc/internal/format/api_test.go
Test fragment formatting and valid source-file children.
tsc/internal/api/session.go
Update insertion formatting to use the revised helper.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
With an empty statement list, indentation lookups repeatedly scan from the fragment’s start, potentially causing quadratic formatting time for large functions or classes with many multiline branches.
No open issues 🎉
1 previously missed issue
In code that hasn't changed since last review
Restrict preceding-token scans to the enclosing if node
tsc/internal/printer/syntheticfile.go:43
With an empty statement list, the FindPrecedingToken call in format/indent.go:502 scans from the start of the fragment for each multiline if/else whose indentation needs this lookup. Repeated branches in a large function or class can therefore cause quadratic token scanning during formatting. Keep the text-only source file, but change that lookup to astnav.FindPrecedingTokenEx(sourceFile, child.Pos(), parent, false) so it searches within the enclosing if node.
Formatting accepts roots that are not statements, including class
members, case clauses, types, and import specifiers. Putting these roots
in a source file violates its statement-child contract.
Use the source file for text and line-map context while traversing the
positioned fragment separately. Preserve the full printed-text range
for trailing comments, and bound indentation token lookups to their
enclosing nodes so large fragments do not incur repeated prefix scans.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The old way is technically violating the type annotations, though they are not currently checked.