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

no-useless-undefined: Add checkArrowFunctionBody option #2232

Merged
merged 5 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion docs/rules/no-useless-undefined.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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