1 | /**
|
2 | * @fileoverview A rule to set the maximum number of statements in a function.
|
3 | * @author Ian Christian Myers
|
4 | */
|
5 |
|
6 | //------------------------------------------------------------------------------
|
7 | // Rule Definition
|
8 | //------------------------------------------------------------------------------
|
9 |
|
10 | module.exports = function(context) {
|
11 |
|
12 | ;
|
13 |
|
14 | //--------------------------------------------------------------------------
|
15 | // Helpers
|
16 | //--------------------------------------------------------------------------
|
17 |
|
18 | var functionStack = [],
|
19 | maxStatements = context.options[0] || 10;
|
20 |
|
21 | function startFunction() {
|
22 | functionStack.push(0);
|
23 | }
|
24 |
|
25 | function endFunction(node) {
|
26 | var count = functionStack.pop();
|
27 |
|
28 | if (count > maxStatements) {
|
29 | context.report(node, "This function has too many statements ({{count}}). Maximum allowed is {{max}}.",
|
30 | { count: count, max: maxStatements });
|
31 | }
|
32 | }
|
33 |
|
34 | function countStatements(node) {
|
35 | functionStack[functionStack.length - 1] += node.body.length;
|
36 | }
|
37 |
|
38 | //--------------------------------------------------------------------------
|
39 | // Public API
|
40 | //--------------------------------------------------------------------------
|
41 |
|
42 | return {
|
43 | "FunctionDeclaration": startFunction,
|
44 | "FunctionExpression": startFunction,
|
45 |
|
46 | "BlockStatement": countStatements,
|
47 |
|
48 | "FunctionDeclaration:exit": endFunction,
|
49 | "FunctionExpression:exit": endFunction
|
50 | };
|
51 |
|
52 | };
|