Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@tmcw
Copy link
Contributor

@tmcw tmcw commented Nov 6, 2025

Summary

This avoids auto-fixing functions if they refer to the arguments object, because arguments is not defined for arrow functions. Even though there are newer ways to use arguments in JavaScript, there's a lot of legacy code that still uses the arguments object, and it's a bummer for biome to break that code.

Note that this change is a little more intensive than I had hoped: the useArrowFunction rule previously worked only by traversing the AST, whereas in order to figure out if an arguments object has been redefined, it need to use a semantic model, and I couldn't find a way to add that to the existing approach, so this switches it to use the Semantic<> style of rule, and traversing with the preorder iterator instead of a visitor. If there's a better way to do this, I'm all ears!

Also notably that pattern of being able to resassign arguments like

function x() {
	const arguments = 10;
}

This only works in script mode. So, an alternative solution would be to simply never fix a function if it both mentions arguments in any way, and is in script mode.

Test Plan

This includes updates to the test snapshots for the cases where it should fix & not fix functions that mention arguments.

tmcw added 2 commits November 5, 2025 20:13
This avoids auto-fixing functions if they refer to the
arguments object, because arguments is not defined for
arrow functions.
@changeset-bot
Copy link

changeset-bot bot commented Nov 6, 2025

๐Ÿฆ‹ Changeset detected

Latest commit: 2a92943

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 6, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 6, 2025

Walkthrough

The useArrowFunction rule was changed to use semantic analysis (type Query = Semantic<AnyThisScope>) and now inspects the semantic model to detect this, new.target and arguments usage before offering an arrow-function fix. The previous query/visitor scaffolding (ActualThisScope, AnyThisScopeMetadata and related Queryable/QueryMatch) was removed. Diagnostics and fix generation were updated to use the new semantic query API. Tests were added to cover functions that reference or shadow arguments.

Suggested labels

A-Diagnostic

Suggested reviewers

  • dyc3

Pre-merge checks and finishing touches

โŒ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage โš ๏ธ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
โœ… Passed checks (4 passed)
Check name Status Explanation
Title check โœ… Passed The title accurately reflects the core change: making the useArrowFunction rule safer by preventing auto-fixing when the arguments object is referenced.
Description check โœ… Passed The description clearly explains the fix for #7616, the implementation approach using semantic analysis, and acknowledges the architectural trade-offs involved.
Linked Issues check โœ… Passed The PR addresses all coding requirements from #7616: detects and respects arguments usage, prevents auto-fixing to avoid breaking legacy code, and includes test snapshot updates.
Out of Scope Changes check โœ… Passed All changes are directly scoped to fixing the useArrowFunction rule: rule implementation updates, semantic model integration, and targeted test cases for arguments handling.
โœจ Finishing touches
๐Ÿงช Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

@dyc3
Copy link
Contributor

dyc3 commented Nov 6, 2025

we can definitely detect if a file is in script mode. JsFileSource should have that info in module_kind.

@tmcw tmcw changed the title Make useArrowFunction rule fixer safer fix: Make useArrowFunction rule fixer safer Nov 6, 2025
@tmcw tmcw changed the title fix: Make useArrowFunction rule fixer safer fix(lint): make useArrowFunction rule fixer safer Nov 6, 2025
@tmcw tmcw changed the title fix(lint): make useArrowFunction rule fixer safer fix(useArrowFunction): make useArrowFunction rule fixer safer Nov 6, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Nov 6, 2025

CodSpeed Performance Report

Merging #8009 will not alter performance

Comparing tmcw:use-arrow-function-arguments (2a92943) with main (5c55347)1

Summary

โœ… 53 untouched
โฉ 85 skipped2

Footnotes

  1. No successful run was found on main (6e8a50e) during the generation of this report, so 5c55347 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. โ†ฉ

  2. 85 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. โ†ฉ

Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

Thank you. Code looks good. Docs should be improved, as well as the changeset

/// return 0;
/// }
/// ```
/// Functions that reference the arguments object
Copy link
Member

Choose a reason for hiding this comment

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

