sindresorhus/eslint-plugin-unicorn

Rule proposal: `no-return-array-push`

Closed

#1,487 opened on Aug 16, 2021

 (1 comment) (5 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

Array#push() returns the new length of the array, most time return array.push(foo) is a mistake.

Fail

function foo() {
	return array.push(bar);
}

Pass

// If mean to return the length
function foo() {
	return array.length + bar.length;
}
// If mean to push and exit
function foo() {
  if (true) {
    array.push(bar);
    return;
  }

  // ...
}
// Have to use `eslint-disable`
function foo() {
    // eslint-disable-next-line unicorn/no-return-array-push
	return array.push(bar);
}

Contributor guide