Skip to main content

useSimplifiedLogicExpression (since v0.7.0)

This rule is recommended by Rome.

Discard redundant terms from logical expressions.

Examples

Invalid

const boolExp = true;
const r = true && boolExp;
error[js/useSimplifiedLogicExpression]: Logical expression contains unnecessary complexity.
   js/useSimplifiedLogicExpression.js:2:11
  
2  const r = true && boolExp;
             ^^^^^^^^^^^^^^^

Suggested fix: Discard redundant terms from the logical expression.
    | @@ -1,2 +1,2 @@
0 0 |   const boolExp = true;
1   | - const r = true && boolExp;
  1 | + const r = boolExp;

const boolExp2 = true;
const r2 = boolExp || true;
error[js/useSimplifiedLogicExpression]: Logical expression contains unnecessary complexity.
   js/useSimplifiedLogicExpression.js:2:12
  
2  const r2 = boolExp || true;
              ^^^^^^^^^^^^^^^

Suggested fix: Discard redundant terms from the logical expression.
    | @@ -1,2 +1,2 @@
0 0 |   const boolExp2 = true;
1   | - const r2 = boolExp || true;
  1 | + const r2 = true;

const nonNullExp = 123;
const r3 = null ?? nonNullExp;
error[js/useSimplifiedLogicExpression]: Logical expression contains unnecessary complexity.
   js/useSimplifiedLogicExpression.js:2:12
  
2  const r3 = null ?? nonNullExp;
              ^^^^^^^^^^^^^^^^^^

Suggested fix: Discard redundant terms from the logical expression.
    | @@ -1,2 +1,2 @@
0 0 |   const nonNullExp = 123;
1   | - const r3 = null ?? nonNullExp;
  1 | + const r3 = nonNullExp;

const boolExpr1 = true;
const boolExpr2 = false;
const r4 = !boolExpr1 || !boolExpr2;
error[js/useSimplifiedLogicExpression]: Logical expression contains unnecessary complexity.
   js/useSimplifiedLogicExpression.js:3:12
  
3  const r4 = !boolExpr1 || !boolExpr2;
              ^^^^^^^^^^^^^^^^^^^^^^^^

Suggested fix: Reduce the complexity of the logical expression.
    | @@ -1,3 +1,3 @@
0 0 |   const boolExpr1 = true;
1 1 |   const boolExpr2 = false;
2   | - const r4 = !boolExpr1 || !boolExpr2;
  2 | + const r4 = !(boolExpr1 && boolExpr2);

Valid

const boolExpr3 = true;
const boolExpr4 = false;
const r5 = !(boolExpr1 && boolExpr2);
const boolExpr5 = true;
const boolExpr6 = false;
const r6 = !!boolExpr1 || !!boolExpr2;