We should add a link to arguments. Without a special explanation, and format, newbies won't know what arguments is

"@biomejs/biome": patch
---

Make useArrowFunction fixer safer
Copy link
Member

Choose a reason for hiding this comment

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

  • start sentences with the past (as per guidelines, please read them)
  • use backticks for the rule name
  • add a link to the rule
  • change "fixer" with "code fix", that's how we document it in the docs

@tmcw
Copy link
Contributor Author

tmcw commented Nov 6, 2025

Thanks for the review, I've rewritten the changeset and doc comment and hopefully clarified them.

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 (3)
.changeset/yellow-mangos-clean.md (2)

7-9: Consider using "code fix" terminology.

The wording "offers to fix" could be aligned with Biome's documentation style by using "offers a code fix" instead.

Based on coding guidelines.

Apply this diff:

-The rule no longer emits diagnostics for or offers to fix functions that reference
+The rule no longer emits diagnostics or offers a code fix for functions that reference

8-8: Remove comma before essential "because" clause.

The comma before "because" should be removed when the clause provides essential context (why the rule doesn't apply).

Apply this diff:

-the [arguments object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments),
-because that object is undefined for arrow functions.
+the [arguments object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments)
+because that object is undefined for arrow functions.
crates/biome_js_analyze/src/lint/complexity/use_arrow_function.rs (1)

160-162: Clarify the comment about arguments semantics.

The comment mentions "only permitted in script mode, not module mode", but the key point is simpler: the code checks whether arguments has been shadowed by a local declaration. Consider rephrasing for clarity:

-                    // This method refers to arguments, and arguments has not
-                    // been reassigned as a variable (which is only permitted in
-                    // script mode, not module mode)
+                    // This function refers to the implicit arguments object
+                    // (not a locally-declared variable named "arguments")
๐Ÿ“œ 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 aac5901 and 1644a4f.

๐Ÿ“’ Files selected for processing (2)
  • .changeset/yellow-mangos-clean.md (1 hunks)
  • crates/biome_js_analyze/src/lint/complexity/use_arrow_function.rs (6 hunks)
๐Ÿงฐ Additional context used
๐Ÿง  Learnings (3)
๐Ÿ“š Learning: 2025-10-15T09:23:33.055Z
Learnt from: CR
Repo: biomejs/biome PR: 0
File: crates/biome_js_type_info/CONTRIBUTING.md:0-0
Timestamp: 2025-10-15T09:23:33.055Z
Learning: Applies to crates/biome_js_type_info/src/{type_info,local_inference,resolver,flattening}.rs : Avoid recursive type structures and cross-module Arcs; represent links between types using TypeReference and TypeData::Reference.

Applied to files:

  • crates/biome_js_analyze/src/lint/complexity/use_arrow_function.rs
๐Ÿ“š Learning: 2025-10-15T09:23:33.055Z
Learnt from: CR
Repo: biomejs/biome PR: 0
File: crates/biome_js_type_info/CONTRIBUTING.md:0-0
Timestamp: 2025-10-15T09:23:33.055Z
Learning: Applies to crates/biome_js_type_info/biome_module_graph/src/js_module_info/scoped_resolver.rs : Full inference must resolve TypeReference::Import across modules to TypeReference::Resolved when the target is available in the module graph.

Applied to files:

  • crates/biome_js_analyze/src/lint/complexity/use_arrow_function.rs
๐Ÿ“š Learning: 2025-10-15T09:22:46.002Z
Learnt from: CR
Repo: biomejs/biome PR: 0
File: crates/biome_js_formatter/CONTRIBUTING.md:0-0
Timestamp: 2025-10-15T09:22:46.002Z
Learning: Applies to crates/biome_js_formatter/**/*.rs : Import and use the `FormatNode` trait for AST nodes

Applied to files:

  • crates/biome_js_analyze/src/lint/complexity/use_arrow_function.rs
๐Ÿงฌ Code graph analysis (1)
crates/biome_js_analyze/src/lint/complexity/use_arrow_function.rs (3)
packages/@biomejs/backend-jsonrpc/src/workspace.ts (4)
  • FixKind (8036-8036)
  • JsFileSource (9570-9579)
  • TextRange (9414-9414)
  • UseArrowFunctionOptions (8156-8156)
crates/biome_rowan/src/syntax/node.rs (1)
  • preorder (384-389)
crates/biome_rowan/src/ast/mod.rs (1)
  • cast_ref (143-152)
๐Ÿช› LanguageTool
.changeset/yellow-mangos-clean.md

[formatting] ~8-~8: If the โ€˜becauseโ€™ clause is essential to the meaning, do not use a comma before the clause.
Context: ...avaScript/Reference/Functions/arguments), because that object is undefined for arrow func...

