noDoubleComparison (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.0 - Diagnostic Category:
lint/nursery/noDoubleComparison - This rule has an unsafe fix.
- The default severity of this rule is warning.
- Sources:
- Same as
unicorn/no-double-comparison
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noDoubleComparison": "error" } } }}Description
Section titled “Description”Disallow two comparisons of the same values that can be combined into one.
When || or && joins two comparisons of the same two values, a single comparison
often means the same thing. For example, x === y || x < y is the same as x <= y.
A single comparison is shorter, easier to read, and evaluates each value only once.
The values can appear in either order: x === y || y < x is the same as x >= y.
The rule reports these pairs:
| Code | Combined |
|---|---|
x === y || x < y | x <= y |
x === y || x > y | x >= y |
x < y || x > y | x !== y |
x <= y && x >= y | x === y |
x <= y && x !== y | x < y |
x >= y && x !== y | x > y |
The rule only checks comparisons of variables, properties, this, and literal values
such as numbers and strings. It ignores comparisons that use == or !=, call a
function, or use optional chaining (?.).
The fix is unsafe because the combined comparison can give a different result when the
two values are of different types, or when a value is undefined or NaN. For example,
undefined === undefined || undefined < undefined is true, but
undefined <= undefined is false. With x = "5", x === 5 || x < 5 is false, but
x <= 5 is true.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”x === y || x < y;code-block.js:1:1 lint/nursery/noDoubleComparison FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ These two comparisons can be combined into x <= y.
> 1 │ x === y || x < y;
│ ^^^^^^^^^^^^^^^^
2 │
ℹ Both comparisons check the same two values, so a single comparison usually means the same thing with less code.
ℹ 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: Combine them into a single comparison.
1 │ - x·===·y·||·x·<·y;
1 │ + x·<=·y;
2 2 │
x <= y && x >= y;code-block.js:1:1 lint/nursery/noDoubleComparison FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ These two comparisons can be combined into x === y.
> 1 │ x <= y && x >= y;
│ ^^^^^^^^^^^^^^^^
2 │
ℹ Both comparisons check the same two values, so a single comparison usually means the same thing with less code.
ℹ 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: Combine them into a single comparison.
1 │ - x·<=·y·&&·x·>=·y;
1 │ + x·===·y;
2 2 │
x <= y && x !== y;code-block.js:1:1 lint/nursery/noDoubleComparison FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ These two comparisons can be combined into x < y.
> 1 │ x <= y && x !== y;
│ ^^^^^^^^^^^^^^^^^
2 │
ℹ Both comparisons check the same two values, so a single comparison usually means the same thing with less code.
ℹ 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: Combine them into a single comparison.
1 │ x·<=·y·&&·x·!==·y;
│ ------------
x === y || y < x;code-block.js:1:1 lint/nursery/noDoubleComparison FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ These two comparisons can be combined into x >= y.
> 1 │ x === y || y < x;
│ ^^^^^^^^^^^^^^^^
2 │
ℹ Both comparisons check the same two values, so a single comparison usually means the same thing with less code.
ℹ 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: Combine them into a single comparison.
1 │ - x·===·y·||·y·<·x;
1 │ + x·>=·y;
2 2 │
x <= y;x >= y;x === y;x < y;x <= y || x < y;x === y || x < z;Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.