# 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`.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

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

Every option, set to its default:

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

{/* End of the generated section. */}

## Examples

Reported:

{/* bad */}
```js
const names = users.map(user => {
  user.name.trim();
});
```

{/* bad */}
```js
const adults = users.filter(user => {
  if (user.age >= 18) {
    return true;
  }
});
```

Not reported:

{/* good */}
```js
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`. |

{/* good {"options": [{"allowImplicit": true}]} */}
```js
const first = users.find(user => {
  if (!user.active) {
    return;
  }
  return user.admin;
});
```

{/* bad {"options": [{"checkForEach": true}]} */}
```js
users.forEach(user => save(user));
```

{/* good {"options": [{"checkForEach": true}]} */}
```js
users.forEach(user => {
  save(user);
});
```

{/* good {"options": [{"checkForEach": true, "allowVoid": true}]} */}
```js
users.forEach(user => void save(user));
```
