noArrayMethodsOnArguments (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.6 - Diagnostic Category:
lint/nursery/noArrayMethodsOnArguments - This rule doesn’t have a fix.
- The default severity of this rule is error.
- Sources:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noArrayMethodsOnArguments": "error" } } }}Description
Section titled “Description”Disallow calling array methods on the arguments object.
Inside a regular function, arguments holds the values the function was called with.
It has a length and its values can be read by index, like an array, but it isn’t an array.
It doesn’t have array methods such as map(), filter(), or reduce(),
so calling one of them throws a TypeError when the code runs.
Declare a rest parameter, such as ...args, to receive the values as a real array.
If you can’t change the function’s parameters, convert arguments to an array first,
for example with Array.from(arguments).
Examples
Section titled “Examples”Invalid
Section titled “Invalid”function sum() { return arguments.reduce((total, value) => total + value, 0);}code-block.js:2:12 lint/nursery/noArrayMethodsOnArguments ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The arguments object doesn't have a reduce() method.
1 │ function sum() {
> 2 │ return arguments.reduce((total, value) => total + value, 0);
│ ^^^^^^^^^^^^^^^^
3 │ }
4 │
ℹ arguments looks like an array, but it isn't one, so this call throws a TypeError when the code runs.
ℹ Declare a rest parameter, such as ...args, and use it instead, or convert arguments to an array with Array.from(arguments).
ℹ 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 first() { return arguments["at"](0);}code-block.js:2:12 lint/nursery/noArrayMethodsOnArguments ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The arguments object doesn't have a at() method.
1 │ function first() {
> 2 │ return arguments["at"](0);
│ ^^^^^^^^^^^^^^^
3 │ }
4 │
ℹ arguments looks like an array, but it isn't one, so this call throws a TypeError when the code runs.
ℹ Declare a rest parameter, such as ...args, and use it instead, or convert arguments to an array with Array.from(arguments).
ℹ 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 sum(...values) { return values.reduce((total, value) => total + value, 0);}
function first() { return Array.from(arguments).at(0);}
function list() { return Array.prototype.slice.call(arguments);}Outside strict mode, you can name a parameter or variable arguments.
When it has a value, it replaces the arguments object, so the rule ignores it:
function count(arguments) { return arguments.filter(Boolean).length;}A var arguments; declaration without a value doesn’t replace the arguments object,
so the rule still reports it:
function count() { var arguments; return arguments.filter(Boolean).length;}code-block.cjs:3:12 lint/nursery/noArrayMethodsOnArguments ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The arguments object doesn't have a filter() method.
1 │ function count() {
2 │ var arguments;
> 3 │ return arguments.filter(Boolean).length;
│ ^^^^^^^^^^^^^^^^
4 │ }
5 │
ℹ arguments looks like an array, but it isn't one, so this call throws a TypeError when the code runs.
ℹ Declare a rest parameter, such as ...args, and use it instead, or convert arguments to an array with Array.from(arguments).
ℹ 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.
See Also
Section titled “See Also”- To disallow the use of
argumentsentirely, seenoArguments.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.