UNPKG

3.48 kBJavaScriptView Raw
1/**
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.
5 */
6
7"use strict";
8
9//------------------------------------------------------------------------------
10// Rule Definition
11//------------------------------------------------------------------------------
12
13module.exports = function(context) {
14
15 //--------------------------------------------------------------------------
16 // Helpers
17 //--------------------------------------------------------------------------
18
19 var functionStack = [],
20 maxStatements = context.options[0] || 10,
21 ignoreTopLevelFunctions = context.options[1] && context.options[1].ignoreTopLevelFunctions || false,
22 topLevelFunctions = [];
23
24 /**
25 * Reports a node if it has too many statements
26 * @param {ASTNode} node node to evaluate
27 * @param {int} count Number of statements in node
28 * @param {int} max Maximum number of statements allowed
29 * @returns {void}
30 * @private
31 */
32 function reportIfTooManyStatements(node, count, max) {
33 if (count > max) {
34 context.report(
35 node,
36 "This function has too many statements ({{count}}). Maximum allowed is {{max}}.",
37 { count: count, max: max });
38 }
39 }
40
41 /**
42 * When parsing a new function, store it in our function stack
43 * @returns {void}
44 * @private
45 */
46 function startFunction() {
47 functionStack.push(0);
48 }
49
50 /**
51 * Evaluate the node at the end of function
52 * @param {ASTNode} node node to evaluate
53 * @returns {void}
54 * @private
55 */
56 function endFunction(node) {
57 var count = functionStack.pop();
58 if (ignoreTopLevelFunctions && functionStack.length === 0) {
59 topLevelFunctions.push({ node: node, count: count});
60 } else {
61 reportIfTooManyStatements(node, count, maxStatements);
62 }
63 }
64
65 /**
66 * Increment the count of the functions
67 * @param {ASTNode} node node to evaluate
68 * @returns {void}
69 * @private
70 */
71 function countStatements(node) {
72 functionStack[functionStack.length - 1] += node.body.length;
73 }
74
75 //--------------------------------------------------------------------------
76 // Public API
77 //--------------------------------------------------------------------------
78
79 return {
80 "FunctionDeclaration": startFunction,
81 "FunctionExpression": startFunction,
82 "ArrowFunctionExpression": startFunction,
83
84 "BlockStatement": countStatements,
85
86 "FunctionDeclaration:exit": endFunction,
87 "FunctionExpression:exit": endFunction,
88 "ArrowFunctionExpression:exit": endFunction,
89
90 "Program:exit": function() {
91 if (topLevelFunctions.length === 1) {
92 return;
93 }
94
95 topLevelFunctions.forEach(function(element) {
96 var count = element.count;
97 var node = element.node;
98 reportIfTooManyStatements(node, count, maxStatements);
99 });
100 }
101 };
102
103};
104
105module.exports.schema = [
106 {
107 "type": "integer",
108 "minimum": 0
109 },
110 {
111 "type": "object",
112 "properties": {
113 "ignoreTopLevelFunctions": {
114 "type": "boolean"
115 }
116 },
117 "additionalProperties": false
118 }
119];