Skip to main content

noNegationElse

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

Examples

Invalid

if (!true) {consequent;} else {alternate;}
warning[noNegationElse]: Invert blocks when performing a negation test.
   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
warning[noNegationElse]: Invert blocks when performing a negation test.
   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