noUninvokedArrayCallback (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.6 - Diagnostic Category:
lint/nursery/noUninvokedArrayCallback - This rule doesn’t have a fix.
- The default severity of this rule is error.
- Sources:
- Inspired from
oxc/uninvoked-array-callback
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noUninvokedArrayCallback": "error" } } }}Description
Section titled “Description”Disallow array methods with callbacks on arrays that only have empty slots.
Calling new Array(5) or Array(5) with a single number doesn’t create an array of five
undefined values. It creates an array with a length of 5 and five empty slots, which
hold no value at all. Methods such as map(), filter(), and forEach() skip empty slots,
so the callback passed to them never runs. For example, new Array(3).map(() => 0) gives
an array with three empty slots, not [0, 0, 0].
This rule reports calls to every(), filter(), flatMap(), forEach(), map(),
reduce(), reduceRight(), some(), sort(), and toSorted() with a callback, directly
on an array created by calling Array with a single number.
To run the callback for every slot, fill the array first with fill(), or create it with
Array.from({ length: 5 }, callback).
Examples
Section titled “Examples”Invalid
Section titled “Invalid”const list = new Array(5).map(() => createElement());code-block.js:1:27 lint/nursery/noUninvokedArrayCallback ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The callback passed to map() is never called.
> 1 │ const list = new Array(5).map(() => createElement());
│ ^^^
2 │
ℹ This array only has empty slots.
> 1 │ const list = new Array(5).map(() => createElement());
│ ^^^^^^^^^^^^
2 │
ℹ Calling Array with a single number creates an array with that many empty slots instead of undefined values. map() doesn't call its callback for empty slots.
ℹ Fill the array first, for example with new Array(length).fill(), or create it with Array.from({ length }, callback).
ℹ 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 list = Array(5).filter(function (item) { return item !== undefined;});code-block.js:1:23 lint/nursery/noUninvokedArrayCallback ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The callback passed to filter() is never called.
> 1 │ const list = Array(5).filter(function (item) {
│ ^^^^^^
2 │ return item !== undefined;
3 │ });
ℹ This array only has empty slots.
> 1 │ const list = Array(5).filter(function (item) {
│ ^^^^^^^^
2 │ return item !== undefined;
3 │ });
ℹ Calling Array with a single number creates an array with that many empty slots instead of undefined values. filter() doesn't call its callback for empty slots.
ℹ Fill the array first, for example with new Array(length).fill(), or create it with Array.from({ length }, callback).
ℹ 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.
new Array(5).forEach((_, index) => console.log(index));code-block.js:1:14 lint/nursery/noUninvokedArrayCallback ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The callback passed to forEach() is never called.
> 1 │ new Array(5).forEach((_, index) => console.log(index));
│ ^^^^^^^
2 │
ℹ This array only has empty slots.
> 1 │ new Array(5).forEach((_, index) => console.log(index));
│ ^^^^^^^^^^^^
2 │
ℹ Calling Array with a single number creates an array with that many empty slots instead of undefined values. forEach() doesn't call its callback for empty slots.
ℹ Fill the array first, for example with new Array(length).fill(), or create it with Array.from({ length }, callback).
ℹ 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 list = new Array(5).fill().map(() => createElement());const items = Array.from({ length: 5 }, () => createElement());const values = new Array(1, 2, 3).map((value) => value * 2);const index = new Array(5).findIndex((item) => item === undefined);Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.