Skip to content

fix(formatter): check every signed number for a comment before fill-printing an array - #28104

Closed
vidigoat wants to merge 1 commit into
oxc-project:mainfrom
vidigoat:fix/formatter-concise-array-unary-comment
Closed

vidigoat wants to merge 1 commit into
oxc-project:mainfrom
vidigoat:fix/formatter-concise-array-unary-comment

Conversation

@vidigoat

Copy link
Copy Markdown
Contributor

js/arrays/numbers-negative-comment-after-minus.js is in the "Not idempotent" list of the Prettier conformance report. Pass 1 prints one element per line, and pass 2 fill-prints the array, because a comment inside a signed number is missed unless that number is the first one checked.

can_concisely_print_array_list shares one comments iterator across the elements. has_comment_inside_unary takes comments from it until the first one that starts after the current unary, then returns whether that comment is inside it. When it is not, the comment is already consumed, so the later element that does contain it sees the next comment instead:

// input
y = [-1, -2, -(/* comment */ 3), -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16, -17];

// main (`/* comment */` is consumed while checking `-1`)
y = [
  -1, -2, -(/* comment */ 3), -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14,
  -15, -16, -17,
];

// this PR, same as Prettier (one element per line)

In the conformance file, pass 1 is saved by the separate "line comment on the same line" check (-// comment1). Pass 1 moves that comment onto its own line inside -(...), so on pass 2 only has_comment_inside_unary could catch it.

The iterator is now Peekable: comments that start before the current unary are skipped, and the next one is only peeked at.

Tests:

  • New fixture js/arrays/concise-unary-comment.js; it fails on main, and its output is identical to Prettier 3.9.10 (the pinned version) at both option sets and stable on a second pass.
  • numbers-negative-comment-after-minus.js drops out of the "Not idempotent" list in conformance__prettier-js.snap; no other conformance change.
  • cargo test -p oxc_formatter --all-features, cargo fmt --check, just lint -p oxc_formatter (clippy with warnings denied) and typos pass.

AI disclosure: this PR (code, tests and description) was written with Claude Code (Claude Opus 5.5); the verification above is what was run.

…rinting an array

`has_comment_inside_unary` consumed the first comment after the element it was
checking, so a comment inside a later signed number (`-(/* c */ 3)`) was already
gone when that element was checked, and the array was fill-printed. Peek at the
comment instead of consuming it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vidigoat
vidigoat requested a review from leaysgur as a code owner October 22, 2026 09:55
@github-actions github-actions Bot added the A-formatter Area - Formatter (oxfmt). Add F-* labels for details label Oct 22, 2026
@codspeed

codspeed Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 77 untouched benchmarks
⏩ 19 skipped benchmarks1


Comparing vidigoat:fix/formatter-concise-array-unary-comment (e61dd40) with main (f1d9032)

Open in CodSpeed

Footnotes

  1. 19 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@leaysgur

Copy link
Copy Markdown
Member

Thanks, this will also be fixed by #28169.

@leaysgur leaysgur closed this Oct 26, 2026
graphite-app Bot pushed a commit that referenced this pull request Oct 26, 2026
…28169)

Closes #28104.

`can_concisely_print_array_list` shared one cursor-based comments iterator across elements.
Checking an element consumed the comments of later ones, so a comment inside a later signed number was missed and the array fill-printed on the second pass.

- Check each element with the position-based `has_any_comment_in_range`, and the trailing line comment check with `all_comments_in_range` over the array
  - Layout decisions use the position-based queries, see `crates/oxc_formatter/AGENTS.md`
- `js/arrays/numbers-negative-comment-after-minus.js` is now idempotent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-formatter Area - Formatter (oxfmt). Add F-* labels for details

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants