array-callback-return

Require callbacks of array methods such as map and filter to return a value

has suggestions

Methods like map and filter use the callback's return value. If the callback forgets return, they get undefined, so map fills the array with undefined and filter drops every item. A callback that only returns a value on some paths is reported too.

The rule checks a function passed directly to every, filter, find, findIndex, findLast, findLastIndex, flatMap, map, reduce, reduceRight, some, sort, toSorted, Array.from or Array.fromAsync.

Configure#

Off by default. To turn it on:

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

Every option, set to its default:

bunfig.toml
[lint.rules]
"array-callback-return" = ["error", {
  allowImplicit = false,
  checkForEach = false,
  allowVoid = false,
}]

Examples#

example.js1 problem
const names = users.map(user => {
  user.name.trim();
});
example.js1 problem
const adults = users.filter(user => {
  if (user.age >= 18) {
    return true;
  }
});
example.jsNo problems
const names = users.map(user => {
  return user.name.trim();
});

const adults = users.filter(user => user.age >= 18);

Options#

OptionDefaultWhat it does
allowImplicitfalseAllow return; without a value as a way to return undefined.
checkForEachfalseAlso check forEach, but the other way around. A callback that returns a value is reported, because forEach ignores it.
allowVoidfalseWith checkForEach, allow a returned value that starts with void.
example.jswith { "allowImplicit": true }No problems
const first = users.find(user => {
  if (!user.active) {
    return;
  }
  return user.admin;
});
example.jswith { "checkForEach": true }1 problem
users.forEach(user => save(user));
example.jswith { "checkForEach": true }No problems
users.forEach(user => {
  save(user);
});
example.jswith { "checkForEach": true, "allowVoid": true }No problems
users.forEach(user => void save(user));