noConstantBinaryExpressions
Este conteúdo não está disponível em sua língua ainda.
Summary
Section titled “Summary”- Rule available since: 
v2.0.0 - Diagnostic Category: 
lint/suspicious/noConstantBinaryExpressions - This rule doesn’t have a fix.
 - The default severity of this rule is information.
 - Sources:
- Same as 
no-constant-binary-expression 
 - Same as 
 
How to configure
Section titled “How to configure”{  "linter": {    "rules": {      "suspicious": {        "noConstantBinaryExpressions": "error"      }    }  }}Description
Section titled “Description”Disallow 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 “Examples”Invalid
Section titled “Invalid”const value1 = +x == null;code-block.js:1:16 lint/suspicious/noConstantBinaryExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ℹ 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/suspicious/noConstantBinaryExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ℹ 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/suspicious/noConstantBinaryExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ℹ 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/suspicious/noConstantBinaryExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ℹ 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/suspicious/noConstantBinaryExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ℹ 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/suspicious/noConstantBinaryExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ℹ 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/suspicious/noConstantBinaryExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ℹ 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/suspicious/noConstantBinaryExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ℹ 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/suspicious/noConstantBinaryExpressions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  ℹ 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 │ 
  
const 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;Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.