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 =>Array.prototype.map() expects a return value from arrow function. {
user.name.trim();
});example.js1 problem
const adults = users.filter(user =>Array.prototype.filter() expects a value to be returned at the end of arrow function. {
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#
| Option | Default | What it does |
|---|---|---|
allowImplicit | false | Allow return; without a value as a way to return undefined. |
checkForEach | false | Also check forEach, but the other way around. A callback that returns a value is reported, because forEach ignores it. |
allowVoid | false | With checkForEach, allow a returned value that starts with void. |
example.jswith
{ "allowImplicit": true }No problemsconst first = users.find(user => {
if (!user.active) {
return;
}
return user.admin;
});example.jswith
{ "checkForEach": true }1 problemusers.forEach(user =>Array.prototype.forEach() expects no useless return value from arrow function. save(user));example.jswith
{ "checkForEach": true }No problemsusers.forEach(user => {
save(user);
});example.jswith
{ "checkForEach": true, "allowVoid": true }No problemsusers.forEach(user => void save(user));