-
-
Notifications
You must be signed in to change notification settings - Fork 471
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useGuardForIn): add rule (#4104)
- Loading branch information
1 parent
2e5b656
commit 970f498
Showing
13 changed files
with
341 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
8 changes: 8 additions & 0 deletions
8
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.
This file contains 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 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 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
136 changes: 136 additions & 0 deletions
136
crates/biome_js_analyze/src/lint/nursery/use_guard_for_in.rs
This file contains 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,136 @@ | ||
use biome_analyze::{ | ||
context::RuleContext, declare_lint_rule, Ast, Rule, RuleDiagnostic, RuleSource, | ||
}; | ||
use biome_console::markup; | ||
use biome_js_syntax::{AnyJsStatement, JsForInStatement}; | ||
use biome_rowan::{AstNode, AstNodeList}; | ||
|
||
declare_lint_rule! { | ||
/// Require `for-in` loops to include an `if` statement. | ||
/// | ||
/// Looping over objects with a `for-in` loop will include properties inherited through the prototype chain. | ||
/// This behavior can lead to unexpected items in your for loop. | ||
/// | ||
/// For codebases that do not support ES2022, `Object.prototype.hasOwnProperty.call(foo, key)` can be used as a check that the property is not inherited. | ||
/// | ||
/// For codebases that do support ES2022, `Object.hasOwn(foo, key)` can be used as a shorter and more reliable alternative. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// for (key in foo) { | ||
/// doSomething(key); | ||
/// } | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```js | ||
/// for (key in foo) { | ||
/// if (Object.hasOwn(foo, key)) { | ||
/// doSomething(key); | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// ```js | ||
/// for (key in foo) { | ||
/// if (Object.prototype.hasOwnProperty.call(foo, key)) { | ||
/// doSomething(key); | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// ```js | ||
/// for (key in foo) { | ||
/// if ({}.hasOwnProperty.call(foo, key)) { | ||
/// doSomething(key); | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
pub UseGuardForIn { | ||
version: "next", | ||
name: "useGuardForIn", | ||
language: "js", | ||
sources: &[RuleSource::Eslint("guard-for-in")], | ||
recommended: false, | ||
} | ||
} | ||
|
||
impl Rule for UseGuardForIn { | ||
type Query = Ast<JsForInStatement>; | ||
type State = (); | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let node = ctx.query(); | ||
let body = node.body().ok()?; | ||
|
||
match body { | ||
AnyJsStatement::JsEmptyStatement(_) | AnyJsStatement::JsIfStatement(_) => None, | ||
AnyJsStatement::JsBlockStatement(block) => { | ||
let statements = block.statements(); | ||
|
||
match statements.len() { | ||
0 => None, | ||
1 => { | ||
let first_statement = statements.first()?; | ||
if first_statement.as_js_if_statement().is_none() { | ||
Some(()) | ||
} else { | ||
None | ||
} | ||
} | ||
_ => { | ||
let first_statement = statements.first()?; | ||
if let Some(first_if_statement) = first_statement.as_js_if_statement() { | ||
match first_if_statement.consequent().ok()? { | ||
AnyJsStatement::JsBlockStatement(block) | ||
if block.statements().len() == 1 => | ||
{ | ||
if block | ||
.statements() | ||
.first()? | ||
.as_js_continue_statement() | ||
.is_none() | ||
{ | ||
Some(()) | ||
} else { | ||
None | ||
} | ||
} | ||
AnyJsStatement::JsContinueStatement(_) => None, | ||
_ => Some(()), | ||
} | ||
} else { | ||
Some(()) | ||
} | ||
} | ||
} | ||
} | ||
_ => Some(()), | ||
} | ||
} | ||
|
||
fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> { | ||
let node = ctx.query(); | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
node.range(), | ||
markup! { | ||
"The body of a for-in should be wrapped in an `if` statement." | ||
}, | ||
) | ||
.note(markup! { | ||
"Looping over the object with for-in loop will include properties that are inherited through the prototype chain, the behaviour can lead to some unexpected items in your loop." | ||
}).note(markup! { | ||
"To resolve this issue, add an if statement like `if (Object.hasOwn(foo, key)) {...}` to filter out the extraneous properties. " | ||
}), | ||
) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
crates/biome_js_analyze/tests/specs/nursery/useGuardForIn/invalid.js
This file contains 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,6 @@ | ||
for (var x in o) { if (x) { f(); continue; } g(); } | ||
for (var x in o) { if (x) { continue; f(); } g(); } | ||
for (var x in o) { if (x) { f(); } g(); } | ||
for (var x in o) { if (x) f(); g(); } | ||
for (var x in o) { foo() } | ||
for (var x in o) foo(); |
Oops, something went wrong.