absolute/max-depth-extended
Disallow too many nested blocks, except when the block only contains an early exit (return or throw).
#Rule Details
A drop-in replacement for ESLint’s core max-depth with one important escape hatch: a nested block is not counted when its only statement is an early exit (return or throw). That keeps guard clauses — the recommended way to flatten code — from being penalized while still discouraging genuinely deep nesting.
Depth is tracked per function: entering any function resets the counter, so a deeply nested callback does not inherit its parent’s depth. The function’s own body block is free; each additional non-guard block adds one level, and exceeding the configured maximum is reported.
#Examples
// options: [1] — two nested if-blocks reach depth 2
function process() {
if (enabled) {
if (ready) {
run();
}
}
}Guard clauses do not count
// options: [1]
function save() {
if (!user) return; // guard clause — early-exit block is not counted
if (!user.email) throw new Error('missing email');
persist(user);
}function outer() {
if (ready) {
function inner() { // a nested function resets the depth counter
if (valid) run();
}
}
}#Options
This rule accepts the following options:
| Option | Type | Default | Description |
|---|---|---|---|
(positional number) | number | 1 | Maximum block nesting depth allowed per function before a report. Blocks whose single statement is a return or throw are exempt. |
{
rules: {
// Allow up to 2 levels of nesting (plus guard-clause early exits)
'absolute/max-depth-extended': ['warn', 2]
}
}#Related Rules
#When Not To Use It
If you already use the core max-depth rule and do not want guard-clause exemptions, prefer that rule instead. Algorithms that are genuinely tree- or matrix-shaped may also need a higher limit rather than this rule.