Skip to content

noNegatedArrayPredicate (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noNegatedArrayPredicate": "error"
}
}
}
}

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.

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)) {}