oxc/uninvoked-array-callback

Disallow array methods with a callback directly on new Array(n)

new Array(5) has a length of 5 but no elements, only empty slots. map, forEach, filter, and most other array methods skip empty slots, so the callback never runs. new Array(5).map(() => 0) gives back five empty slots, not five zeros.

The rule reports a method call directly on new Array(n), where n is a number written in the code and the first argument is a function written in the call. It doesn't look at the method name. find() and findIndex(), which do visit empty slots, are reported too, and so is fill(() => 0).

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"oxc/uninvoked-array-callback" = "error" # or "warn"

Examples#

example.js1 problem
const scores = new Array(5).map(() => 0);
example.js1 problem
new Array(3).forEach(function () {
  addRow();
});
example.jsNo problems
const scores = Array.from({ length: 5 }, () => 0);

fill() puts a value in every slot, so the methods after it see all of them:

example.jsNo problems
const indexes = new Array(5).fill(0).map((_, index) => index);
example.jsNo problems
for (let row = 0; row < 3; row++) {
  addRow();
}