Skip to content

Commit

Permalink
chore: update detail
Browse files Browse the repository at this point in the history
  • Loading branch information
fireairforce committed Sep 27, 2024
1 parent ace637e commit 0c0be67
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 18 deletions.
2 changes: 1 addition & 1 deletion crates/biome_configuration/src/analyzer/linter/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3379,7 +3379,7 @@ pub struct Nursery {
#[serde(skip_serializing_if = "Option::is_none")]
pub use_explicit_function_return_type:
Option<RuleConfiguration<biome_js_analyze::options::UseExplicitFunctionReturnType>>,
#[doc = "Require for-in loops to include an if statement"]
#[doc = "Require for-in loops to include an if statement."]
#[serde(skip_serializing_if = "Option::is_none")]
pub use_guard_for_in: Option<RuleConfiguration<biome_js_analyze::options::UseGuardForIn>>,
#[doc = "Disallows package private imports."]
Expand Down
15 changes: 12 additions & 3 deletions crates/biome_js_analyze/src/lint/nursery/use_guard_for_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ use biome_rowan::{AstNode, AstNodeList};

declare_lint_rule! {
///
/// Require `for-in` loops to include an `if` statement
/// Require `for-in` loops to include an `if` statement.
///
/// Looping over objects with a for in loop will include properties that are 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 alternative.
///
/// ## Examples
///
Expand Down Expand Up @@ -117,11 +124,13 @@ impl Rule for UseGuardForIn {
rule_category!(),
node.range(),
markup! {
"Require `for-in` loops to include an `if` statement."
"The body of a for-in should be wrapped in an `if` statement."
},
)
.note(markup! {
"The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype."
"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 wrapper the body of for-in loop. "
}),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,42 @@ for (var x in o) foo();
```
invalid.js:1:1 lint/nursery/useGuardForIn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Require `for-in` loops to include an `if` statement.
! The body of a for-in should be wrapped in an `if` statement.
> 1 │ for (var x in o) { if (x) { f(); continue; } g(); }
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │ for (var x in o) { if (x) { continue; f(); } g(); }
3 │ for (var x in o) { if (x) { f(); } g(); }
i The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype.
i 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.
i To resolve this issue, add an if statement like `if (Object.hasOwn(foo, key)) {...}` to wrapper the body of for-in loop.
```

```
invalid.js:2:1 lint/nursery/useGuardForIn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Require `for-in` loops to include an `if` statement.
! The body of a for-in should be wrapped in an `if` statement.
1 │ for (var x in o) { if (x) { f(); continue; } g(); }
> 2 │ for (var x in o) { if (x) { continue; f(); } g(); }
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │ for (var x in o) { if (x) { f(); } g(); }
4 │ for (var x in o) { if (x) f(); g(); }
i The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype.
i 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.
i To resolve this issue, add an if statement like `if (Object.hasOwn(foo, key)) {...}` to wrapper the body of for-in loop.
```

```
invalid.js:3:1 lint/nursery/useGuardForIn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Require `for-in` loops to include an `if` statement.
! The body of a for-in should be wrapped in an `if` statement.
1 │ for (var x in o) { if (x) { f(); continue; } g(); }
2 │ for (var x in o) { if (x) { continue; f(); } g(); }
Expand All @@ -56,15 +60,17 @@ invalid.js:3:1 lint/nursery/useGuardForIn ━━━━━━━━━━━━
4 │ for (var x in o) { if (x) f(); g(); }
5 │ for (var x in o) { foo() }
i The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype.
i 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.
i To resolve this issue, add an if statement like `if (Object.hasOwn(foo, key)) {...}` to wrapper the body of for-in loop.
```

```
invalid.js:4:1 lint/nursery/useGuardForIn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Require `for-in` loops to include an `if` statement.
! The body of a for-in should be wrapped in an `if` statement.
2 │ for (var x in o) { if (x) { continue; f(); } g(); }
3 │ for (var x in o) { if (x) { f(); } g(); }
Expand All @@ -73,38 +79,44 @@ invalid.js:4:1 lint/nursery/useGuardForIn ━━━━━━━━━━━━
5 │ for (var x in o) { foo() }
6 │ for (var x in o) foo();
i The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype.
i 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.
i To resolve this issue, add an if statement like `if (Object.hasOwn(foo, key)) {...}` to wrapper the body of for-in loop.
```

```
invalid.js:5:1 lint/nursery/useGuardForIn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Require `for-in` loops to include an `if` statement.
! The body of a for-in should be wrapped in an `if` statement.
3 │ for (var x in o) { if (x) { f(); } g(); }
4 │ for (var x in o) { if (x) f(); g(); }
> 5 │ for (var x in o) { foo() }
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^
6 │ for (var x in o) foo();
i The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype.
i 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.
i To resolve this issue, add an if statement like `if (Object.hasOwn(foo, key)) {...}` to wrapper the body of for-in loop.
```

```
invalid.js:6:1 lint/nursery/useGuardForIn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Require `for-in` loops to include an `if` statement.
! The body of a for-in should be wrapped in an `if` statement.
4 │ for (var x in o) { if (x) f(); g(); }
5 │ for (var x in o) { foo() }
> 6 │ for (var x in o) foo();
│ ^^^^^^^^^^^^^^^^^^^^^^^
i The body of a for-in should be wrapped in an if statement to filter unwanted properties from the prototype.
i 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.
i To resolve this issue, add an if statement like `if (Object.hasOwn(foo, key)) {...}` to wrapper the body of for-in loop.
```
2 changes: 1 addition & 1 deletion packages/@biomejs/backend-jsonrpc/src/workspace.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/@biomejs/biome/configuration_schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0c0be67

Please sign in to comment.