Actualizacion maquina principal
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / eslint / lib / rules / no-labels.js
1 /**
2  * @fileoverview Disallow Labeled Statements
3  * @author Nicholas C. Zakas
4  */
5 "use strict";
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          * @param {ASTNode} node A node to get.
55          * @returns {string} The kind of the node.
56          */
57         function getBodyKind(node) {
58             if (astUtils.isLoop(node)) {
59                 return "loop";
60             }
61             if (node.type === "SwitchStatement") {
62                 return "switch";
63             }
64             return "other";
65         }
66
67         /**
68          * Checks whether the label of a given kind is allowed or not.
69          * @param {string} kind A kind to check.
70          * @returns {boolean} `true` if the kind is allowed.
71          */
72         function isAllowed(kind) {
73             switch (kind) {
74                 case "loop": return allowLoop;
75                 case "switch": return allowSwitch;
76                 default: return false;
77             }
78         }
79
80         /**
81          * Checks whether a given name is a label of a loop or not.
82          * @param {string} label A name of a label to check.
83          * @returns {boolean} `true` if the name is a label of a loop.
84          */
85         function getKind(label) {
86             let info = scopeInfo;
87
88             while (info) {
89                 if (info.label === label) {
90                     return info.kind;
91                 }
92                 info = info.upper;
93             }
94
95             /* istanbul ignore next: syntax error */
96             return "other";
97         }
98
99         //--------------------------------------------------------------------------
100         // Public
101         //--------------------------------------------------------------------------
102
103         return {
104             LabeledStatement(node) {
105                 scopeInfo = {
106                     label: node.label.name,
107                     kind: getBodyKind(node.body),
108                     upper: scopeInfo
109                 };
110             },
111
112             "LabeledStatement:exit"(node) {
113                 if (!isAllowed(scopeInfo.kind)) {
114                     context.report({
115                         node,
116                         message: "Unexpected labeled statement."
117                     });
118                 }
119
120                 scopeInfo = scopeInfo.upper;
121             },
122
123             BreakStatement(node) {
124                 if (node.label && !isAllowed(getKind(node.label.name))) {
125                     context.report({
126                         node,
127                         message: "Unexpected label in break statement."
128                     });
129                 }
130             },
131
132             ContinueStatement(node) {
133                 if (node.label && !isAllowed(getKind(node.label.name))) {
134                     context.report({
135                         node,
136                         message: "Unexpected label in continue statement."
137                     });
138                 }
139             }
140         };
141
142     }
143 };