(COMMA_BEFORE_BECAUSE)

๐Ÿ”‡ Additional comments (9)
crates/biome_js_analyze/src/lint/complexity/use_arrow_function.rs (9)

1-20: Import additions support the semantic refactor.

The new imports (Semantic, BindingExtensions, WalkEvent) are appropriate for the shift from visitor-based to semantic model-based analysis.


63-72: Clear documentation with proper context.

The added documentation explains the arguments edge case well and includes a helpful example.


93-96: Semantic query enables binding analysis.

The switch to Semantic<AnyThisScope> is essential for detecting whether arguments refers to the implicit object or a shadowed variable.


98-137: Setup and early checks are sound.

The semantic model setup and early-return conditions correctly preserve the rule's existing exclusions (generators, named functions, explicit this types, prototype requirements).


139-168: Traversal logic correctly detects unsafe patterns.

The preorder traversal properly:

  • Skips nested function scopes to avoid false positives (lines 142-146)
  • Blocks transformation when this or new.target is found (lines 147-154)
  • Detects unbound arguments references via semantic binding resolution (lines 156-164)

The logic at line 158 correctly identifies the implicit arguments object by checking for the absence of a binding.


173-195: Methods updated for semantic query API.

The text_range and diagnostic methods correctly use the new ctx.query() API.


197-252: Action method correctly migrated to semantic query.

The fix generation logic properly uses ctx.query() whilst preserving the arrow function transformation behaviour.


254-308: Helper function and union type are sound.

The needs_parentheses logic and AnyThisScope union properly support the refactored rule.


310-346: Body transformation logic preserved.

The to_arrow_body function correctly handles edge cases (directives, comments, sequences, object literals) when converting function bodies.

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 (1)
.changeset/yellow-mangos-clean.md (1)

7-9: Minor grammar fix: remove comma before "because".

Line 8 has an unnecessary comma before the "because" clause. Remove it for better readability.

 The rule no longer emits diagnostics for or offers to fix functions that reference
-the [arguments object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments), because that object is undefined for arrow functions.
+the [arguments object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments) because that object is undefined for arrow functions.
๐Ÿ“œ 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 1644a4f and 2a92943.

๐Ÿ“’ Files selected for processing (1)
  • .changeset/yellow-mangos-clean.md (1 hunks)
๐Ÿงฐ Additional context used
๐Ÿง  Learnings (2)
๐Ÿ“š 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:

  • .changeset/yellow-mangos-clean.md
๐Ÿ“š 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:

  • .changeset/yellow-mangos-clean.md
๐Ÿช› LanguageTool
.changeset/yellow-mangos-clean.md

[formatting] ~8-~8: If the โ€˜becauseโ€™ clause is essential to the meaning, do not use a comma before the clause.
Context: ...avaScript/Reference/Functions/arguments), because that object is undefined for arrow func...

(COMMA_BEFORE_BECAUSE)

๐Ÿ”‡ Additional comments (1)
.changeset/yellow-mangos-clean.md (1)

13-18: Example is clear and illustrative. โœ“

The code snippet effectively demonstrates a function that cannot be safely converted to an arrow function due to arguments usage.

@ematipico ematipico merged commit 6374b1f into biomejs:main Nov 6, 2025
18 checks passed
@github-actions github-actions bot mentioned this pull request Nov 6, 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.

๐Ÿ“ useArrowFunction rule does not pay attention to use of arguments, breaks functions that use it

3 participants