Linter · Rules
no-dupe-else-if
Disallow an else if with a condition that an earlier branch already covers
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
Reported:
if (status === "active") {
enable();
} else if (status === "paused") {
pause();
} else if (status === "active") {
archive();
}
Not reported:
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:
if (user.isAdmin || user.isOwner) {
showAll();
} else if (user.isOwner && user.isVerified) {
showMost();
} else if (user.isAdmin) {
showSome();
}
Put the narrower condition first:
if (user.isOwner && user.isVerified) {
showMost();
} else if (user.isAdmin || user.isOwner) {
showAll();
}