]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/src/rules/default-case-last.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / default-case-last.md
CommitLineData
8f9d1d4d
DC
1---
2title: default-case-last
8f9d1d4d
DC
3rule_type: suggestion
4related_rules:
5- default-case
6further_reading:
7- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
8---
9
eb39fafa
DC
10
11A `switch` statement can optionally have a `default` clause.
12
13If 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
15Even 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
17If 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
21This rule enforces `default` clauses in `switch` statements to be last.
22
23It applies only to `switch` statements that already have a `default` clause.
24
8f9d1d4d 25This 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
27Examples of **incorrect** code for this rule:
28
8f9d1d4d
DC
29:::incorrect
30
eb39fafa
DC
31```js
32/*eslint default-case-last: "error"*/
33
34switch (foo) {
35 default:
36 bar();
37 break;
38 case "a":
39 baz();
40 break;
41}
42
43switch (foo) {
44 case 1:
45 bar();
46 break;
47 default:
48 baz();
49 break;
50 case 2:
51 quux();
52 break;
53}
54
55switch (foo) {
56 case "x":
57 bar();
58 break;
59 default:
60 case "y":
61 baz();
62 break;
63}
64
65switch (foo) {
66 default:
67 break;
68 case -1:
69 bar();
70 break;
71}
72
73switch (foo) {
74 default:
75 doSomethingIfNotZero();
76 case 0:
77 doSomethingAnyway();
78}
79```
80
8f9d1d4d
DC
81:::
82
eb39fafa
DC
83Examples of **correct** code for this rule:
84
8f9d1d4d
DC
85:::correct
86
eb39fafa
DC
87```js
88/*eslint default-case-last: "error"*/
89
90switch (foo) {
91 case "a":
92 baz();
93 break;
94 default:
95 bar();
96 break;
97}
98
99switch (foo) {
100 case 1:
101 bar();
102 break;
103 case 2:
104 quux();
105 break;
106 default:
107 baz();
108 break;
109}
110
111switch (foo) {
112 case "x":
113 bar();
114 break;
115 case "y":
116 default:
117 baz();
118 break;
119}
120
121switch (foo) {
122 case -1:
123 bar();
124 break;
125}
126
127if (foo !== 0) {
128 doSomethingIfNotZero();
129}
130doSomethingAnyway();
131```
132
8f9d1d4d 133:::