1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | "use strict";
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | module.exports = function(context) {
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | var functionStack = [],
|
20 | maxDepth = context.options[0] || 4;
|
21 |
|
22 | |
23 |
|
24 |
|
25 |
|
26 |
|
27 | function startFunction() {
|
28 | functionStack.push(0);
|
29 | }
|
30 |
|
31 | |
32 |
|
33 |
|
34 |
|
35 |
|
36 | function endFunction() {
|
37 | functionStack.pop();
|
38 | }
|
39 |
|
40 | |
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | function pushBlock(node) {
|
47 | var len = ++functionStack[functionStack.length - 1];
|
48 |
|
49 | if (len > maxDepth) {
|
50 | context.report(node, "Blocks are nested too deeply ({{depth}}).",
|
51 | { depth: len });
|
52 | }
|
53 | }
|
54 |
|
55 | |
56 |
|
57 |
|
58 |
|
59 |
|
60 | function popBlock() {
|
61 | functionStack[functionStack.length - 1]--;
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | return {
|
69 | "Program": startFunction,
|
70 | "FunctionDeclaration": startFunction,
|
71 | "FunctionExpression": startFunction,
|
72 | "ArrowFunctionExpression": startFunction,
|
73 |
|
74 | "IfStatement": function(node) {
|
75 | if (node.parent.type !== "IfStatement") {
|
76 | pushBlock(node);
|
77 | }
|
78 | },
|
79 | "SwitchStatement": pushBlock,
|
80 | "TryStatement": pushBlock,
|
81 | "DoWhileStatement": pushBlock,
|
82 | "WhileStatement": pushBlock,
|
83 | "WithStatement": pushBlock,
|
84 | "ForStatement": pushBlock,
|
85 | "ForInStatement": pushBlock,
|
86 | "ForOfStatement": pushBlock,
|
87 |
|
88 | "IfStatement:exit": popBlock,
|
89 | "SwitchStatement:exit": popBlock,
|
90 | "TryStatement:exit": popBlock,
|
91 | "DoWhileStatement:exit": popBlock,
|
92 | "WhileStatement:exit": popBlock,
|
93 | "WithStatement:exit": popBlock,
|
94 | "ForStatement:exit": popBlock,
|
95 | "ForInStatement:exit": popBlock,
|
96 | "ForOfStatement:exit": popBlock,
|
97 |
|
98 | "FunctionDeclaration:exit": endFunction,
|
99 | "FunctionExpression:exit": endFunction,
|
100 | "ArrowFunctionExpression:exit": endFunction,
|
101 | "Program:exit": endFunction
|
102 | };
|
103 |
|
104 | };
|
105 |
|
106 | module.exports.schema = [
|
107 | {
|
108 | "type": "integer",
|
109 | "minimum": 0
|
110 | }
|
111 | ];
|