-
-
Notifications
You must be signed in to change notification settings - Fork 760
fix(useArrowFunction): make useArrowFunction rule fixer safer #8009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This avoids auto-fixing functions if they refer to the arguments object, because arguments is not defined for arrow functions.
๐ฆ Changeset detectedLatest commit: 2a92943 The changes in this PR will be included in the next version bump. This PR includes changesets to release 13 packages
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 |
WalkthroughThe useArrowFunction rule was changed to use semantic analysis ( Suggested labels
Suggested reviewers
Pre-merge checks and finishing touchesโ Failed checks (1 warning)
โ Passed checks (4 passed)
โจ Finishing touches๐งช Generate unit tests (beta)
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. Comment |
|
we can definitely detect if a file is in script mode. |
CodSpeed Performance ReportMerging #8009 will not alter performanceComparing Summary
Footnotes
|
ematipico
left a comment
There was a problem hiding this 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 |
There was a problem hiding this comment.
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
.changeset/yellow-mangos-clean.md
Outdated
| "@biomejs/biome": patch | ||
| --- | ||
|
|
||
| Make useArrowFunction fixer safer |
There was a problem hiding this comment.
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
|
Thanks for the review, I've rewritten the changeset and doc comment and hopefully clarified them. |
There was a problem hiding this 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 aboutargumentssemantics.The comment mentions "only permitted in script mode, not module mode", but the key point is simpler: the code checks whether
argumentshas 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
๐ 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 whetherargumentsrefers 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
thistypes, 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
thisornew.targetis found (lines 147-154)- Detects unbound
argumentsreferences via semantic binding resolution (lines 156-164)The logic at line 158 correctly identifies the implicit
argumentsobject by checking for the absence of a binding.
173-195: Methods updated for semantic query API.The
text_rangeanddiagnosticmethods correctly use the newctx.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_parentheseslogic andAnyThisScopeunion properly support the refactored rule.
310-346: Body transformation logic preserved.The
to_arrow_bodyfunction correctly handles edge cases (directives, comments, sequences, object literals) when converting function bodies.
There was a problem hiding this 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
๐ 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
argumentsusage.
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
argumentsobject, 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
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.