]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: default-case-last | |
8f9d1d4d DC |
3 | rule_type: suggestion |
4 | related_rules: | |
5 | - default-case | |
6 | further_reading: | |
7 | - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch | |
8 | --- | |
9 | ||
eb39fafa DC |
10 | |
11 | A `switch` statement can optionally have a `default` clause. | |
12 | ||
13 | If present, it's usually the last clause, but it doesn't need to be. It is also allowed to put the `default` clause before all `case` clauses, or anywhere between. The behavior is mostly the same as if it was the last clause. The `default` block will be still executed only if there is no match in the `case` clauses (including those defined after the `default`), but there is also the ability to "fall through" from the `default` clause to the following clause in the list. However, such flow is not common and it would be confusing to the readers. | |
14 | ||
15 | Even if there is no "fall through" logic, it's still unexpected to see the `default` clause before or between the `case` clauses. By convention, it is expected to be the last clause. | |
16 | ||
17 | If a `switch` statement should have a `default` clause, it's considered a best practice to define it as the last clause. | |
18 | ||
19 | ## Rule Details | |
20 | ||
21 | This rule enforces `default` clauses in `switch` statements to be last. | |
22 | ||
23 | It applies only to `switch` statements that already have a `default` clause. | |
24 | ||
8f9d1d4d | 25 | This rule does not enforce the existence of `default` clauses. See [default-case](default-case) if you also want to enforce the existence of `default` clauses in `switch` statements. |
eb39fafa DC |
26 | |
27 | Examples of **incorrect** code for this rule: | |
28 | ||
8f9d1d4d DC |
29 | :::incorrect |
30 | ||
eb39fafa DC |
31 | ```js |
32 | /*eslint default-case-last: "error"*/ | |
33 | ||
34 | switch (foo) { | |
35 | default: | |
36 | bar(); | |
37 | break; | |
38 | case "a": | |
39 | baz(); | |
40 | break; | |
41 | } | |
42 | ||
43 | switch (foo) { | |
44 | case 1: | |
45 | bar(); | |
46 | break; | |
47 | default: | |
48 | baz(); | |
49 | break; | |
50 | case 2: | |
51 | quux(); | |
52 | break; | |
53 | } | |
54 | ||
55 | switch (foo) { | |
56 | case "x": | |
57 | bar(); | |
58 | break; | |
59 | default: | |
60 | case "y": | |
61 | baz(); | |
62 | break; | |
63 | } | |
64 | ||
65 | switch (foo) { | |
66 | default: | |
67 | break; | |
68 | case -1: | |
69 | bar(); | |
70 | break; | |
71 | } | |
72 | ||
73 | switch (foo) { | |
74 | default: | |
75 | doSomethingIfNotZero(); | |
76 | case 0: | |
77 | doSomethingAnyway(); | |
78 | } | |
79 | ``` | |
80 | ||
8f9d1d4d DC |
81 | ::: |
82 | ||
eb39fafa DC |
83 | Examples of **correct** code for this rule: |
84 | ||
8f9d1d4d DC |
85 | :::correct |
86 | ||
eb39fafa DC |
87 | ```js |
88 | /*eslint default-case-last: "error"*/ | |
89 | ||
90 | switch (foo) { | |
91 | case "a": | |
92 | baz(); | |
93 | break; | |
94 | default: | |
95 | bar(); | |
96 | break; | |
97 | } | |
98 | ||
99 | switch (foo) { | |
100 | case 1: | |
101 | bar(); | |
102 | break; | |
103 | case 2: | |
104 | quux(); | |
105 | break; | |
106 | default: | |
107 | baz(); | |
108 | break; | |
109 | } | |
110 | ||
111 | switch (foo) { | |
112 | case "x": | |
113 | bar(); | |
114 | break; | |
115 | case "y": | |
116 | default: | |
117 | baz(); | |
118 | break; | |
119 | } | |
120 | ||
121 | switch (foo) { | |
122 | case -1: | |
123 | bar(); | |
124 | break; | |
125 | } | |
126 | ||
127 | if (foo !== 0) { | |
128 | doSomethingIfNotZero(); | |
129 | } | |
130 | doSomethingAnyway(); | |
131 | ``` | |
132 | ||
8f9d1d4d | 133 | ::: |