Skip to content

Commit

Permalink
no-useless-undefined: Add checkArrowFunctionBody option (#2232)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
fisker and sindresorhus committed Dec 20, 2023
1 parent 6708a30 commit 9d7048c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
25 changes: 24 additions & 1 deletion docs/rules/no-useless-undefined.md
Expand Up @@ -9,7 +9,9 @@

`undefined` is the default value for new variables, parameters, return statements, etc… so specifying it doesn't make any difference.

The only case where passing `undefined` is required is due to bad TypeScript types in functions, in which case you can use `checkArguments: false` option.
Where passing `undefined` as argument is required is due to bad TypeScript types in functions, in which case you can use `checkArguments: false` option.

Using `undefined` as arrow function body sometimes make the purpose more explicit. You can use the `checkArrowFunctionBody: false` option to allow this.

## Fail

Expand Down Expand Up @@ -116,6 +118,27 @@ foo(bar, baz, undefined);
foo(bar, baz, undefined);
```

### checkArrowFunctionBody

Type: `boolean`\
Default: `true`

Disallow the use of `undefined` as arrow function body. Pass `checkArrowFunctionBody: false` to disable checking them.

#### Fail

```js
// eslint unicorn/no-useless-undefined: ["error", {"checkArrowFunctionBody": true}]
const foo = () => undefined;
```

#### Pass

```js
// eslint unicorn/no-useless-undefined: ["error", {"checkArrowFunctionBody": false}]
const foo = () => undefined;
```

## Conflict with ESLint `array-callback-return` rule

We recommend setting the ESLint [`array-callback-return`](https://eslint.org/docs/rules/array-callback-return#top) rule option [`allowImplicit`](https://eslint.org/docs/rules/array-callback-return#options) to `true`:
Expand Down
32 changes: 19 additions & 13 deletions rules/no-useless-undefined.js
Expand Up @@ -106,6 +106,7 @@ const create = context => {

const options = {
checkArguments: true,
checkArrowFunctionBody: true,
...context.options[0],
};

Expand Down Expand Up @@ -143,19 +144,21 @@ const create = context => {
});

// `() => undefined`
context.on('Identifier', node => {
if (
isUndefined(node)
&& node.parent.type === 'ArrowFunctionExpression'
&& node.parent.body === node
) {
return getProblem(
node,
fixer => replaceNodeOrTokenAndSpacesBefore(node, ' {}', fixer, sourceCode),
/* CheckFunctionReturnType */ true,
);
}
});
if (options.checkArrowFunctionBody) {
context.on('Identifier', node => {
if (
isUndefined(node)
&& node.parent.type === 'ArrowFunctionExpression'
&& node.parent.body === node
) {
return getProblem(
node,
fixer => replaceNodeOrTokenAndSpacesBefore(node, ' {}', fixer, sourceCode),
/* CheckFunctionReturnType */ true,
);
}
});
}

// `let foo = undefined` / `var foo = undefined`
context.on('Identifier', node => {
Expand Down Expand Up @@ -276,6 +279,9 @@ const schema = [
checkArguments: {
type: 'boolean',
},
checkArrowFunctionBody: {
type: 'boolean',
},
},
},
];
Expand Down
7 changes: 7 additions & 0 deletions test/no-useless-undefined.mjs
Expand Up @@ -7,6 +7,7 @@ const messageId = 'no-useless-undefined';

const errors = [{messageId}];
const optionsIgnoreArguments = [{checkArguments: false}];
const optionsIgnoreArrowFunctionBody = [{checkArrowFunctionBody: false}];

test({
valid: [
Expand Down Expand Up @@ -77,6 +78,12 @@ test({
code: 'foo.bind(undefined);',
options: optionsIgnoreArguments,
},

// `checkArrowFunctionBody: false`
{
code: 'const foo = () => undefined',
options: optionsIgnoreArrowFunctionBody,
},
],
invalid: [
{
Expand Down

0 comments on commit 9d7048c

Please sign in to comment.