sindresorhus/eslint-plugin-unicorn

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

Geschlossen

#1.487 geöffnet am 16.08.2021

 (1 Kommentar) (5 Reaktionen) (0 zugewiesene Personen)JavaScript (468 Forks)user submission
help wantednew rule

Repository-Metriken

Stars
 (5.022 Sterne)
PR-Merge-Metriken
 (Durchschn. Merge 4h 30m) (26 gemergte PRs in 30 T)

Beschreibung

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