sindresorhus/eslint-plugin-unicorn

Rule proposal: `no-useless-recursion`

Closed

#1,078 opened on Jan 27, 2021

 (7 comments) (0 reactions) (0 assignees)JavaScript (468 forks)user submission
help wantednew rule

Repository metrics

Stars
 (5,022 stars)
PR merge metrics
 (Avg merge 1d 16h) (399 merged PRs in 30d)

Description

Disallow simple recursive function.

Fail

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

  return bar;
}

Pass

function foo(bar) {
  while (bar.baz) {
    bar = bar.baz:
  }

  return bar;
}
function foo(bar) {
  for (; bar.baz; bar = bar.baz) {}

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

  return bar;
}

I'm not sure how to define "simple".

Contributor guide