fix(formatter): break JSX && chain when final operand has leading own-line comment#21993
Closed
jsmecham wants to merge 3 commits into
Closed
fix(formatter): break JSX && chain when final operand has leading own-line comment#21993jsmecham wants to merge 3 commits into
&& chain when final operand has leading own-line comment#21993jsmecham wants to merge 3 commits into
Conversation
2cb7f18 to
36f1dea
Compare
…wn-line comment Fixes oxc-project#21991 When a JSX `&&` chain's final operand is a parenthesized JSX element with a leading own-line comment (e.g. an inline `// ...` after `(`), the chain group's flat `fits` check treats the comment as zero-width (it's emitted as a `line_suffix`), so the chain stays on a single line — even when the comment pushes the line well past `printWidth`. Prettier breaks before each `&&` in this case. Detect the leading own-line comment on the JSX operand and mark the `#logicalChain` group with `should_expand(true)` so the chain breaks.
36f1dea to
7e8d2a9
Compare
Merging this PR will not alter performance
Comparing Footnotes
|
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.
Summary
Fixes #21991
When a JSX
&&chain's final operand is a parenthesized JSX element with a leading own-line comment after((e.g. an inline// ...line comment, or a block comment separated from the JSX by a newline), oxfmt collapses the entire&&chain onto a single line — even when the resulting line exceedsprintWidth. Prettier breaks before each&&so the line fits.Input
Before (incorrect — 163-char line at
printWidth: 120)After (matches Prettier)
Root cause
In the binary-expression printer (
binary_like_expression.rs), when the chain ends with a JSX operand, the chain is built as:The
#logicalChaingroup decides whether to break by running its own flatfitscheck on the chain operands. Leading comments on the JSX are emitted asline_suffix(comment)followed byexpand_parent, but:line_suffixcontent is zero-width during flat-modefitsmeasurement, so the comment doesn't push the chain pastprintWidth.expand_parentonly propagates to enclosing groups — and the#logicalChaingroup is a sibling of the comment (both are children of the outer wrapper), so itsexpand_parentnever reaches the chain.Result: the chain stays flat, the comment is queued for the end of the (now very long) line, and the printed line blows through
printWidth.Fix
Detect, when building the chain, whether the final JSX operand has a leading own-line comment via
Comments::has_leading_own_line_comment. If so, mark the#logicalChaingroup withshould_expand(true)so the chain breaks before each&&. This catches both inline//comments after((which always end the line) and block comments separated from the JSX by a newline.A small helper
BinaryLeftOrRightSide::operand_span_startreturns the span start of the side's operand for the comment lookup.Prettier conformance: no regressions (746/753 JS, 591/601 TS — unchanged).
AI Disclosure
This PR was co-authored with Claude Code (AI assistant), as noted in the commit. The fix was reviewed, tested against the full Prettier conformance suite, and verified to produce no regressions.