]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: no-extra-label | |
8f9d1d4d DC |
3 | rule_type: suggestion |
4 | related_rules: | |
5 | - no-labels | |
6 | - no-label-var | |
7 | - no-unused-labels | |
8 | --- | |
9 | ||
10 | ||
eb39fafa DC |
11 | |
12 | If a loop contains no nested loops or switches, labeling the loop is unnecessary. | |
13 | ||
14 | ```js | |
15 | A: while (a) { | |
16 | break A; | |
17 | } | |
18 | ``` | |
19 | ||
20 | You can achieve the same result by removing the label and using `break` or `continue` without a label. | |
21 | Probably those labels would confuse developers because they expect labels to jump to further. | |
22 | ||
23 | ## Rule Details | |
24 | ||
25 | This rule is aimed at eliminating unnecessary labels. | |
26 | ||
27 | Examples of **incorrect** code for this rule: | |
28 | ||
8f9d1d4d DC |
29 | ::: incorrect |
30 | ||
eb39fafa DC |
31 | ```js |
32 | /*eslint no-extra-label: "error"*/ | |
33 | ||
34 | A: while (a) { | |
35 | break A; | |
36 | } | |
37 | ||
38 | B: for (let i = 0; i < 10; ++i) { | |
39 | break B; | |
40 | } | |
41 | ||
42 | C: switch (a) { | |
43 | case 0: | |
44 | break C; | |
45 | } | |
46 | ``` | |
47 | ||
8f9d1d4d DC |
48 | ::: |
49 | ||
eb39fafa DC |
50 | Examples of **correct** code for this rule: |
51 | ||
8f9d1d4d DC |
52 | ::: correct |
53 | ||
eb39fafa DC |
54 | ```js |
55 | /*eslint no-extra-label: "error"*/ | |
56 | ||
57 | while (a) { | |
58 | break; | |
59 | } | |
60 | ||
61 | for (let i = 0; i < 10; ++i) { | |
62 | break; | |
63 | } | |
64 | ||
65 | switch (a) { | |
66 | case 0: | |
67 | break; | |
68 | } | |
69 | ||
70 | A: { | |
71 | break A; | |
72 | } | |
73 | ||
74 | B: while (a) { | |
75 | while (b) { | |
76 | break B; | |
77 | } | |
78 | } | |
79 | ||
80 | C: switch (a) { | |
81 | case 0: | |
82 | while (b) { | |
83 | break C; | |
84 | } | |
85 | break; | |
86 | } | |
87 | ``` | |
88 | ||
8f9d1d4d DC |
89 | ::: |
90 | ||
eb39fafa DC |
91 | ## When Not To Use It |
92 | ||
93 | If you don't want to be notified about usage of labels, then it's safe to disable this rule. |