2 * @fileoverview A rule to set the maximum number of statements in a function.
3 * @author Ian Christian Myers
4 * @copyright 2013 Ian Christian Myers. All rights reserved.
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
13 module.exports = function(context) {
15 //--------------------------------------------------------------------------
17 //--------------------------------------------------------------------------
19 var functionStack = [],
20 maxStatements = context.options[0] || 10;
22 function startFunction() {
23 functionStack.push(0);
26 function endFunction(node) {
27 var count = functionStack.pop();
29 if (count > maxStatements) {
30 context.report(node, "This function has too many statements ({{count}}). Maximum allowed is {{max}}.",
31 { count: count, max: maxStatements });
35 function countStatements(node) {
36 functionStack[functionStack.length - 1] += node.body.length;
39 //--------------------------------------------------------------------------
41 //--------------------------------------------------------------------------
44 "FunctionDeclaration": startFunction,
45 "FunctionExpression": startFunction,
46 "ArrowFunctionExpression": startFunction,
48 "BlockStatement": countStatements,
50 "FunctionDeclaration:exit": endFunction,
51 "FunctionExpression:exit": endFunction,
52 "ArrowFunctionExpression:exit": endFunction