src/content/docs/linter/rules/use-collapsed-else-if.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.1.0` - Diagnostic Category: [`lint/style/useCollapsedElseIf`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule has a [**safe**](/linter/#safe-fixes) fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`no-lonely-if`](https://eslint.org/docs/latest/rules/no-lonely-if) - Same as [`collapsible_else_if`](https://rust-lang.github.io/rust-clippy/master/#collapsible_else_if){
"linter": {
"rules": {
"style": {
"useCollapsedElseIf": "error"
}
}
}
}
Enforce using else if instead of nested if in else clauses.
If an if statement is the only statement in the else block, it is often clearer to use an else if form.
if (condition) {
// ...
} else {
if (anotherCondition) {
// ...
}
}
if (condition) {
// ...
} else {
if (anotherCondition) {
// ...
} else {
// ...
}
}
if (condition) {
// ...
} else {
// Comment
if (anotherCondition) {
// ...
}
}
if (condition) {
// ...
} else if (anotherCondition) {
// ...
}
if (condition) {
// ...
} else if (anotherCondition) {
// ...
} else {
// ...
}
if (condition) {
// ...
} else {
if (anotherCondition) {
// ...
}
doSomething();
}