Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions crates/oxc_linter/src/rules/eslint/guard_for_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,47 @@ pub struct GuardForIn;

declare_oxc_lint!(
/// ### What it does
/// This rule is aimed at preventing unexpected behavior that could arise from using a for in loop without filtering the results in the loop. As such, it will warn when for in loops do not filter their results with an if statement.
///
/// Require for-in loops to include an if statement.
///
/// ### Why is this bad?
///
/// Looping over objects with a `for in` loop will include properties that are inherited through
/// the prototype chain. Using a `for in` loop without filtering the results in the loop can
/// lead to unexpected items in your for loop which can then lead to unexpected behaviour.
///
/// ### Examples
///
/// Examples of **incorrect** code for this rule:
///
/// ```javascript
/// for (key in foo) {
/// doSomething(key);
/// }
/// ```
///
/// ### Example
/// Examples of **correct** code for this rule:
/// ```javascript
/// for (key in foo) {
/// if (Object.hasOwn(foo, key)) {
/// doSomething(key);
/// }
/// }
/// ```
///
/// ```javascript
/// for (key in foo) {
/// if (Object.prototype.hasOwnProperty.call(foo, key)) {
/// doSomething(key);
/// }
/// }
/// ```
///
/// ```javascript
/// for (key in foo) {
/// if ({}.hasOwnProperty.call(foo, key)) {
/// doSomething(key);
/// }
/// }
/// ```
GuardForIn,
Expand Down