1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | module.exports = function(context) {
|
11 |
|
12 | "use strict";
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | var functionStack = [],
|
19 | maxDepth = context.options[0] || 4;
|
20 |
|
21 | function startFunction() {
|
22 | functionStack.push(0);
|
23 | }
|
24 |
|
25 | function endFunction() {
|
26 | functionStack.pop();
|
27 | }
|
28 |
|
29 | function pushBlock(node) {
|
30 | var len = ++functionStack[functionStack.length - 1];
|
31 |
|
32 | if (len > maxDepth) {
|
33 | context.report(node, "Blocks are nested too deeply ({{depth}}).",
|
34 | { depth: len });
|
35 | }
|
36 | }
|
37 |
|
38 | function popBlock() {
|
39 | functionStack[functionStack.length - 1]--;
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | return {
|
47 | "Program": startFunction,
|
48 | "FunctionDeclaration": startFunction,
|
49 | "FunctionExpression": startFunction,
|
50 |
|
51 | "IfStatement": pushBlock,
|
52 | "SwitchStatement": pushBlock,
|
53 | "TryStatement": pushBlock,
|
54 | "DoWhileStatement": pushBlock,
|
55 | "WhileStatement": pushBlock,
|
56 | "WithStatement": pushBlock,
|
57 | "ForStatement": pushBlock,
|
58 | "ForInStatement": pushBlock,
|
59 |
|
60 | "IfStatement:exit": popBlock,
|
61 | "SwitchStatement:exit": popBlock,
|
62 | "TryStatement:exit": popBlock,
|
63 | "DoWhileStatement:exit": popBlock,
|
64 | "WhileStatement:exit": popBlock,
|
65 | "WithStatement:exit": popBlock,
|
66 | "ForStatement:exit": popBlock,
|
67 | "ForInStatement:exit": popBlock,
|
68 |
|
69 | "FunctionDeclaration:exit": endFunction,
|
70 | "FunctionExpression:exit": endFunction,
|
71 | "Program:exit": endFunction
|
72 | };
|
73 |
|
74 | };
|