array-callback-return
Require callbacks of array methods such as `map` and `filter` to return a value
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#
Reported:
const names = users.map(user => {
user.name.trim();
});const adults = users.filter(user => {
if (user.age >= 18) {
return true;
}
});Not reported:
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. |
const first = users.find(user => {
if (!user.active) {
return;
}
return user.admin;
});users.forEach(user => save(user));users.forEach(user => {
save(user);
});users.forEach(user => void save(user));