π Disallow negated conditions.
πΌ This rule is enabled in the following configs: β
recommended, βοΈ unopinionated.
π§ This rule is automatically fixable by the --fix CLI option.
Negated conditions are more difficult to understand. Code can be made more readable by inverting the condition.
This is an improved version of the no-negated-condition ESLint rule that makes it automatically fixable. ESLint did not want to make it fixable.
// β
if (!a) {
doSomethingC();
} else {
doSomethingB();
}
// β
if (a) {
doSomethingB();
} else {
doSomethingC();
}// β
if (a !== b) {
doSomethingC();
} else {
doSomethingB();
}
// β
if (a === b) {
doSomethingB();
} else {
doSomethingC();
}// β
!a ? c : b
// β
a ? b : c// β
if (a != b) {
doSomethingC();
} else {
doSomethingB();
}
// β
if (a == b) {
doSomethingB();
} else {
doSomethingC();
}// β
if (!a) {
doSomething();
}// β
if (!a) {
doSomething();
} else if (b) {
doSomethingElse();
}// β
if (a != b) {
doSomething();
}