useCollapsedIf
Summary
Section titled “Summary”- Rule available since:
v1.9.4
- Diagnostic Category:
lint/style/useCollapsedIf
- This rule has a safe fix.
- The default severity of this rule is information.
- Sources:
- Same as
unicorn/no-lonely-if
- Same as
collapsible_if
- Same as
Description
Section titled “Description”Enforce using single if
instead of nested if
clauses.
If an if (b)
statement is the only statement in an if (a)
block, it is often clearer to use an if (a && b)
form.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”if (condition) { if (anotherCondition) { // ... }}
code-block.js:2:5 lint/style/useCollapsedIf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This if statement can be collapsed into another if statement.
1 │ if (condition) {
> 2 │ if (anotherCondition) {
│ ^^^^^^^^^^^^^^^^^^^^^^^
> 3 │ // …
> 4 │ }
│ ^
5 │ }
6 │
ℹ Safe fix: Use collapsed if instead.
1 │ - if·(condition)·{
2 │ - ····if·(anotherCondition)·{
1 │ + if·(condition·&&·anotherCondition)·{
3 2 │ // …
4 │ - ····}
5 │ - }
3 │ + ····}
6 4 │
if (condition) { // Comment if (anotherCondition) { // ... }}
code-block.js:3:5 lint/style/useCollapsedIf ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This if statement can be collapsed into another if statement.
1 │ if (condition) {
2 │ // Comment
> 3 │ if (anotherCondition) {
│ ^^^^^^^^^^^^^^^^^^^^^^^
> 4 │ // …
> 5 │ }
│ ^
6 │ }
7 │
if (condition && anotherCondition) { // ...}
if (condition) { if (anotherCondition) { // ... } doSomething();}
if (condition) { if (anotherCondition) { // ... } else { // ... }}
How to configure
Section titled “How to configure”{ "linter": { "rules": { "style": { "useCollapsedIf": "error" } } }}