eslint/max-depth Pedantic ​
What it does ​
Enforce a maximum depth that blocks can be nested. This rule helps to limit the complexity of nested blocks, improving readability and maintainability by ensuring that code does not become too deeply nested.
Why is this bad? ​
Many developers consider code difficult to read if blocks are nested beyond a certain depth. Excessive nesting can make it harder to follow the flow of the code, increasing cognitive load and making maintenance more error-prone. By enforcing a maximum block depth, this rule encourages cleaner, more readable code.
Examples ​
Examples of incorrect code for this rule with the default { "max": 3 }
option:
function foo() {
for (;;) { // Nested 1 deep
while (true) { // Nested 2 deep
if (true) { // Nested 3 deep
if (true) { // Nested 4 deep }
}
}
}
}
Examples of correct code for this rule with the default { "max": 3 }
option:
function foo() {
for (;;) { // Nested 1 deep
while (true) { // Nested 2 deep
if (true) { // Nested 3 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.
Example:
function foo() {
if (true) { // Nested 1 deep
class C {
static {
if (true) { // Nested 1 deep
if (true) { // Nested 2 deep }
}
}
}
}
}
Options ​
max ​
{ type: number, default: 4 }
The max
enforces a maximum depth that blocks can be nested
Example:
"eslint/max-depth": ["error", 4]
"eslint/max-depth": [
"error",
{
max: 4
}
]
How to use ​
To enable this rule in the CLI or using the config file, you can use:
oxlint --deny max-depth
{
"rules": {
"max-depth": "error"
}
}