eslint/no-lonely-if Pedantic
What it does
Disallow if
statements as the only statement in else
blocks
Why is this bad?
When an if
statement is the only statement in an else
block, it is often clearer to use an else if
instead.
Examples
Examples of incorrect code for this rule:
js
if (condition) {
// ...
} else {
if (anotherCondition) {
// ...
}
}
js
if (condition) {
// ...
} else {
if (anotherCondition) {
// ...
} else {
// ...
}
}
Examples of correct code for this rule:
js
if (condition) {
// ...
} else if (anotherCondition) {
// ...
}
js
if (condition) {
// ...
} else if (anotherCondition) {
// ...
} else {
// ...
}
js
if (condition) {
// ...
} else {
if (anotherCondition) {
// ...
}
doSomething();
}
How to use
To enable this rule in the CLI or using the config file, you can use:
bash
oxlint --deny no-lonely-if
json
{
"rules": {
"no-lonely-if": "error"
}
}