noConstantBinaryExpression
Summary
Section titled Summary- Diagnostic Category:
lint/nursery/noConstantBinaryExpression
- This rule doesn’t have a fix.
- The default severity of this rule is information.
- Sources:
- Same as
no-constant-binary-expression
- Same as
Description
Section titled DescriptionDisallow expressions where the operation doesn’t affect the value
Comparisons which will always evaluate to true or false and logical expressions
(||
, &&
, ??
) which either always short-circuit or never short-circuit are both likely
indications of programmer error.
Examples
Section titled ExamplesInvalid
Section titled Invalidconst value1 = +x == null;
code-block.js:1:16 lint/nursery/noConstantBinaryExpression ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This binary expression leads to a constant result.
> 1 │ const value1 = +x == null;
│ ^^^^^^^^^^
2 │
const value2 = condition ? x : {} || DEFAULT;
code-block.js:1:32 lint/nursery/noConstantBinaryExpression ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This logical expression can be simplified.
> 1 │ const value2 = condition ? x : {} || DEFAULT;
│ ^^^^^^^^^^^^^
2 │
ℹ This operand always evaluates to the same truthiness.
> 1 │ const value2 = condition ? x : {} || DEFAULT;
│ ^^
2 │
const value3 = !foo == null;
code-block.js:1:16 lint/nursery/noConstantBinaryExpression ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This binary expression leads to a constant result.
> 1 │ const value3 = !foo == null;
│ ^^^^^^^^^^^^
2 │
const value4 = new Boolean(foo) === true;
code-block.js:1:16 lint/nursery/noConstantBinaryExpression ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This binary expression leads to a constant result.
> 1 │ const value4 = new Boolean(foo) === true;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
const objIsEmpty = someObj === {};
code-block.js:1:20 lint/nursery/noConstantBinaryExpression ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected comparison to newly constructed object.
> 1 │ const objIsEmpty = someObj === {};
│ ^^^^^^^^^^^^^^
2 │
ℹ This expression always constructs a new object.
> 1 │ const objIsEmpty = someObj === {};
│ ^^
2 │
const arrIsEmpty = someArr === [];
code-block.js:1:20 lint/nursery/noConstantBinaryExpression ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected comparison to newly constructed object.
> 1 │ const arrIsEmpty = someArr === [];
│ ^^^^^^^^^^^^^^
2 │
ℹ This expression always constructs a new object.
> 1 │ const arrIsEmpty = someArr === [];
│ ^^
2 │
const shortCircuit1 = condition1 && false && condition2;
code-block.js:1:23 lint/nursery/noConstantBinaryExpression ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This logical expression can be simplified.
> 1 │ const shortCircuit1 = condition1 && false && condition2;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ This operand always evaluates to the same truthiness.
> 1 │ const shortCircuit1 = condition1 && false && condition2;
│ ^^^^^^^^^^^^^^^^^^^
2 │
const shortCircuit2 = condition1 || true || condition2;
code-block.js:1:23 lint/nursery/noConstantBinaryExpression ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This logical expression can be simplified.
> 1 │ const shortCircuit2 = condition1 || true || condition2;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ This operand always evaluates to the same truthiness.
> 1 │ const shortCircuit2 = condition1 || true || condition2;
│ ^^^^^^^^^^^^^^^^^^
2 │
const shortCircuit3 = condition1 ?? "non-nullish" ?? condition2;
code-block.js:1:23 lint/nursery/noConstantBinaryExpression ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This logical expression can be simplified.
> 1 │ const shortCircuit3 = condition1 ?? “non-nullish” ?? condition2;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ This operand always evaluates to the same nullishness.
> 1 │ const shortCircuit3 = condition1 ?? “non-nullish” ?? condition2;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
Valid
Section titled Validconst value1 = x == null;
const value2 = (condition ? x : {}) || DEFAULT;
const value3 = !(foo == null);
const value4 = Boolean(foo) === true;
const objIsEmpty = Object.keys(someObj).length === 0;
const arrIsEmpty = someArr.length === 0;
How to configure
Section titled How to configure{ "linter": { "rules": { "nursery": { "noConstantBinaryExpression": "error" } } }}