-
-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <[email protected]>
- Loading branch information
1 parent
3521a0a
commit 10e7a0c
Showing
10 changed files
with
2,180 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
# Prefer `switch` over multiple `else-if` | ||
|
||
A switch statement is easier to read than multiple if statements with simple equality comparisons. | ||
|
||
This rule is partly fixable. | ||
|
||
## Fail | ||
|
||
```js | ||
if (foo === 1) { | ||
// 1 | ||
} else if (foo === 2) { | ||
// 2 | ||
} else if (foo === 3) { | ||
// 3 | ||
} else { | ||
// default | ||
} | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
if (foo === 1) { | ||
// 1 | ||
} else if (foo === 2) { | ||
// 2 | ||
} | ||
``` | ||
|
||
```js | ||
switch (foo) { | ||
case 1: { | ||
// 1 | ||
break; | ||
} | ||
case 2: { | ||
// 2 | ||
break; | ||
} | ||
case 3: { | ||
// 3 | ||
break; | ||
} | ||
default: { | ||
// default | ||
} | ||
} | ||
``` | ||
|
||
### `options` | ||
|
||
Type: `object` | ||
|
||
#### `minimumCases` | ||
|
||
Type: `integer`\ | ||
Minimum: `2`\ | ||
Default: `3` | ||
|
||
The minimum number of cases before reporting. | ||
|
||
The `default` branch doesn't count. Multiple comparisons on the same `if` block is considered one case. | ||
|
||
Examples: | ||
|
||
```js | ||
// eslint unicorn/prefer-switch: ["error", {"minimumCases": 4}] | ||
if (foo === 1) {} | ||
else (foo === 2) {} | ||
else (foo === 3) {} | ||
|
||
// Passes, only 3 cases. | ||
``` | ||
|
||
```js | ||
// eslint unicorn/prefer-switch: ["error", {"minimumCases": 4}] | ||
if (foo === 1) {} | ||
else (foo === 2) {} | ||
else (foo === 3) {} | ||
else {} | ||
|
||
// Passes, only 3 cases. | ||
``` | ||
|
||
```js | ||
// eslint unicorn/prefer-switch: ["error", {"minimumCases": 4}] | ||
if (foo === 1) {} | ||
else if (foo === 2 || foo === 3) {} | ||
else if (foo === 4) {} | ||
|
||
// Passes, only 3 cases. | ||
``` | ||
|
||
```js | ||
// eslint unicorn/prefer-switch: ["error", {"minimumCases": 2}] | ||
if (foo === 1) {} | ||
else if (foo === 2) {} | ||
|
||
// Fails | ||
``` | ||
|
||
#### `emptyDefaultCase` | ||
|
||
Type: `string`\ | ||
Default: `'no-default-comment'` | ||
|
||
To avoid conflict with the [`default-case`](https://eslint.org/docs/rules/default-case) rule, choose the fix style you prefer: | ||
|
||
- `'no-default-comment'` (default) | ||
Insert `// No default` comment after last case. | ||
- `'do-nothing-comment'` | ||
Insert `default` case and add `// Do nothing` comment. | ||
- `'no-default-case'` | ||
Don't insert default case or comment. | ||
|
||
```js | ||
if (foo === 1) {} | ||
else (foo === 2) {} | ||
else (foo === 3) {} | ||
``` | ||
|
||
Fixed | ||
|
||
```js | ||
/* eslint unicorn/prefer-switch: ["error", { "emptyDefaultCase": "no-default-comment" }] */ | ||
switch (foo) { | ||
case 1: { | ||
break; | ||
} | ||
case 2: { | ||
break; | ||
} | ||
case 3: { | ||
break; | ||
} | ||
// No default | ||
} | ||
``` | ||
|
||
```js | ||
/* eslint unicorn/prefer-switch: ["error", { "emptyDefaultCase": "do-nothing-comment" }] */ | ||
switch (foo) { | ||
case 1: { | ||
break; | ||
} | ||
case 2: { | ||
break; | ||
} | ||
case 3: { | ||
break; | ||
} | ||
default: | ||
// Do nothing | ||
} | ||
``` | ||
|
||
```js | ||
/* eslint unicorn/prefer-switch: ["error", { "emptyDefaultCase": "no-default-case" }] */ | ||
switch (foo) { | ||
case 1: { | ||
break; | ||
} | ||
case 2: { | ||
break; | ||
} | ||
case 3: { | ||
break; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.