Skip to content

Latest commit

Β 

History

History
95 lines (77 loc) Β· 1.49 KB

File metadata and controls

95 lines (77 loc) Β· 1.49 KB

no-negated-condition

πŸ“ 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.

Examples

// ❌
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();
}