π 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.
// β
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;
}