]>
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 //------------------------------------------------------------------------------
12 /** @type {import('../shared/types').Rule} */
18 description
: "Enforce a maximum depth that blocks can be nested",
20 url
: "https://eslint.org/docs/latest/rules/max-depth"
42 additionalProperties
: false
48 tooDeeply
: "Blocks are nested too deeply ({{depth}}). Maximum allowed is {{maxDepth}}."
54 //--------------------------------------------------------------------------
56 //--------------------------------------------------------------------------
58 const functionStack
= [],
59 option
= context
.options
[0];
63 typeof option
=== "object" &&
64 (Object
.prototype.hasOwnProperty
.call(option
, "maximum") || Object
.prototype.hasOwnProperty
.call(option
, "max"))
66 maxDepth
= option
.maximum
|| option
.max
;
68 if (typeof option
=== "number") {
73 * When parsing a new function, store it in our function stack
77 function startFunction() {
78 functionStack
.push(0);
82 * When parsing is done then pop out the reference
86 function endFunction() {
91 * Save the block and Evaluate the node
92 * @param {ASTNode} node node to evaluate
96 function pushBlock(node
) {
97 const len
= ++functionStack
[functionStack
.length
- 1];
100 context
.report({ node
, messageId
: "tooDeeply", data
: { depth
: len
, maxDepth
} });
105 * Pop the saved block
109 function popBlock() {
110 functionStack
[functionStack
.length
- 1]--;
113 //--------------------------------------------------------------------------
115 //--------------------------------------------------------------------------
118 Program
: startFunction
,
119 FunctionDeclaration
: startFunction
,
120 FunctionExpression
: startFunction
,
121 ArrowFunctionExpression
: startFunction
,
122 StaticBlock
: startFunction
,
125 if (node
.parent
.type
!== "IfStatement") {
129 SwitchStatement
: pushBlock
,
130 TryStatement
: pushBlock
,
131 DoWhileStatement
: pushBlock
,
132 WhileStatement
: pushBlock
,
133 WithStatement
: pushBlock
,
134 ForStatement
: pushBlock
,
135 ForInStatement
: pushBlock
,
136 ForOfStatement
: pushBlock
,
138 "IfStatement:exit": popBlock
,
139 "SwitchStatement:exit": popBlock
,
140 "TryStatement:exit": popBlock
,
141 "DoWhileStatement:exit": popBlock
,
142 "WhileStatement:exit": popBlock
,
143 "WithStatement:exit": popBlock
,
144 "ForStatement:exit": popBlock
,
145 "ForInStatement:exit": popBlock
,
146 "ForOfStatement:exit": popBlock
,
148 "FunctionDeclaration:exit": endFunction
,
149 "FunctionExpression:exit": endFunction
,
150 "ArrowFunctionExpression:exit": endFunction
,
151 "StaticBlock:exit": endFunction
,
152 "Program:exit": endFunction