Skip to content

Conversation

@hornta
Copy link
Contributor

@hornta hornta commented Nov 20, 2025

Summary

Fixes #8179.

The useConsistentArrowReturn rule had a bug in its autofix when the style option was set to "always". When converting arrow functions with multiline expressions to block statements, the autofix would incorrectly place a newline after the return keyword. This triggered JavaScript's Automatic Semicolon Insertion (ASI), causing the function to return undefined instead of the intended value.

For example, this code:

const foo = (l) =>
  l
    .split('\n')

Was incorrectly fixed to:

const foo = (l) => {
  return
  l.split('\n');
}

The fix ensures that multiline expressions are kept on the same line as the return keyword:

const foo = (l) => {
  return l.split('\n');
}

Test Plan

Added comprehensive test cases in multiline.invalid.js covering various multiline expression scenarios including:

  • Method chaining
  • Object literals
  • Array operations
  • Nested expressions

All tests verify that the autofix produces correct code without triggering ASI.

Docs

This is a bugfix for an existing rule, so no documentation changes are needed beyond the changeset.

@changeset-bot
Copy link

changeset-bot bot commented Nov 20, 2025

🦋 Changeset detected

Latest commit: bd77c58

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 13 packages
Name Type
@biomejs/biome Patch
@biomejs/cli-win32-x64 Patch
@biomejs/cli-win32-arm64 Patch
@biomejs/cli-darwin-x64 Patch
@biomejs/cli-darwin-arm64 Patch
@biomejs/cli-linux-x64 Patch
@biomejs/cli-linux-arm64 Patch
@biomejs/cli-linux-x64-musl Patch
@biomejs/cli-linux-arm64-musl Patch
@biomejs/wasm-web Patch
@biomejs/wasm-bundler Patch
@biomejs/wasm-nodejs Patch
@biomejs/backend-jsonrpc Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added A-Linter Area: linter L-JavaScript Language: JavaScript and super languages labels Nov 20, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 20, 2025

Walkthrough

Trims leading trivia from the expression when converting a concise arrow body into a block with return in the useConsistentArrowReturn nursery lint autofix. Adds tests for multiline arrow return expressions and a linter options file enabling the rule with style: "always". Includes a changeset noting the fix for previously produced semantically incorrect code caused by a newline after return (ASI).

Possibly related PRs

Suggested reviewers

  • arendjr
  • siketyan

Pre-merge checks and finishing touches

✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarises the main fix: correcting multiline autofix behaviour in the useConsistentArrowReturn rule.
Description check ✅ Passed The description clearly explains the bug, the problematic behaviour, the fix applied, and test coverage plans—all related to the changeset.
Linked Issues check ✅ Passed The PR successfully addresses issue #8179 by fixing the autofix to avoid placing newlines after return, preventing ASI and ensuring semantically correct code output.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the useConsistentArrowReturn autofix: implementation fix, test cases, and changelog entry—no out-of-scope additions detected.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e5547c3 and bd77c58.

⛔ Files ignored due to path filters (1)
  • crates/biome_js_analyze/tests/specs/nursery/useConsistentArrowReturn/multiline.invalid.js.snap is excluded by !**/*.snap and included by **
📒 Files selected for processing (2)
  • .changeset/wild-ghosts-design.md (1 hunks)
  • crates/biome_js_analyze/tests/specs/nursery/useConsistentArrowReturn/multiline.invalid.js (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • crates/biome_js_analyze/tests/specs/nursery/useConsistentArrowReturn/multiline.invalid.js
  • .changeset/wild-ghosts-design.md

Tip

📝 Customizable high-level summaries are now available in beta!

You can now customize how CodeRabbit generates the high-level summary in your pull requests — including its content, structure, tone, and formatting.

  • Provide your own instructions using the high_level_summary_instructions setting.
  • Format the summary however you like (bullet lists, tables, multi-section layouts, contributor stats, etc.).
  • Use high_level_summary_in_walkthrough to move the summary from the description to the walkthrough section.

Example instruction:

"Divide the high-level summary into five sections:

  1. 📝 Description — Summarize the main change in 50–60 words, explaining what was done.
  2. 📓 References — List relevant issues, discussions, documentation, or related PRs.
  3. 📦 Dependencies & Requirements — Mention any new/updated dependencies, environment variable changes, or configuration updates.
  4. 📊 Contributor Summary — Include a Markdown table showing contributions:
    | Contributor | Lines Added | Lines Removed | Files Changed |
  5. ✔️ Additional Notes — Add any extra reviewer context.
    Keep each section concise (under 200 words) and use bullet or numbered lists for clarity."

Note: This feature is currently in beta for Pro-tier users, and pricing will be announced later.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
crates/biome_js_analyze/tests/specs/nursery/useConsistentArrowReturn/multiline.invalid.options.json (1)

1-15: Configuration looks good; minor formatting inconsistency.

The rule configuration correctly enables useConsistentArrowReturn with style: always for testing.

Minor nit: line 3 uses a tab whilst the remaining lines use spaces.

crates/biome_js_analyze/tests/specs/nursery/useConsistentArrowReturn/multiline.invalid.js (1)

1-27: Good test coverage for multiline patterns.

The test cases effectively cover common multiline arrow function scenarios: method chaining, object literals, arrays, and nested calls. These will properly exercise the trim_leading_trivia() fix.

If you're feeling thorough, you could also add cases for multiline template literals or ternary expressions, but the current set is solid.

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e9f068e and 48bb3a1.

⛔ Files ignored due to path filters (1)
  • crates/biome_js_analyze/tests/specs/nursery/useConsistentArrowReturn/multiline.invalid.js.snap is excluded by !**/*.snap and included by **
