useGuardForIn
Este conteúdo não está disponível em sua língua ainda.
Diagnostic Category: lint/nursery/useGuardForIn
Since: v1.9.4
Sources:
- Same as:
guard-for-in
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
Section titled ExamplesInvalid
Section titled Invalidcode-block.js:1:1 lint/nursery/useGuardForIn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ The body of a for-in should be wrapped in an if
statement.
> 1 │ for (key in foo) {
│ ^^^^^^^^^^^^^^^^^^
> 2 │ doSomething(key);
> 3 │ }
│ ^
4 │
ℹ 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.
ℹ To resolve this issue, add an if statement like if (Object.hasOwn(foo, key)) {...}
to filter out the extraneous properties.