9fe11bfd91d8bc21a4f51dc25286abb7f24407e1
[platform/framework/web/crosswalk-tizen.git] /
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
13 module.exports = function(context) {
14
15     //--------------------------------------------------------------------------
16     // Helpers
17     //--------------------------------------------------------------------------
18
19     var functionStack = [],
20         maxStatements = context.options[0] || 10;
21
22     function startFunction() {
23         functionStack.push(0);
24     }
25
26     function endFunction(node) {
27         var count = functionStack.pop();
28
29         if (count > maxStatements) {
30             context.report(node, "This function has too many statements ({{count}}). Maximum allowed is {{max}}.",
31                     { count: count, max: maxStatements });
32         }
33     }
34
35     function countStatements(node) {
36         functionStack[functionStack.length - 1] += node.body.length;
37     }
38
39     //--------------------------------------------------------------------------
40     // Public API
41     //--------------------------------------------------------------------------
42
43     return {
44         "FunctionDeclaration": startFunction,
45         "FunctionExpression": startFunction,
46         "ArrowFunctionExpression": startFunction,
47
48         "BlockStatement": countStatements,
49
50         "FunctionDeclaration:exit": endFunction,
51         "FunctionExpression:exit": endFunction,
52         "ArrowFunctionExpression:exit": endFunction
53     };
54
55 };