📒 Files selected for processing (3)
  • crates/biome_js_analyze/src/lint/nursery/use_consistent_arrow_return.rs (1 hunks)
  • crates/biome_js_analyze/tests/specs/nursery/useConsistentArrowReturn/multiline.invalid.js (1 hunks)
  • crates/biome_js_analyze/tests/specs/nursery/useConsistentArrowReturn/multiline.invalid.options.json (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📚 Learning: 2025-08-05T14:43:29.581Z
Learnt from: dyc3
Repo: biomejs/biome PR: 7081
File: packages/@biomejs/biome/configuration_schema.json:7765-7781
Timestamp: 2025-08-05T14:43:29.581Z
Learning: The file `packages/biomejs/biome/configuration_schema.json` is auto-generated and should not be manually edited or reviewed for schema issues; any changes should be made at the code generation source.

Applied to files:

  • crates/biome_js_analyze/tests/specs/nursery/useConsistentArrowReturn/multiline.invalid.options.json
📚 Learning: 2025-09-25T12:32:59.003Z
Learnt from: arendjr
Repo: biomejs/biome PR: 7593
File: crates/biome_service/src/workspace/server.rs:1306-1306
Timestamp: 2025-09-25T12:32:59.003Z
Learning: In the biomejs/biome project, do not flag compilation errors during code review as they are handled by the existing test infrastructure and CI. Focus on other code quality aspects instead.

Applied to files:

  • crates/biome_js_analyze/tests/specs/nursery/useConsistentArrowReturn/multiline.invalid.options.json
🔇 Additional comments (1)
crates/biome_js_analyze/src/lint/nursery/use_consistent_arrow_return.rs (1)

187-187: Properly handles multiline expressions.

Trimming leading trivia before constructing the return statement correctly addresses the multiline case. This prevents malformed formatting when converting concise arrows with leading whitespace/newlines to block-body returns.

@hornta hornta marked this pull request as draft November 20, 2025 17:31
@hornta hornta changed the title feat(lint): handle multiline arrow functions fix(lint): correct multiline autofix in useConsistentArrowReturn Nov 20, 2025
@hornta hornta marked this pull request as ready for review November 20, 2025 18:12
Comment on lines 28 to 31
```js
const foo = (l) => {
return l.split('\n');
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: this code block is not closed...

Comment on lines 9 to 31
For example, this code:

```js
const foo = (l) =>
l
.split('\n')
```

Was incorrectly fixed to:

```js
const foo = (l) => {
return
l.split('\n');
}
```

Biome now correctly produces:

```js
const foo = (l) => {
return l.split('\n');
}
Copy link
Contributor

Choose a reason for hiding this comment

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

...but actually you could probably make this a little bit more concise using a diff block to show the change.


Fixed [#8179](https://github.com/biomejs/biome/issues/8179): The [`useConsistentArrowReturn`](https://biomejs.dev/linter/rules/use-consistent-arrow-return/) rule now correctly handles multiline expressions in its autofix when the `style` option is set to `"always"`.

Previously, when converting arrow functions with multiline expressions to block statements, the autofix would place a newline after the `return` keyword. This triggered JavaScript's Automatic Semicolon Insertion (ASI), causing the function to return `undefined` instead of the intended value.
Copy link
Contributor

@dyc3 dyc3 Nov 21, 2025

Choose a reason for hiding this comment

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

nit: This statement (line 7) isn't entirely accurate and IMO its a bit too "in the weeds" for end users to care about. I would rather we just omit this paragraph.

" ",
)]))
.with_argument(expr_to_return)
.with_argument(expr_to_return.trim_leading_trivia()?)
Copy link
Contributor

Choose a reason for hiding this comment

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

This would get rid of comments. Let's make sure the comments get preserved and moved to the correct nodes.

Copy link
Contributor

Choose a reason for hiding this comment

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

Include comments in these tests so we can verify we don't accidentally delete comments.

@codspeed-hq
Copy link

codspeed-hq bot commented Nov 21, 2025

CodSpeed Performance Report

Merging #8183 will not alter performance

Comparing hornta:main (bd77c58) with main (98ca2ae)

Summary

✅ 58 untouched
⏩ 95 skipped1

Footnotes

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

@hornta
Copy link
Contributor Author

hornta commented Nov 25, 2025

I think I've addressed all comments. I've added a few code snippets that has comments and noticed they didn't generate something in the snapshots which I think means they wont be like autofixable and that the user has to address them manually?

Copy link
Contributor

@dyc3 dyc3 left a comment

Choose a reason for hiding this comment

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

Nicely done!

@dyc3 dyc3 merged commit b064786 into biomejs:main Dec 14, 2025
18 checks passed
@github-actions github-actions bot mentioned this pull request Dec 14, 2025
@coderabbitai coderabbitai bot mentioned this pull request Dec 14, 2025
This was referenced Dec 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Linter Area: linter L-JavaScript Language: JavaScript and super languages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 useConsistentArrowReturn autofix produces semantically incorrect code (returns undefined)

2 participants