massive update, probably broken
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / eslint / lib / rules / max-statements.js
1 /**
2  * @fileoverview A rule to set the maximum number of statements in a function.
3  * @author Ian Christian Myers
4  */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const astUtils = require("./utils/ast-utils");
13 const { upperCaseFirst } = require("../shared/string-utils");
14
15 //------------------------------------------------------------------------------
16 // Rule Definition
17 //------------------------------------------------------------------------------
18
19 module.exports = {
20     meta: {
21         type: "suggestion",
22
23         docs: {
24             description: "enforce a maximum number of statements allowed in function blocks",
25             category: "Stylistic Issues",
26             recommended: false,
27             url: "https://eslint.org/docs/rules/max-statements"
28         },
29
30         schema: [
31             {
32                 oneOf: [
33                     {
34                         type: "integer",
35                         minimum: 0
36                     },
37                     {
38                         type: "object",
39                         properties: {
40                             maximum: {
41                                 type: "integer",
42                                 minimum: 0
43                             },
44                             max: {
45                                 type: "integer",
46                                 minimum: 0
47                             }
48                         },
49                         additionalProperties: false
50                     }
51                 ]
52             },
53             {
54                 type: "object",
55                 properties: {
56                     ignoreTopLevelFunctions: {
57                         type: "boolean"
58                     }
59                 },
60                 additionalProperties: false
61             }
62         ],
63         messages: {
64             exceed: "{{name}} has too many statements ({{count}}). Maximum allowed is {{max}}."
65         }
66     },
67
68     create(context) {
69
70         //--------------------------------------------------------------------------
71         // Helpers
72         //--------------------------------------------------------------------------
73
74         const functionStack = [],
75             option = context.options[0],
76             ignoreTopLevelFunctions = context.options[1] && context.options[1].ignoreTopLevelFunctions || false,
77             topLevelFunctions = [];
78         let maxStatements = 10;
79
80         if (
81             typeof option === "object" &&
82             (Object.prototype.hasOwnProperty.call(option, "maximum") || Object.prototype.hasOwnProperty.call(option, "max"))
83         ) {
84             maxStatements = option.maximum || option.max;
85         } else if (typeof option === "number") {
86             maxStatements = option;
87         }
88
89         /**
90          * Reports a node if it has too many statements
91          * @param {ASTNode} node node to evaluate
92          * @param {int} count Number of statements in node
93          * @param {int} max Maximum number of statements allowed
94          * @returns {void}
95          * @private
96          */
97         function reportIfTooManyStatements(node, count, max) {
98             if (count > max) {
99                 const name = upperCaseFirst(astUtils.getFunctionNameWithKind(node));
100
101                 context.report({
102                     node,
103                     messageId: "exceed",
104                     data: { name, count, max }
105                 });
106             }
107         }
108
109         /**
110          * When parsing a new function, store it in our function stack
111          * @returns {void}
112          * @private
113          */
114         function startFunction() {
115             functionStack.push(0);
116         }
117
118         /**
119          * Evaluate the node at the end of function
120          * @param {ASTNode} node node to evaluate
121          * @returns {void}
122          * @private
123          */
124         function endFunction(node) {
125             const count = functionStack.pop();
126
127             if (ignoreTopLevelFunctions && functionStack.length === 0) {
128                 topLevelFunctions.push({ node, count });
129             } else {
130                 reportIfTooManyStatements(node, count, maxStatements);
131             }
132         }
133
134         /**
135          * Increment the count of the functions
136          * @param {ASTNode} node node to evaluate
137          * @returns {void}
138          * @private
139          */
140         function countStatements(node) {
141             functionStack[functionStack.length - 1] += node.body.length;
142         }
143
144         //--------------------------------------------------------------------------
145         // Public API
146         //--------------------------------------------------------------------------
147
148         return {
149             FunctionDeclaration: startFunction,
150             FunctionExpression: startFunction,
151             ArrowFunctionExpression: startFunction,
152
153             BlockStatement: countStatements,
154
155             "FunctionDeclaration:exit": endFunction,
156             "FunctionExpression:exit": endFunction,
157             "ArrowFunctionExpression:exit": endFunction,
158
159             "Program:exit"() {
160                 if (topLevelFunctions.length === 1) {
161                     return;
162                 }
163
164                 topLevelFunctions.forEach(element => {
165                     const count = element.count;
166                     const node = element.node;
167
168                     reportIfTooManyStatements(node, count, maxStatements);
169                 });
170             }
171         };
172
173     }
174 };