max-depth
强制块可以嵌套的最大深度
许多开发者认为,如果块嵌套超过一定深度,代码就难以阅读。
规则详情
此规则强制块可以嵌套的最大深度,以降低代码复杂性。
选项
此规则有一个数字或对象选项
"max"
(默认4
)强制块可以嵌套的最大深度
已弃用: 对象属性 maximum
已弃用;请改用对象属性 max
。
max
对于此规则,使用默认 { "max": 4 }
选项的错误代码示例
在 Playground 中打开
/*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
}
}
}
}
}
对于此规则,使用默认 { "max": 4 }
选项的正确代码示例
在 Playground 中打开
/*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
}
}
}
}
}
请注意,类静态块不计为嵌套块,并且其中的深度与封闭上下文分开计算。
对于此规则,使用 { "max": 2 }
选项的错误代码示例
在 Playground 中打开
/*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
}
}
}
}
}
}
对于此规则,使用 { "max": 2 }
选项的正确代码示例
在 Playground 中打开
/*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 中引入。