]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # Disallow Labeled Statements (no-labels) |
2 | ||
3 | Labeled statements in JavaScript are used in conjunction with `break` and `continue` to control flow around multiple loops. For example: | |
4 | ||
5 | ```js | |
6 | outer: | |
7 | while (true) { | |
8 | ||
9 | while (true) { | |
10 | break outer; | |
11 | } | |
12 | } | |
13 | ``` | |
14 | ||
15 | The `break outer` statement ensures that this code will not result in an infinite loop because control is returned to the next statement after the `outer` label was applied. If this statement was changed to be just `break`, control would flow back to the outer `while` statement and an infinite loop would result. | |
16 | ||
17 | While convenient in some cases, labels tend to be used only rarely and are frowned upon by some as a remedial form of flow control that is more error prone and harder to understand. | |
18 | ||
19 | ## Rule Details | |
20 | ||
21 | This rule aims to eliminate the use of labeled statements in JavaScript. It will warn whenever a labeled statement is encountered and whenever `break` or `continue` are used with a label. | |
22 | ||
23 | Examples of **incorrect** code for this rule: | |
24 | ||
25 | ```js | |
26 | /*eslint no-labels: "error"*/ | |
27 | ||
28 | label: | |
29 | while(true) { | |
30 | // ... | |
31 | } | |
32 | ||
33 | label: | |
34 | while(true) { | |
35 | break label; | |
36 | } | |
37 | ||
38 | label: | |
39 | while(true) { | |
40 | continue label; | |
41 | } | |
42 | ||
43 | label: | |
44 | switch (a) { | |
45 | case 0: | |
46 | break label; | |
47 | } | |
48 | ||
49 | label: | |
50 | { | |
51 | break label; | |
52 | } | |
53 | ||
54 | label: | |
55 | if (a) { | |
56 | break label; | |
57 | } | |
58 | ``` | |
59 | ||
60 | Examples of **correct** code for this rule: | |
61 | ||
62 | ```js | |
63 | /*eslint no-labels: "error"*/ | |
64 | ||
65 | var f = { | |
66 | label: "foo" | |
67 | }; | |
68 | ||
69 | while (true) { | |
70 | break; | |
71 | } | |
72 | ||
73 | while (true) { | |
74 | continue; | |
75 | } | |
76 | ``` | |
77 | ||
78 | ## Options | |
79 | ||
80 | The options allow labels with loop or switch statements: | |
81 | ||
82 | * `"allowLoop"` (`boolean`, default is `false`) - If this option was set `true`, this rule ignores labels which are sticking to loop statements. | |
83 | * `"allowSwitch"` (`boolean`, default is `false`) - If this option was set `true`, this rule ignores labels which are sticking to switch statements. | |
84 | ||
85 | Actually labeled statements in JavaScript can be used with other than loop and switch statements. | |
86 | However, this way is ultra rare, not well-known, so this would be confusing developers. | |
87 | ||
88 | ### allowLoop | |
89 | ||
90 | Examples of **correct** code for the `{ "allowLoop": true }` option: | |
91 | ||
92 | ```js | |
93 | /*eslint no-labels: ["error", { "allowLoop": true }]*/ | |
94 | ||
95 | label: | |
96 | while (true) { | |
97 | break label; | |
98 | } | |
99 | ``` | |
100 | ||
101 | ### allowSwitch | |
102 | ||
103 | Examples of **correct** code for the `{ "allowSwitch": true }` option: | |
104 | ||
105 | ```js | |
106 | /*eslint no-labels: ["error", { "allowSwitch": true }]*/ | |
107 | ||
108 | label: | |
109 | switch (a) { | |
110 | case 0: | |
111 | break label; | |
112 | } | |
113 | ``` | |
114 | ||
115 | ## When Not To Use It | |
116 | ||
117 | If you need to use labeled statements everywhere, then you can safely disable this rule. | |
118 | ||
119 | ## Related Rules | |
120 | ||
121 | * [no-extra-label](./no-extra-label.md) | |
122 | * [no-label-var](./no-label-var.md) | |
123 | * [no-unused-labels](./no-unused-labels.md) |