no-dupe-else-if
Disallow an else if with a condition that an earlier branch already covers
error by default
In an if ... else if chain, a branch only runs if every condition before it was false. If a condition repeats an earlier one, its branch can never run. It's usually a copied line where someone forgot to change the condition.
The rule also understands || and &&. After if (a || b), both else if (a) and else if (b && c) are dead. The order of the operands doesn't matter.
This rule is on by default.
Configure#
On by default as an error. To turn it off:
bunfig.toml
[lint.rules]
"no-dupe-else-if" = "off" # or "warn"Examples#
example.js1 problem
if (status === "active") {
enable();
} else if (status === "paused") {
pause();
} else if (status === "active"This branch can never execute. Its condition is a duplicate or covered by previous conditions in the if-else-if chain.) {
archive();
}example.jsNo problems
if (status === "active") {
enable();
} else if (status === "paused") {
pause();
} else if (status === "archived") {
archive();
}A condition that's narrower than an earlier one is reported too:
example.js2 problems
if (user.isAdmin || user.isOwner) {
showAll();
} else if (user.isOwner && user.isVerifiedThis branch can never execute. Its condition is a duplicate or covered by previous conditions in the if-else-if chain.) {
showMost();
} else if (user.isAdminThis branch can never execute. Its condition is a duplicate or covered by previous conditions in the if-else-if chain.) {
showSome();
}Put the narrower condition first:
example.jsNo problems
if (user.isOwner && user.isVerified) {
showMost();
} else if (user.isAdmin || user.isOwner) {
showAll();
}