1 | /**
|
2 | * @fileoverview Disallow Labeled Statements
|
3 | * @author Nicholas C. Zakas
|
4 | */
|
5 | ;
|
6 |
|
7 | //------------------------------------------------------------------------------
|
8 | // Requirements
|
9 | //------------------------------------------------------------------------------
|
10 |
|
11 | const astUtils = require("./utils/ast-utils");
|
12 |
|
13 | //------------------------------------------------------------------------------
|
14 | // Rule Definition
|
15 | //------------------------------------------------------------------------------
|
16 |
|
17 | module.exports = {
|
18 | meta: {
|
19 | type: "suggestion",
|
20 |
|
21 | docs: {
|
22 | description: "disallow labeled statements",
|
23 | category: "Best Practices",
|
24 | recommended: false,
|
25 | url: "https://eslint.org/docs/rules/no-labels"
|
26 | },
|
27 |
|
28 | schema: [
|
29 | {
|
30 | type: "object",
|
31 | properties: {
|
32 | allowLoop: {
|
33 | type: "boolean",
|
34 | default: false
|
35 | },
|
36 | allowSwitch: {
|
37 | type: "boolean",
|
38 | default: false
|
39 | }
|
40 | },
|
41 | additionalProperties: false
|
42 | }
|
43 | ]
|
44 | },
|
45 |
|
46 | create(context) {
|
47 | const options = context.options[0];
|
48 | const allowLoop = options && options.allowLoop;
|
49 | const allowSwitch = options && options.allowSwitch;
|
50 | let scopeInfo = null;
|
51 |
|
52 | /**
|
53 | * Gets the kind of a given node.
|
54 | *
|
55 | * @param {ASTNode} node - A node to get.
|
56 | * @returns {string} The kind of the node.
|
57 | */
|
58 | function getBodyKind(node) {
|
59 | if (astUtils.isLoop(node)) {
|
60 | return "loop";
|
61 | }
|
62 | if (node.type === "SwitchStatement") {
|
63 | return "switch";
|
64 | }
|
65 | return "other";
|
66 | }
|
67 |
|
68 | /**
|
69 | * Checks whether the label of a given kind is allowed or not.
|
70 | *
|
71 | * @param {string} kind - A kind to check.
|
72 | * @returns {boolean} `true` if the kind is allowed.
|
73 | */
|
74 | function isAllowed(kind) {
|
75 | switch (kind) {
|
76 | case "loop": return allowLoop;
|
77 | case "switch": return allowSwitch;
|
78 | default: return false;
|
79 | }
|
80 | }
|
81 |
|
82 | /**
|
83 | * Checks whether a given name is a label of a loop or not.
|
84 | *
|
85 | * @param {string} label - A name of a label to check.
|
86 | * @returns {boolean} `true` if the name is a label of a loop.
|
87 | */
|
88 | function getKind(label) {
|
89 | let info = scopeInfo;
|
90 |
|
91 | while (info) {
|
92 | if (info.label === label) {
|
93 | return info.kind;
|
94 | }
|
95 | info = info.upper;
|
96 | }
|
97 |
|
98 | /* istanbul ignore next: syntax error */
|
99 | return "other";
|
100 | }
|
101 |
|
102 | //--------------------------------------------------------------------------
|
103 | // Public
|
104 | //--------------------------------------------------------------------------
|
105 |
|
106 | return {
|
107 | LabeledStatement(node) {
|
108 | scopeInfo = {
|
109 | label: node.label.name,
|
110 | kind: getBodyKind(node.body),
|
111 | upper: scopeInfo
|
112 | };
|
113 | },
|
114 |
|
115 | "LabeledStatement:exit"(node) {
|
116 | if (!isAllowed(scopeInfo.kind)) {
|
117 | context.report({
|
118 | node,
|
119 | message: "Unexpected labeled statement."
|
120 | });
|
121 | }
|
122 |
|
123 | scopeInfo = scopeInfo.upper;
|
124 | },
|
125 |
|
126 | BreakStatement(node) {
|
127 | if (node.label && !isAllowed(getKind(node.label.name))) {
|
128 | context.report({
|
129 | node,
|
130 | message: "Unexpected label in break statement."
|
131 | });
|
132 | }
|
133 | },
|
134 |
|
135 | ContinueStatement(node) {
|
136 | if (node.label && !isAllowed(getKind(node.label.name))) {
|
137 | context.report({
|
138 | node,
|
139 | message: "Unexpected label in continue statement."
|
140 | });
|
141 | }
|
142 | }
|
143 | };
|
144 |
|
145 | }
|
146 | };
|