noReturnArrayPush (JavaScript)
Summary
Section titled βSummaryβ- Rule available since:
v2.6.4 - Diagnostic Category:
lint/nursery/noReturnArrayPush - This rule has an unsafe fix.
- The default severity of this rule is warning.
- Sources:
- Same as
unicorn/no-return-array-push
- Same as
How to configure
Section titled βHow to configureβ{ "linter": { "rules": { "nursery": { "noReturnArrayPush": "error" } } }}Description
Section titled βDescriptionβDisallow using the return value of Array.prototype.push() and Array.prototype.unshift().
push() and unshift() add items to an array and return the new length of the array.
They donβt return the added item or the array itself.
Returning or storing this length is almost always a mistake, because the code reads as if
the result were meaningful when it says nothing about what was added.
To add an item and stop the function, call push() or unshift() before return.
If you need the new length, read the length property of the array after adding the item.
The rule only reports calls with at least one argument.
It ignores calls whose result is discarded with the void operator, such as void items.push(item),
and calls whose result is accessed with . or [], such as router.push(to).catch(handleError),
because this usually means push() is a custom method that returns something other than a length.
The rule also ignores push() calls on objects that are commonly not arrays:
stream, router, this, this.stream, this.router, this.$router,
process.stdin, process.stdout, and process.stderr.
It ignores push() and unshift() calls on values that are written in a way that shows they
arenβt arrays: objects ({}), functions, classes, template strings, and new expressions
other than new Array().
Examples
Section titled βExamplesβInvalid
Section titled βInvalidβfunction add(item) { return items.push(item);}code-block.js:2:18 lint/nursery/noReturnArrayPush FIXABLE ββββββββββββββββββββββββββββββββββββββββ
β The return value of push() is used here.
1 β function add(item) {
> 2 β return items.push(item);
β ^^^^
3 β }
4 β
βΉ push() returns the new length of the array, not the added item or the array. Using this length is usually a mistake.
βΉ Call push() as its own statement. If you need the new length, read the length property of the array afterward.
βΉ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
βΉ Unsafe fix: Move the call before the return statement.
1 1 β function add(item) {
2 β - Β·Β·Β·Β·returnΒ·items.push(item);
2 β + Β·Β·Β·Β·items.push(item);Β·return;
3 3 β }
4 4 β
const add = (item) => items.push(item);code-block.js:1:29 lint/nursery/noReturnArrayPush ββββββββββββββββββββββββββββββββββββββββββββββββββ
β The return value of push() is used here.
> 1 β const add = (item) => items.push(item);
β ^^^^
2 β
βΉ push() returns the new length of the array, not the added item or the array. Using this length is usually a mistake.
βΉ Call push() as its own statement. If you need the new length, read the length property of the array afterward.
βΉ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
const length = items.unshift(item);code-block.js:1:22 lint/nursery/noReturnArrayPush ββββββββββββββββββββββββββββββββββββββββββββββββββ
β The return value of unshift() is used here.
> 1 β const length = items.unshift(item);
β ^^^^^^^
2 β
βΉ unshift() returns the new length of the array, not the added item or the array. Using this length is usually a mistake.
βΉ Call unshift() as its own statement. If you need the new length, read the length property of the array afterward.
βΉ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
An arrow function without braces returns its body, even when the caller ignores the result:
source.forEach((item) => target.push(item));code-block.js:1:33 lint/nursery/noReturnArrayPush ββββββββββββββββββββββββββββββββββββββββββββββββββ
β The return value of push() is used here.
> 1 β source.forEach((item) => target.push(item));
β ^^^^
2 β
βΉ push() returns the new length of the array, not the added item or the array. Using this length is usually a mistake.
βΉ Call push() as its own statement. If you need the new length, read the length property of the array afterward.
βΉ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
function add(item) { items.push(item); return;}
const addItem = (item) => { items.push(item);};
items.unshift(item);const length = items.length;
function getNextLength(item) { items.push(item); return items.length;}
target.push(...source);
void items.push(item);Related links
Section titled βRelated linksβCopyright (c) 2023-present Biome Developers and Contributors.