max-depth
强制执行块可以嵌套的最大深度
如果块嵌套超过一定深度,许多开发者认为代码难以阅读。
¥Many developers consider code difficult to read if blocks are nested beyond a certain depth.
规则详情
¥Rule Details
此规则强制执行块可以嵌套的最大深度以降低代码复杂性。
¥This rule enforces a maximum depth that blocks can be nested to reduce code complexity.
选项
¥Options
此规则有一个数字或对象选项:
¥This rule has a number or object option:
-
"max"
(默认4
)强制块可以嵌套的最大深度¥
"max"
(default4
) enforces a maximum depth that blocks can be nested
已弃用:对象属性 maximum
已弃用;请改用对象属性 max
。
¥Deprecated: The object property maximum
is deprecated; please use the object property max
instead.
max
使用默认 { "max": 4 }
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the default { "max": 4 }
option:
/*eslint max-depth: ["error", 4]*/
function foo() {
for (;;) { // Nested 1 deep
while (true) { // Nested 2 deep
if (true) { // Nested 3 deep
if (true) { // Nested 4 deep
if (true) { // Nested 5 deep
}
}
}
}
}
}
使用默认 { "max": 4 }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the default { "max": 4 }
option:
/*eslint max-depth: ["error", 4]*/
function foo() {
for (;;) { // Nested 1 deep
while (true) { // Nested 2 deep
if (true) { // Nested 3 deep
if (true) { // Nested 4 deep
}
}
}
}
}
请注意,类静态块不计为嵌套块,并且它们中的深度是与封闭上下文分开计算的。
¥Note that class static blocks do not count as nested blocks, and that the depth in them is calculated separately from the enclosing context.
使用 { "max": 2 }
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with { "max": 2 }
option:
/*eslint max-depth: ["error", 2]*/
function foo() {
if (true) { // Nested 1 deep
class C {
static {
if (true) { // Nested 1 deep
if (true) { // Nested 2 deep
if (true) { // Nested 3 deep
}
}
}
}
}
}
}
使用 { "max": 2 }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with { "max": 2 }
option:
/*eslint max-depth: ["error", 2]*/
function foo() {
if (true) { // Nested 1 deep
class C {
static {
if (true) { // Nested 1 deep
if (true) { // Nested 2 deep
}
}
}
}
}
}
相关规则
版本
此规则是在 ESLint v0.0.9 中引入。