Repository navigation
Fix formatter crash and invalid output for comments in imports and empty literals - #12465
Open
Aaron Thomas (arxhr007) wants to merge 2 commits into
Open
Aaron Thomas (arxhr007) wants to merge 2 commits into
Aaron Thomas (arxhr007) wants to merge 2 commits into
Conversation
…pty literals A comment between 'import' and the path, or inside an empty array literal, was attached to a node whose printer never printed it, so the formatter threw. A line comment inside an empty object literal was printed on the same line as the closing token, which swallowed it.
|
Azure Pipelines: Successfully started running 1 pipeline(s). 1 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Timothee Guerin (timotheeguerin)
approved these changes
Nov 2, 2026
commit: |
Contributor
|
All changed packages have been documented.
Show changes
|
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 formatter throws
TypeError: Cannot read properties of undefined (reading 'trim')when a comment sits betweenimportand its path, or inside an empty array literal:For an empty object literal it doesn't crash, but the closing tokens end up on the comment line, so the output no longer parses:
is formatted as
@foo(#{ // note }).Cause: the import statement was printed from
node.path.value, so a comment attached to the path was never printed, andprintArrayLiteraldid not print dangling comments. Prettier then reports a comment that "was not printed", and building that message crashes because TypeSpec comments have novalue. InprintObjectLiteralthe dangling comment was printed inside a group that is not forced to break.Changes:
#[]and#{}on their own indented line, with the closing token on the next line (a small shared helper)Tests: four new formatter tests, one per case above; they fail without the change and pass with it. The formatter tests (
test/formatter) pass. I did not run the rest of the repo's test suite.I found these by formatting mutated
.tspsamples and checking that the output parses and formats the same twice. Two related cases remain and are not part of this PR: a block comment between a decorator and a parameter name (op n(@y/* */y:t):e;) still throws the same error, and one decorator line-break case is not idempotent.