]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/max-statements.js
2 * @fileoverview A rule to set the maximum number of statements in a function.
3 * @author Ian Christian Myers
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils
= require("./utils/ast-utils");
13 const { upperCaseFirst
} = require("../shared/string-utils");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
19 /** @type {import('../shared/types').Rule} */
25 description
: "Enforce a maximum number of statements allowed in function blocks",
27 url
: "https://eslint.org/docs/rules/max-statements"
49 additionalProperties
: false
56 ignoreTopLevelFunctions
: {
60 additionalProperties
: false
64 exceed
: "{{name}} has too many statements ({{count}}). Maximum allowed is {{max}}."
70 //--------------------------------------------------------------------------
72 //--------------------------------------------------------------------------
74 const functionStack
= [],
75 option
= context
.options
[0],
76 ignoreTopLevelFunctions
= context
.options
[1] && context
.options
[1].ignoreTopLevelFunctions
|| false,
77 topLevelFunctions
= [];
78 let maxStatements
= 10;
81 typeof option
=== "object" &&
82 (Object
.prototype.hasOwnProperty
.call(option
, "maximum") || Object
.prototype.hasOwnProperty
.call(option
, "max"))
84 maxStatements
= option
.maximum
|| option
.max
;
85 } else if (typeof option
=== "number") {
86 maxStatements
= option
;
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
97 function reportIfTooManyStatements(node
, count
, max
) {
99 const name
= upperCaseFirst(astUtils
.getFunctionNameWithKind(node
));
104 data
: { name
, count
, max
}
110 * When parsing a new function, store it in our function stack
114 function startFunction() {
115 functionStack
.push(0);
119 * Evaluate the node at the end of function
120 * @param {ASTNode} node node to evaluate
124 function endFunction(node
) {
125 const count
= functionStack
.pop();
128 * This rule does not apply to class static blocks, but we have to track them so
129 * that statements in them do not count as statements in the enclosing function.
131 if (node
.type
=== "StaticBlock") {
135 if (ignoreTopLevelFunctions
&& functionStack
.length
=== 0) {
136 topLevelFunctions
.push({ node
, count
});
138 reportIfTooManyStatements(node
, count
, maxStatements
);
143 * Increment the count of the functions
144 * @param {ASTNode} node node to evaluate
148 function countStatements(node
) {
149 functionStack
[functionStack
.length
- 1] += node
.body
.length
;
152 //--------------------------------------------------------------------------
154 //--------------------------------------------------------------------------
157 FunctionDeclaration
: startFunction
,
158 FunctionExpression
: startFunction
,
159 ArrowFunctionExpression
: startFunction
,
160 StaticBlock
: startFunction
,
162 BlockStatement
: countStatements
,
164 "FunctionDeclaration:exit": endFunction
,
165 "FunctionExpression:exit": endFunction
,
166 "ArrowFunctionExpression:exit": endFunction
,
167 "StaticBlock:exit": endFunction
,
170 if (topLevelFunctions
.length
=== 1) {
174 topLevelFunctions
.forEach(element
=> {
175 const count
= element
.count
;
176 const node
= element
.node
;
178 reportIfTooManyStatements(node
, count
, maxStatements
);