]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/no-labels.md
bump version to 8.4.0-3
[pve-eslint.git] / eslint / docs / rules / no-labels.md
CommitLineData
eb39fafa
DC
1# Disallow Labeled Statements (no-labels)
2
3Labeled statements in JavaScript are used in conjunction with `break` and `continue` to control flow around multiple loops. For example:
4
5```js
6outer:
7 while (true) {
8
9 while (true) {
10 break outer;
11 }
12 }
13```
14
15The `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
17While 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
21This 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
23Examples of **incorrect** code for this rule:
24
25```js
26/*eslint no-labels: "error"*/
27
28label:
29 while(true) {
30 // ...
31 }
32
33label:
34 while(true) {
35 break label;
36 }
37
38label:
39 while(true) {
40 continue label;
41 }
42
43label:
44 switch (a) {
45 case 0:
46 break label;
47 }
48
49label:
50 {
51 break label;
52 }
53
54label:
55 if (a) {
56 break label;
57 }
58```
59
60Examples of **correct** code for this rule:
61
62```js
63/*eslint no-labels: "error"*/
64
65var f = {
66 label: "foo"
67};
68
69while (true) {
70 break;
71}
72
73while (true) {
74 continue;
75}
76```
77
78## Options
79
80The 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
85Actually labeled statements in JavaScript can be used with other than loop and switch statements.
86However, this way is ultra rare, not well-known, so this would be confusing developers.
87
88### allowLoop
89
90Examples of **correct** code for the `{ "allowLoop": true }` option:
91
92```js
93/*eslint no-labels: ["error", { "allowLoop": true }]*/
94
95label:
96 while (true) {
97 break label;
98 }
99```
100
101### allowSwitch
102
103Examples of **correct** code for the `{ "allowSwitch": true }` option:
104
105```js
106/*eslint no-labels: ["error", { "allowSwitch": true }]*/
107
108label:
109 switch (a) {
110 case 0:
111 break label;
112 }
113```
114
115## When Not To Use It
116
117If 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)