fix: stabilize with statement formatting with comments#788
Open
todor-a wants to merge 1 commit into
Open
Conversation
`WithStmt` had no IR generator and fell into the raw-text fallback,
which dumps node source verbatim without consuming child comments. The
comment tracker then re-emitted the inner line comment after the node,
duplicating it on every pass:
with (a) { //
}
-> with (a) { //
} //
-> with (a) { //
} //
//
...
Causing "Formatting not stable. Bailed after 5 tries."
Add a proper `gen_with_stmt` that mirrors `gen_while_stmt` (identical
shape: keyword + parenthesized expression + body statement) and reuses
the existing `while_statement_*` configuration.
with statement formatting with comments (#787)with statement formatting with comments
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.
Fixes #787.
Transparency note: This PR was prepared with AI assistance (Claude).
Problem
emits
Error formatting repro.ts. Message: Formatting not stable. Bailed after 5 tries.Root cause
Node::WithStmthad no IR generator. It fell into thegen_node_innerfallback at the bottom of the dispatch (Class | Function | Invalid | WithStmt | TsModuleBlock), which callsgen_from_raw_string(node.text_fast(...))— i.e. emits the original source bytes verbatim without walking children.That bypasses the comment tracker. The inner
//is never marked consumed, so on the next pass it's re-emitted as a trailing comment after the closing brace. Each format pass appends another//:Stability check bails after 5 attempts.
Fix
Add a real
gen_with_stmtmirroringgen_while_stmt.with (obj) bodyhas the same shape aswhile (test) body(keyword + parenthesized expression + body statement), so the new function reuses the existingwhile_statement_*configuration rather than introducing new config keys (withis deprecated and forbidden in strict mode, so dedicated config seems unwarranted).Removed
WithStmtfrom the "should never be matched" fallback list.Tests
Added tests for this specific case, plus extra tests from https://github.com/prettier/prettier/tree/main/tests/format/json/with-comment.