Skip to content

Latest commit

 

History

History
92 lines (74 loc) · 1.38 KB

no-negated-condition.md

File metadata and controls

92 lines (74 loc) · 1.38 KB

Disallow negated conditions

✅ This rule is enabled in the recommended config.

🔧 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.

Fail

if (!a) {
	doSomethingC();
} else {
	doSomethingB();
}
if (a !== b) {
	doSomethingC();
} else {
	doSomethingB();
}
!a ? c : b
if (a != b) {
	doSomethingC();
} else {
	doSomethingB();
}

Pass

if (a) {
	doSomethingB();
} else {
	doSomethingC();
}
if (a === b) {
	doSomethingB();
} else {
	doSomethingC();
}
a ? b : c
if (a == b) {
	doSomethingB();
} else {
	doSomethingC();
}
if (!a) {
	doSomething();
}
if (!a) {
	doSomething();
} else if (b) {
	doSomethingElse();
}
if (a != b) {
	doSomething();
}