]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/max-depth.js
2 * @fileoverview A rule to set the maximum depth block can be nested in a function.
3 * @author Ian Christian Myers
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
17 description
: "enforce a maximum depth that blocks can be nested",
19 url
: "https://eslint.org/docs/rules/max-depth"
41 additionalProperties
: false
47 tooDeeply
: "Blocks are nested too deeply ({{depth}}). Maximum allowed is {{maxDepth}}."
53 //--------------------------------------------------------------------------
55 //--------------------------------------------------------------------------
57 const functionStack
= [],
58 option
= context
.options
[0];
62 typeof option
=== "object" &&
63 (Object
.prototype.hasOwnProperty
.call(option
, "maximum") || Object
.prototype.hasOwnProperty
.call(option
, "max"))
65 maxDepth
= option
.maximum
|| option
.max
;
67 if (typeof option
=== "number") {
72 * When parsing a new function, store it in our function stack
76 function startFunction() {
77 functionStack
.push(0);
81 * When parsing is done then pop out the reference
85 function endFunction() {
90 * Save the block and Evaluate the node
91 * @param {ASTNode} node node to evaluate
95 function pushBlock(node
) {
96 const len
= ++functionStack
[functionStack
.length
- 1];
99 context
.report({ node
, messageId
: "tooDeeply", data
: { depth
: len
, maxDepth
} });
104 * Pop the saved block
108 function popBlock() {
109 functionStack
[functionStack
.length
- 1]--;
112 //--------------------------------------------------------------------------
114 //--------------------------------------------------------------------------
117 Program
: startFunction
,
118 FunctionDeclaration
: startFunction
,
119 FunctionExpression
: startFunction
,
120 ArrowFunctionExpression
: startFunction
,
121 StaticBlock
: startFunction
,
124 if (node
.parent
.type
!== "IfStatement") {
128 SwitchStatement
: pushBlock
,
129 TryStatement
: pushBlock
,
130 DoWhileStatement
: pushBlock
,
131 WhileStatement
: pushBlock
,
132 WithStatement
: pushBlock
,
133 ForStatement
: pushBlock
,
134 ForInStatement
: pushBlock
,
135 ForOfStatement
: pushBlock
,
137 "IfStatement:exit": popBlock
,
138 "SwitchStatement:exit": popBlock
,
139 "TryStatement:exit": popBlock
,
140 "DoWhileStatement:exit": popBlock
,
141 "WhileStatement:exit": popBlock
,
142 "WithStatement:exit": popBlock
,
143 "ForStatement:exit": popBlock
,
144 "ForInStatement:exit": popBlock
,
145 "ForOfStatement:exit": popBlock
,
147 "FunctionDeclaration:exit": endFunction
,
148 "FunctionExpression:exit": endFunction
,
149 "ArrowFunctionExpression:exit": endFunction
,
150 "StaticBlock:exit": endFunction
,
151 "Program:exit": endFunction