useCollapsedElseIf
Este conteúdo não está disponível em sua língua ainda.
Diagnostic Category: lint/style/useCollapsedElseIf
Since: v1.1.0
Sources:
- Same as:
no-lonely-if
- Same as:
collapsible_else_if
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.
Examples
Section titled ExamplesInvalid
Section titled Invalidcode-block.js:3:9 lint/style/useCollapsedElseIf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This if statement can be collapsed into an else if statement.
1 │ if (condition) {
2 │ // …
> 3 │ } else {
│
> 4 │ if (anotherCondition) {
> 5 │ // …
> 6 │ }
│ ^
7 │ }
8 │
ℹ Safe fix: Use collapsed else if instead.
1 1 │ if (condition) {
2 2 │ // …
3 │ - }·else·{
4 │ - ····if·(anotherCondition)·{
3 │ + }·else·if·(anotherCondition)·{
5 4 │ // …
6 │ - ····}
7 │ - }
5 │ + ····}
8 6 │
code-block.js:3:9 lint/style/useCollapsedElseIf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This if statement can be collapsed into an else if statement.
1 │ if (condition) {
2 │ // …
> 3 │ } else {
│
> 4 │ if (anotherCondition) {
> 5 │ // …
> 6 │ } else {
> 7 │ // …
> 8 │ }
│ ^
9 │ }
10 │
ℹ Safe fix: Use collapsed else if instead.
1 1 │ if (condition) {
2 2 │ // …
3 │ - }·else·{
4 │ - ····if·(anotherCondition)·{
3 │ + }·else·if·(anotherCondition)·{
5 4 │ // …
6 5 │ } else {
7 6 │ // …
8 │ - ····}
9 │ - }
7 │ + ····}
10 8 │
code-block.js:3:9 lint/style/useCollapsedElseIf ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This if statement can be collapsed into an else if statement.
1 │ if (condition) {
2 │ // …
> 3 │ } else {
│
> 4 │ // Comment
> 5 │ if (anotherCondition) {
> 6 │ // …
> 7 │ }
│ ^
8 │ }
9 │