noNegationElse
Diagnostic Category: lint/style/noNegationElse
Since: v1.0.0
Sources:
- Same as:
no-negated-condition
- Same as:
if_not_else
Description
Section titled DescriptionDisallow negation in the condition of an if
statement if it has an else
clause.
Examples
Section titled ExamplesInvalid
Section titled Invalidif (!cond) { f();} else { g();}
code-block.js:1:1 lint/style/noNegationElse FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Invert blocks when performing a negation test.
> 1 │ if (!cond) { f();} else { g();}
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Safe fix: Invert the condition and the blocks.
1 │ - if·(!cond)·{·f();}·else·{·g();}
1 │ + if·(cond)·{·g();}·else·{·f();}
2 2 │
!cond ? 0 : 1
code-block.js:1:1 lint/style/noNegationElse FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Invert blocks when performing a negation test.
> 1 │ !cond ? 0 : 1
│ ^^^^^^^^^^^^^
2 │
ℹ Safe fix: Invert the condition and the blocks.
1 │ - !cond·?·0·:·1
1 │ + cond·?·1·:·0
2 2 │
Valid
Section titled Validif (!cond) { f(); }
cond ? 1 : 0
if (!cond) { f(); }
if (!!val) { f(); } else { g(); }
How to configure
Section titled How to configure{ "linter": { "rules": { "style": { "noNegationElse": "error" } } }}