Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allowVoid option in array-callback-return #17564

Merged
merged 4 commits into from Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion docs/src/rules/array-callback-return.md
Expand Up @@ -92,10 +92,13 @@ var bar = foo.map(node => node.getAttribute("id"));

## Options

This rule accepts a configuration object with two options:
This rule accepts a configuration object with three options:

* `"allowImplicit": false` (default) When set to `true`, allows callbacks of methods that require a return value to implicitly return `undefined` with a `return` statement containing no expression.
* `"checkForEach": false` (default) When set to `true`, rule will also report `forEach` callbacks that return a value.
* `"allowVoid": false` (default) When set to `true`, allows `void` in `forEach` callbacks, so rule will not report the return value with a `void` operator.

**Note-** `{ "allowVoid": true }` works only if `checkForEach` option is set to `true`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
**Note-** `{ "allowVoid": true }` works only if `checkForEach` option is set to `true`.
**Note:** `{ "allowVoid": true }` works only if `checkForEach` option is set to `true`.


### allowImplicit

Expand Down Expand Up @@ -171,6 +174,20 @@ myArray.forEach(item => {

:::

### allowVoid

Examples of **correct** code for the `{ "allowVoid": true }` option:

:::correct

```js
/*eslint array-callback-return: ["error", { checkForEach: true, allowVoid: true }]*/

myArray.forEach(item => void handleItem(item));
```

:::

## Known Limitations

This rule checks callback functions of methods with the given names, *even if* the object which has the method is *not* an array.
Expand Down
14 changes: 6 additions & 8 deletions lib/rules/array-callback-return.js
Expand Up @@ -213,15 +213,13 @@ module.exports = {

if (funcInfo.arrayMethodName === "forEach") {
if (options.checkForEach && node.type === "ArrowFunctionExpression" && node.expression) {
messageId = "expectedNoReturnValue";
}
if (options.allowVoid &&
node.body.type === "UnaryExpression" &&
node.body.operator === "void") {
return;
}

if (options.checkForEach &&
options.allowVoid &&
node.type === "ArrowFunctionExpression" &&
node.body.type === "UnaryExpression" &&
node.body.operator === "void") {
return;
messageId = "expectedNoReturnValue";
}
} else {
if (node.body.type === "BlockStatement" && isAnySegmentReachable(funcInfo.currentSegments)) {
Expand Down