noNegatedArrayPredicate (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.6 - Diagnostic Category:
lint/nursery/noNegatedArrayPredicate - This rule has an unsafe fix.
- The default severity of this rule is information.
- Sources:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noNegatedArrayPredicate": "error" } } }}Description
Section titled “Description”Disallow negating the result of Array#some() or Array#every().
!array.some(callback) checks that no item passes the test, which is the same as checking
that every item fails it: array.every((item) => !callback(item)). In the same way,
!array.every(callback) is the same as array.some((item) => !callback(item)).
Putting the ! inside the callback function keeps the condition next to the test it changes,
and two ! operators that cancel each other out, as in !array.some((item) => !item.done),
can be removed entirely: array.every((item) => item.done).
The rule only reports calls whose callback function returns a single expression, either
directly, as in (item) => item.done, or with one return statement.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”const isMissing = !array.some((item) => isUnicorn(item));code-block.js:1:26 lint/nursery/noNegatedArrayPredicate FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The result of some() is negated with !.
> 1 │ const isMissing = !array.some((item) => isUnicorn(item));
│ ^^^^
2 │
ℹ Negating the whole call hides the condition that is actually checked. Calling every() with a negated callback function gives the same result and is easier to read.
ℹ Remove the ! before the call, use every() instead, and negate the value that the callback function returns.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use every() with a negated callback function.
1 │ - const·isMissing·=·!array.some((item)·=>·isUnicorn(item));
1 │ + const·isMissing·=·array.every((item)·=>·!isUnicorn(item));
2 2 │
const isNotEveryUnicorn = !array.every((item) => isUnicorn(item));code-block.js:1:34 lint/nursery/noNegatedArrayPredicate FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The result of every() is negated with !.
> 1 │ const isNotEveryUnicorn = !array.every((item) => isUnicorn(item));
│ ^^^^^
2 │
ℹ Negating the whole call hides the condition that is actually checked. Calling some() with a negated callback function gives the same result and is easier to read.
ℹ Remove the ! before the call, use some() instead, and negate the value that the callback function returns.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use some() with a negated callback function.
1 │ - const·isNotEveryUnicorn·=·!array.every((item)·=>·isUnicorn(item));
1 │ + const·isNotEveryUnicorn·=·array.some((item)·=>·!isUnicorn(item));
2 2 │
if (!array.some((item) => !isUnicorn(item))) {}code-block.js:1:12 lint/nursery/noNegatedArrayPredicate FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The result of some() is negated with !.
> 1 │ if (!array.some((item) => !isUnicorn(item))) {}
│ ^^^^
2 │
ℹ Negating the whole call hides the condition that is actually checked. Calling every() with a negated callback function gives the same result and is easier to read.
ℹ Remove the ! before the call, use every() instead, and negate the value that the callback function returns.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use every() with a negated callback function.
1 │ - if·(!array.some((item)·=>·!isUnicorn(item)))·{}
1 │ + if·(array.every((item)·=>·isUnicorn(item)))·{}
2 2 │
const isMissing = array.every((item) => !isUnicorn(item));
const isNotEveryUnicorn = array.some((item) => !isUnicorn(item));
if (array.every((item) => isUnicorn(item))) {}
// The callback function isn't written inline, so there's nothing to negate.if (!array.every(Boolean)) {}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.