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 //------------------------------------------------------------------------------
24 description: "enforce a maximum number of statements allowed in function blocks",
25 category: "Stylistic Issues",
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();
127 if (ignoreTopLevelFunctions && functionStack.length === 0) {
128 topLevelFunctions.push({ node, count });
130 reportIfTooManyStatements(node, count, maxStatements);
135 * Increment the count of the functions
136 * @param {ASTNode} node node to evaluate
140 function countStatements(node) {
141 functionStack[functionStack.length - 1] += node.body.length;
144 //--------------------------------------------------------------------------
146 //--------------------------------------------------------------------------
149 FunctionDeclaration: startFunction,
150 FunctionExpression: startFunction,
151 ArrowFunctionExpression: startFunction,
153 BlockStatement: countStatements,
155 "FunctionDeclaration:exit": endFunction,
156 "FunctionExpression:exit": endFunction,
157 "ArrowFunctionExpression:exit": endFunction,
160 if (topLevelFunctions.length === 1) {
164 topLevelFunctions.forEach(element => {
165 const count = element.count;
166 const node = element.node;
168 reportIfTooManyStatements(node, count, maxStatements);