-
-
Notifications
You must be signed in to change notification settings - Fork 780
feat(biome_js_analyze): implement noReturnAssign rule #8248
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
Open
emilyinure
wants to merge
14
commits into
biomejs:main
Choose a base branch
from
emilyinure:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,224
−79
Open
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
bedd78b
feat(biome_js_analyze): implement noReturnAssign rule
emilyinure 78c45db
chore: add changeset
emilyinure a5fd33d
[autofix.ci] apply automated fixes
autofix-ci[bot] ab1c937
fix(noReturnAssign): correct names in rule options, add missing expre…
emilyinure 0975ae9
add arrowfunction handling, update changeset, update tests, remove ru…
emilyinure ab54ee3
[autofix.ci] apply automated fixes
autofix-ci[bot] 9e18238
fix inverted diagnostics
emilyinure 6905c52
chore: remove diagnostic tutorial
emilyinure e463f37
fix(noReturnAssign): simplify implementation, produce multiple signals
emilyinure a59ca8b
Apply suggestion from @ematipico
emilyinure 87af8c5
fix(noReturnAssign): convert signal to vec, add tests for multiple
emilyinure dff0faf
chore: add missing test cases for different expressions and arrow fun…
emilyinure 32fae07
chore: remove debug print
emilyinure 28cf924
chore: fix mismatched snap results(?)
emilyinure File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| --- | ||
| "@biomejs/biome": patch | ||
| --- | ||
|
|
||
| Added new rule | ||
| [noReturnAssign](https://biomejs.dev/linter/rules/no-return-assign): | ||
|
|
||
| Disallows assignments inside return statements | ||
|
|
||
|
|
||
| Based on [no-return-assign](https://eslint.org/docs/latest/rules/no-return-assign) | ||
|
|
||
| Disallowed example: | ||
|
|
||
| ```js | ||
| function f(a) { | ||
| return a = 1; | ||
| } | ||
| ``` |
12 changes: 12 additions & 0 deletions
12
crates/biome_cli/src/execute/migrate/eslint_any_rule_to_biome.rs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
173 changes: 97 additions & 76 deletions
173
crates/biome_configuration/src/analyzer/linter/rules.rs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
crates/biome_js_analyze/src/lint/nursery/no_return_assign.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| use biome_analyze::{ | ||
| QueryMatch, Rule, RuleDiagnostic, RuleSource, context::RuleContext, declare_lint_rule, | ||
| }; | ||
| use biome_console::markup; | ||
| use biome_diagnostics::Severity; | ||
| use biome_js_syntax::{ | ||
| AnyJsExpression, JsArrowFunctionExpression, JsAssignmentExpression, JsReturnStatement, | ||
| }; | ||
| use biome_rowan::{AstNode, TextRange, WalkEvent, declare_node_union}; | ||
| use biome_rule_options::no_return_assign::NoReturnAssignOptions; | ||
|
|
||
| use crate::services::semantic::Semantic; | ||
|
|
||
| declare_lint_rule! { | ||
| /// Disallow assignments in return statements. | ||
| /// | ||
| /// In return statements, it is common to mistype a comparison operator (such as `==`) as an assignment operator (such as `=`). | ||
| /// Moreover, the use of assignments in a return statement is confusing. | ||
| /// Return statements are often considered side-effect free. | ||
| /// | ||
| /// ## Examples | ||
| /// | ||
| /// ### Invalid | ||
| /// | ||
| /// ```js,expect_diagnostic | ||
| /// function f(a) { | ||
| /// return a = 1; | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| /// ### Valid | ||
| /// | ||
| /// ```js | ||
| /// function f(a) { | ||
| /// a = 1; | ||
| /// return a; | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| /// ```js | ||
| /// function f(a) { | ||
| /// return a == 1; | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| pub NoReturnAssign { | ||
| version: "next", | ||
| name: "noReturnAssign", | ||
| language: "js", | ||
| sources: &[RuleSource::Eslint("no-return-assign").same()], | ||
| recommended: false, | ||
| severity: Severity::Error, | ||
| } | ||
| } | ||
|
|
||
| declare_node_union! { | ||
| pub AnyReturn = JsReturnStatement | JsArrowFunctionExpression | ||
| } | ||
|
|
||
| impl Rule for NoReturnAssign { | ||
| type Query = Semantic<AnyReturn>; | ||
| type State = TextRange; | ||
| type Signals = Vec<Self::State>; | ||
| type Options = NoReturnAssignOptions; | ||
|
|
||
| fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
| run_options(ctx).unwrap_or_default() | ||
| } | ||
|
|
||
| fn diagnostic(ctx: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> { | ||
| Some( | ||
| RuleDiagnostic::new( | ||
| rule_category!(), | ||
| state, | ||
| match ctx.query() { | ||
| AnyReturn::JsArrowFunctionExpression(_) => markup! { | ||
| <Emphasis>"Arrow function"</Emphasis>" should not return "<Emphasis>"assignment"</Emphasis>"." | ||
| }, | ||
| AnyReturn::JsReturnStatement(_) => markup! { | ||
| <Emphasis>"Function"</Emphasis>" should not return "<Emphasis>"assignment"</Emphasis>"." | ||
| }, | ||
| } | ||
| ).note(markup! { | ||
| "Return statements are often considered side-effect free.\nYou likely want to do a comparison `==`\nOtherwise move the assignment outside of the return statement" | ||
| })) | ||
| } | ||
| } | ||
|
|
||
| fn run_options(ctx: &RuleContext<NoReturnAssign>) -> Option<Vec<TextRange>> { | ||
| match ctx.query() { | ||
| AnyReturn::JsReturnStatement(query) => Some(traverse_expression(&query.argument()?)), | ||
|
|
||
| AnyReturn::JsArrowFunctionExpression(query) => Some(traverse_expression( | ||
| query.body().ok()?.as_any_js_expression()?, | ||
| )), | ||
| } | ||
| } | ||
|
|
||
| fn traverse_expression(root: &AnyJsExpression) -> Vec<TextRange> { | ||
| let mut signal = Vec::new(); | ||
| let mut iter = root.syntax().preorder(); | ||
|
|
||
| while let Some(event) = iter.next() { | ||
| if let WalkEvent::Enter(node) = event { | ||
| if JsAssignmentExpression::can_cast(node.kind()) { | ||
| signal.push(node.text_range()); | ||
| } | ||
|
|
||
| let is_expression = AnyJsExpression::can_cast(node.kind()); | ||
|
|
||
| if !is_expression { | ||
| std::println!("{:?}", node.kind()); | ||
| iter.skip_subtree(); | ||
| } | ||
emilyinure marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| signal | ||
| } | ||
110 changes: 110 additions & 0 deletions
110
crates/biome_js_analyze/tests/specs/nursery/noReturnAssign/invalid.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /* should generate diagnostics */ | ||
|
|
||
| const f = (a) => a = 1; | ||
|
|
||
| function f(a) { | ||
| return a = 1; | ||
| } | ||
|
|
||
| function f(a) { | ||
| return (a = 1); | ||
| } | ||
|
|
||
| function f(a, b, c) { | ||
| return (a, b, c = 1); | ||
| } | ||
|
|
||
| function f(a, b, c) { | ||
| return a == (b = c); | ||
| } | ||
| function f(a, b) { | ||
| return a && (b = 1); | ||
| } | ||
|
|
||
| function f(a, b, c) { | ||
| return a = (b = c); | ||
| } | ||
|
|
||
| function f(a, b, c) { | ||
| return (a = 1, b = 2, c = 3); | ||
| } | ||
|
|
||
| function f(a) { | ||
| return [a = 1]; | ||
| } | ||
|
|
||
| async function f(a) { | ||
| return await (a = 1); | ||
| } | ||
|
|
||
| function f(a) { | ||
| return 5 + (a = 1); | ||
| } | ||
|
|
||
| function f(a) { | ||
| return (a = 1) + 5; | ||
| } | ||
|
|
||
| function f(a) { | ||
| return foo(a = 1); | ||
| } | ||
|
|
||
| function f(a, b) { | ||
| return b[a = 1]; | ||
| } | ||
|
|
||
| function f(a) { | ||
| return (a = 1) ? true : false; | ||
| } | ||
|
|
||
| function f(a) { | ||
| return true ? a = 1 : false; | ||
| } | ||
|
|
||
| function f(a, b) { | ||
| return true ? false : a = 1; | ||
| } | ||
|
|
||
| function f(a, b) { | ||
| return (a = 1) in b; | ||
| } | ||
|
|
||
| function f(a, Class) { | ||
| return (a = 1) instanceof Class; | ||
| } | ||
|
|
||
| function f(a, b) { | ||
| return (a = 1) || b; | ||
| } | ||
|
|
||
| function f(a, b) { | ||
| return a || (b = 1); | ||
| } | ||
|
|
||
| function f(a, b) { | ||
| return (a = 1) ?? b; | ||
| } | ||
|
|
||
| function f(a, b) { | ||
| return a ?? (b = 1); | ||
| } | ||
|
|
||
| function f(a) { | ||
| return !(a = 1); | ||
| } | ||
|
|
||
| function f(a) { | ||
| return typeof (a = 1); | ||
| } | ||
|
|
||
| function f(a) { | ||
| return void (a = 1); | ||
| } | ||
|
|
||
| function f(a) { | ||
| return -(a = 1); | ||
| } | ||
|
|
||
| function f(a) { | ||
| return <div prop={a = 1} />; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.