590274ffd1c3b3438ad335b1c56dd23a80550c65
[platform/framework/web/crosswalk-tizen.git] /
1 /**
2  * @fileoverview Rule to enforce a maximum number of nested callbacks.
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     // Constants
17     //--------------------------------------------------------------------------
18
19     var THRESHOLD = context.options[0];
20
21     //--------------------------------------------------------------------------
22     // Helpers
23     //--------------------------------------------------------------------------
24
25     var callbackStack = [];
26
27     /**
28      * Checks a given function node for too many callbacks.
29      * @param {ASTNode} node The node to check.
30      * @returns {void}
31      * @private
32      */
33     function checkFunction(node) {
34         var parent = node.parent;
35
36         if (parent.type === "CallExpression") {
37             callbackStack.push(node);
38         }
39
40         if (callbackStack.length > THRESHOLD) {
41             var opts = {num: callbackStack.length, max: THRESHOLD};
42             context.report(node, "Too many nested callbacks ({{num}}). Maximum allowed is {{max}}.", opts);
43         }
44     }
45
46     /**
47      * Pops the call stack.
48      * @returns {void}
49      * @private
50      */
51     function popStack() {
52         callbackStack.pop();
53     }
54
55     //--------------------------------------------------------------------------
56     // Public API
57     //--------------------------------------------------------------------------
58
59     return {
60         "ArrowFunctionExpression": checkFunction,
61         "ArrowFunctionExpression:exit": popStack,
62
63         "FunctionExpression": checkFunction,
64         "FunctionExpression:exit": popStack
65     };
66
67 };