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.
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
13 module.exports = function(context) {
15 //--------------------------------------------------------------------------
17 //--------------------------------------------------------------------------
19 var THRESHOLD = context.options[0];
21 //--------------------------------------------------------------------------
23 //--------------------------------------------------------------------------
25 var callbackStack = [];
28 * Checks a given function node for too many callbacks.
29 * @param {ASTNode} node The node to check.
33 function checkFunction(node) {
34 var parent = node.parent;
36 if (parent.type === "CallExpression") {
37 callbackStack.push(node);
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);
47 * Pops the call stack.
55 //--------------------------------------------------------------------------
57 //--------------------------------------------------------------------------
60 "ArrowFunctionExpression": checkFunction,
61 "ArrowFunctionExpression:exit": popStack,
63 "FunctionExpression": checkFunction,
64 "FunctionExpression:exit": popStack