noNewArray (JavaScript)
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/noNewArray - This rule has a safe fix.
- The default severity of this rule is information.
- Sources:
- Inspired from
unicorn/no-new-array
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noNewArray": "error" } } }}Description
Section titled “Description”Disallow new Array() with a single argument.
new Array() with one argument does two different things depending on the argument.
If the argument is a number, it creates an empty array of that length.
Otherwise, it creates an array that holds the argument as its only item.
Because of this, readers can’t tell from the code alone which one was intended.
Use an array literal, such as [value], to create an array that holds a value.
Use Array.from({ length }) to create an array of a given length.
Note that new Array(length) creates an array of empty slots,
while Array.from({ length }) creates an array filled with undefined.
Some array methods, such as forEach() and filter(), skip empty slots.
When the argument is never a number, such as a string or an object, Biome replaces the call with an array literal. Biome doesn’t offer a fix when the call has comments that the fix would remove.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”const length = 10;const array = new Array(length);code-block.js:2:15 lint/nursery/noNewArray ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This new Array() call has a single argument, so its result depends on whether the argument is a number.
1 │ const length = 10;
> 2 │ const array = new Array(length);
│ ^^^^^^^^^^^^^^^^^
3 │
ℹ With a number, new Array() creates an empty array of that length. With any other value, it creates an array that holds the value.
ℹ Use an array literal such as [value] to create an array that holds the value, or Array.from({ length }) to create an array of a given length.
ℹ 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 array = new Array("value");code-block.js:1:15 lint/nursery/noNewArray FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This new Array() call has a single argument, so its result depends on whether the argument is a number.
> 1 │ const array = new Array("value");
│ ^^^^^^^^^^^^^^^^^^
2 │
ℹ With a number, new Array() creates an empty array of that length. With any other value, it creates an array that holds the value.
ℹ Use an array literal such as [value] to create an array that holds the value, or Array.from({ length }) to create an array of a given length.
ℹ 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.
ℹ Safe fix: Use an array literal.
1 │ - const·array·=·new·Array("value");
1 │ + const·array·=·["value"];
2 2 │
const length = 10;const empty = Array.from({ length });const array = ["value"];const pair = new Array(1, 2);See Also
Section titled “See Also”- To use array literals instead of
Array()with no arguments, several arguments, or a spread argument such asnew Array(...items), seeuseArrayLiterals.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.