no-negated-condition
Disallow negated conditions in if/else and in ternaries
When both branches are there, a negated condition makes the else a double negative: "if not logged in .. otherwise ..". Swapping the branches removes the negation and says the same thing.
The rule looks at conditions of the form !x, a != b and a !== b. An if without else is fine, and so is one followed by else if.
With an oxlint config, --fix removes the negation and swaps the branches.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"no-negated-condition" = "error" # or "warn"Examples#
example.js1 problem
if (!user) {Unexpected negated condition.
redirect("/login");
} else {
render(user);
}example.js1 problem
const label = count !== 1 ? "items" : "item"Unexpected negated condition.;example.jsNo problems
if (user) {
render(user);
} else {
redirect("/login");
}
const label = count === 1 ? "item" : "items";example.jsNo problems
if (!user) {
redirect("/login");
}
if (!user) {
redirect("/login");
} else if (user.isAdmin) {
renderAdmin(user);
}