]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: no-label-var | |
8f9d1d4d DC |
3 | rule_type: suggestion |
4 | related_rules: | |
5 | - no-extra-label | |
6 | - no-labels | |
7 | - no-unused-labels | |
8 | --- | |
9 | ||
eb39fafa DC |
10 | |
11 | ## Rule Details | |
12 | ||
13 | This rule aims to create clearer code by disallowing the bad practice of creating a label that shares a name with a variable that is in scope. | |
14 | ||
15 | Examples of **incorrect** code for this rule: | |
16 | ||
8f9d1d4d DC |
17 | ::: incorrect |
18 | ||
eb39fafa DC |
19 | ```js |
20 | /*eslint no-label-var: "error"*/ | |
21 | ||
22 | var x = foo; | |
23 | function bar() { | |
24 | x: | |
25 | for (;;) { | |
26 | break x; | |
27 | } | |
28 | } | |
29 | ``` | |
30 | ||
8f9d1d4d DC |
31 | ::: |
32 | ||
eb39fafa DC |
33 | Examples of **correct** code for this rule: |
34 | ||
8f9d1d4d DC |
35 | ::: correct |
36 | ||
eb39fafa DC |
37 | ```js |
38 | /*eslint no-label-var: "error"*/ | |
39 | ||
40 | // The variable that has the same name as the label is not in scope. | |
41 | ||
42 | function foo() { | |
43 | var q = t; | |
44 | } | |
45 | ||
46 | function bar() { | |
47 | q: | |
48 | for(;;) { | |
49 | break q; | |
50 | } | |
51 | } | |
52 | ``` | |
53 | ||
8f9d1d4d DC |
54 | ::: |
55 | ||
eb39fafa DC |
56 | ## When Not To Use It |
57 | ||
58 | If you don't want to be notified about usage of labels, then it's safe to disable this rule. |