1 | /**
|
2 | * @fileoverview Rule to enforce a maximum number of nested callbacks.
|
3 | * @author Ian Christian Myers
|
4 | */
|
5 |
|
6 | //------------------------------------------------------------------------------
|
7 | // Rule Definition
|
8 | //------------------------------------------------------------------------------
|
9 |
|
10 | module.exports = function(context) {
|
11 |
|
12 | ;
|
13 |
|
14 | //--------------------------------------------------------------------------
|
15 | // Constants
|
16 | //--------------------------------------------------------------------------
|
17 |
|
18 | var THRESHOLD = context.options[0];
|
19 |
|
20 | //--------------------------------------------------------------------------
|
21 | // Public API
|
22 | //--------------------------------------------------------------------------
|
23 |
|
24 | var callbackStack = [];
|
25 |
|
26 | return {
|
27 |
|
28 | "FunctionExpression": function (node) {
|
29 | var parent = context.getAncestors().pop();
|
30 |
|
31 | if (parent.type === "CallExpression") {
|
32 | callbackStack.push(node);
|
33 | }
|
34 |
|
35 | if (callbackStack.length > THRESHOLD) {
|
36 | var opts = {num: callbackStack.length, max: THRESHOLD};
|
37 | context.report(node, "Too many nested callbacks ({{num}}). Maximum allowed is {{max}}.", opts);
|
38 | }
|
39 | },
|
40 |
|
41 |
|
42 | "FunctionExpression:exit": function() {
|
43 | callbackStack.pop();
|
44 | }
|
45 |
|
46 | };
|
47 |
|
48 | };
|