Skip to content

Latest commit

Β 

History

History
47 lines (35 loc) Β· 1.09 KB

File metadata and controls

47 lines (35 loc) Β· 1.09 KB

no-useless-recursion

πŸ“ Disallow simple recursive function calls that can be replaced with a loop.

πŸ’ΌπŸš« This rule is enabled in the βœ… recommended config. This rule is disabled in the β˜‘οΈ unopinionated config.

Recursive functions can throw a stack overflow error when they run too deeply. Simple tail-recursive functions are usually clearer as loops in JavaScript.

This rule intentionally only reports direct returned self-calls in named functions. More complex recursion can be useful and is left alone.

Examples

// ❌
function foo(bar) {
	if (bar.baz) {
		return foo(bar.baz);
	}

	return bar;
}
// βœ…
function foo(bar) {
	while (bar.baz) {
		bar = bar.baz;
	}

	return bar;
}
// βœ…
function foo(bar) {
	if (Array.isArray(bar)) {
		return bar.map(baz => foo(baz));
	}

	return bar;
}