Skip to main content

noNegationElse (since v0.7.0)

This rule is recommended by Rome.

Disallow negation in the condition of an if statement if it has an else clause

Examples

Invalid

if (!true) {consequent;} else {alternate;}
error[js/noNegationElse]: Invert blocks when performing a negation test.
   js/noNegationElse.js:1:1
  
1  if (!true) {consequent;} else {alternate;}
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Suggested fix: Exchange alternate and consequent of the node
    | @@ -1 +1 @@
0   | - if (!true) {consequent;} else {alternate;}
  0 | + if (true) {alternate;} else {consequent;}

!true ? consequent : alternate
error[js/noNegationElse]: Invert blocks when performing a negation test.
   js/noNegationElse.js:1:1
  
1  !true ? consequent : alternate
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Suggested fix: Exchange alternate and consequent of the node
    | @@ -1 +1 @@
0   | - !true ? consequent : alternate
  0 | + true ? alternate : consequent

Valid

if (!true) {consequent;}
true ? consequent : alternate