AbsoluteJS

absolute/max-depth-extended

Disallow too many nested blocks, except when the block only contains an early exit (return or throw).

Code QualitySuggestion
View on GitHub

#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

Incorrect
TS
// options: [1] — two nested if-blocks reach depth 2
function process() {
	if (enabled) {
		if (ready) {
			run();
		}
	}
}
Correct

Guard clauses do not count

TS
// options: [1]
function save() {
	if (!user) return;          // guard clauseearly-exit block is not counted
	if (!user.email) throw new Error('missing email');
	persist(user);
}
TS
function outer() {
	if (ready) {
		function inner() {       // a nested function resets the depth counter
			if (valid) run();
		}
	}
}

#Options

This rule accepts the following options:

OptionTypeDefaultDescription
(positional number)number1Maximum block nesting depth allowed per function before a report. Blocks whose single statement is a return or throw are exempt.
JS
{
	rules: {
		// Allow up to 2 levels of nesting (plus guard-clause early exits)
		'absolute/max-depth-extended': ['warn', 2]
	}
}

#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.

#Resources