Skip to content

fix(linter): preserve comments in arrow-body-style auto-fix#22853

Closed
aartisonigra wants to merge 4 commits into
oxc-project:mainfrom
aartisonigra:fix/arrow-body-style-comments
Closed

fix(linter): preserve comments in arrow-body-style auto-fix#22853
aartisonigra wants to merge 4 commits into
oxc-project:mainfrom
aartisonigra:fix/arrow-body-style-comments

Conversation

@aartisonigra

Copy link
Copy Markdown

Description

This PR fixes an issue where the auto-fix for eslint/arrow-body-style removes comments located inside the block body before the return statement.

Now, the fixer checks if there are any comments between the opening brace and the return statement using ctx.has_comments_between. If comments are detected, it safely aborts the auto-fix by returning fixer.noop(), thereby preserving the comments and preventing code loss.

Closes #22833

Key Changes

  • Updated fix_block_to_concise in crates/oxc_linter/src/rules/eslint/arrow_body_style.rs to detect inner comments.
  • Added test cases in the fail suite covering both line comments (//) and block comments (/* ... */) to ensure they are not dropped during auto-fix.

Checklist

  • I have run cargo test to ensure all tests pass.
  • Added new test cases for this specific bug.

@aartisonigra aartisonigra requested a review from camc314 as a code owner May 30, 2026 12:05
Comment on lines +349 to +357
// Check if there are comments between the opening brace and the return statement.
// If so, we cannot safely fix because the comment would be lost.
// ESLint also skips the fix in this case.
let block_start = body.span.start + 1; // Skip the opening '{'
let return_start = return_arg.span().start;
if ctx.has_comments_between(Span::new(block_start, return_start)) {
// Cannot fix when there are comments before the return statement
return fixer.noop();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Signed-off-by: Aarti Sonigra <23amtics292@gmail.com>
Signed-off-by: Aarti Sonigra <23amtics292@gmail.com>

@Sysix Sysix left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on this issue and welcome to the project 🫶
Like @kapobajza said, we should not skip the fix, instead trying to preserve the comments.
We should also make sure, that comments after the return are also preserved.

The fix behavior is difficult to get right, I got with #22854 a good try 🤞 Thank you for the work

@aartisonigra

Copy link
Copy Markdown
Author

Thanks for the warm welcome! I'll look into #22854 and update the PR accordingly.

@graphite-app graphite-app Bot closed this in 39d92d6 May 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

linter: auto-fix for eslint/arrow-body-style removes comments

3 participants