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 }
}
}
}
}
}Configuration โ
This rule accepts a configuration object with the following properties:
max โ
type: integer
default: 4
The max enforces a maximum depth that blocks can be nested
How to use โ
To enable this rule using the config file or in the CLI, you can use:
{
"rules": {
"max-depth": "error"
}
}import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"max-depth": "error",
},
});oxlint --deny max-depthVersion โ
This rule was added in v0.15.12.
