Skip to content

fix(formatter): Honor trailing ignore comments after list separators#19925

Open
alubbe wants to merge 1 commit intooxc-project:mainfrom
alubbe:trailing-comment-array-of-objects
Open

fix(formatter): Honor trailing ignore comments after list separators#19925
alubbe wants to merge 1 commit intooxc-project:mainfrom
alubbe:trailing-comment-array-of-objects

Conversation

@alubbe
Copy link
Contributor

@alubbe alubbe commented Mar 2, 2026

Summary

After running the new oxfmt release (which comes with #19304) against our code base, we found one case where trailing ignore comments aren't properly interpreted (playground link at the end of this PR).

const items = [
  {a:aa(),b:bb(),c:cc(),d:dd(),e:ee(),f:ff(),g:gg()}, // prettier-ignore
];

Prettier keeps the code as-is, but oxfmt turns it into:

const items = [
  { a: aa(), b: bb(), c: cc(), d: dd(), e: ee(), f: ff(), g: gg() }, // prettier-ignore
];

Root cause

  • Trailing suppression detection only allowed whitespace/=/: between node end and comment.
  • In list items, there is typically a comma before the trailing comment (}, // prettier-ignore).
  • Expression formatting path also needed trailing suppression handling.

Changes

  • Extended trailing suppression gap handling to include list separators (',', ';').
  • Reused a shared internal comment scan helper to avoid duplicated logic between:
    • end-of-line comment detection
    • trailing suppression detection
  • Added trailing suppression handling for AstNode<Expression>.
  • Updated the formatter generator accordingly so regenerated code keeps this behavior.

Validation

Ran:

  • cargo test -p oxc_formatter --test mod fixtures::js::ignore
  • cargo test -p oxc_formatter --test mod fixtures::ts::ignore

Both pass.

AI disclosure

This PR was prepared with AI assistance (Codex), and all changes were reviewed and validated by the author.

https://playground.oxc.rs/?t=formatter&options=%7B%22run%22%3A%7B%22lint%22%3Atrue%2C%22formatter%22%3Atrue%2C%22transform%22%3Afalse%2C%22isolatedDeclarations%22%3Afalse%2C%22whitespace%22%3Afalse%2C%22mangle%22%3Afalse%2C%22compress%22%3Afalse%2C%22scope%22%3Atrue%2C%22symbol%22%3Atrue%2C%22cfg%22%3Afalse%7D%2C%22parser%22%3A%7B%22extension%22%3A%22tsx%22%2C%22allowReturnOutsideFunction%22%3Atrue%2C%22preserveParens%22%3Atrue%2C%22allowV8Intrinsics%22%3Atrue%2C%22semanticErrors%22%3Atrue%7D%2C%22linter%22%3A%7B%7D%2C%22formatter%22%3A%7B%22useTabs%22%3Afalse%2C%22tabWidth%22%3A2%2C%22endOfLine%22%3A%22lf%22%2C%22printWidth%22%3A80%2C%22singleQuote%22%3Afalse%2C%22jsxSingleQuote%22%3Afalse%2C%22quoteProps%22%3A%22as-needed%22%2C%22trailingComma%22%3A%22all%22%2C%22semi%22%3Atrue%2C%22arrowParens%22%3A%22always%22%2C%22bracketSpacing%22%3Atrue%2C%22bracketSameLine%22%3Afalse%2C%22objectWrap%22%3A%22preserve%22%2C%22singleAttributePerLine%22%3Afalse%7D%2C%22transformer%22%3A%7B%22target%22%3A%22es2015%22%2C%22useDefineForClassFields%22%3Atrue%2C%22experimentalDecorators%22%3Atrue%2C%22emitDecoratorMetadata%22%3Atrue%7D%2C%22isolatedDeclarations%22%3A%7B%22stripInternal%22%3Afalse%7D%2C%22codegen%22%3A%7B%22normal%22%3Atrue%2C%22jsdoc%22%3Atrue%2C%22annotation%22%3Atrue%2C%22legal%22%3Atrue%7D%2C%22compress%22%3A%7B%7D%2C%22mangle%22%3A%7B%22topLevel%22%3Atrue%2C%22keepNames%22%3Afalse%7D%2C%22controlFlow%22%3A%7B%22verbose%22%3Afalse%7D%2C%22inject%22%3A%7B%22inject%22%3A%7B%7D%7D%2C%22define%22%3A%7B%22define%22%3A%7B%7D%7D%7D&code=const+items+%3D+%5B%0A++%7Ba%3Aaa%28%29%2Cb%3Abb%28%29%2Cc%3Acc%28%29%2Cd%3Add%28%29%2Ce%3Aee%28%29%2Cf%3Aff%28%29%2Cg%3Agg%28%29%7D%2C+%2F%2F+prettier-ignore%0A%5D%3B%0A

@github-actions github-actions bot added A-ast-tools Area - AST tools A-formatter Area - Formatter C-bug Category - Bug labels Mar 2, 2026
@codspeed-hq
Copy link

codspeed-hq bot commented Mar 2, 2026

Merging this PR will not alter performance

✅ 49 untouched benchmarks
⏩ 7 skipped benchmarks1


Comparing alubbe:trailing-comment-array-of-objects (641051e) with main (a607119)

Open in CodSpeed

Footnotes

  1. 7 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-ast-tools Area - AST tools A-formatter Area - Formatter C-bug Category - Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant