Skip to content

Commit 9d7048c

Browse files
fiskersindresorhus
andauthoredDec 20, 2023
no-useless-undefined: Add checkArrowFunctionBody option (#2232)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 6708a30 commit 9d7048c

File tree

3 files changed

+50
-14
lines changed

3 files changed

+50
-14
lines changed
 

‎docs/rules/no-useless-undefined.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

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

12-
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.
12+
Where passing `undefined` as argument is required is due to bad TypeScript types in functions, in which case you can use `checkArguments: false` option.
13+
14+
Using `undefined` as arrow function body sometimes make the purpose more explicit. You can use the `checkArrowFunctionBody: false` option to allow this.
1315

1416
## Fail
1517

@@ -116,6 +118,27 @@ foo(bar, baz, undefined);
116118
foo(bar, baz, undefined);
117119
```
118120

121+
### checkArrowFunctionBody
122+
123+
Type: `boolean`\
124+
Default: `true`
125+
126+
Disallow the use of `undefined` as arrow function body. Pass `checkArrowFunctionBody: false` to disable checking them.
127+
128+
#### Fail
129+
130+
```js
131+
// eslint unicorn/no-useless-undefined: ["error", {"checkArrowFunctionBody": true}]
132+
const foo = () => undefined;
133+
```
134+
135+
#### Pass
136+
137+
```js
138+
// eslint unicorn/no-useless-undefined: ["error", {"checkArrowFunctionBody": false}]
139+
const foo = () => undefined;
140+
```
141+
119142
## Conflict with ESLint `array-callback-return` rule
120143

121144
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`:

‎rules/no-useless-undefined.js

+19-13
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ const create = context => {
106106

107107
const options = {
108108
checkArguments: true,
109+
checkArrowFunctionBody: true,
109110
...context.options[0],
110111
};
111112

@@ -143,19 +144,21 @@ const create = context => {
143144
});
144145

145146
// `() => undefined`
146-
context.on('Identifier', node => {
147-
if (
148-
isUndefined(node)
149-
&& node.parent.type === 'ArrowFunctionExpression'
150-
&& node.parent.body === node
151-
) {
152-
return getProblem(
153-
node,
154-
fixer => replaceNodeOrTokenAndSpacesBefore(node, ' {}', fixer, sourceCode),
155-
/* CheckFunctionReturnType */ true,
156-
);
157-
}
158-
});
147+
if (options.checkArrowFunctionBody) {
148+
context.on('Identifier', node => {
149+
if (
150+
isUndefined(node)
151+
&& node.parent.type === 'ArrowFunctionExpression'
152+
&& node.parent.body === node
153+
) {
154+
return getProblem(
155+
node,
156+
fixer => replaceNodeOrTokenAndSpacesBefore(node, ' {}', fixer, sourceCode),
157+
/* CheckFunctionReturnType */ true,
158+
);
159+
}
160+
});
161+
}
159162

160163
// `let foo = undefined` / `var foo = undefined`
161164
context.on('Identifier', node => {
@@ -276,6 +279,9 @@ const schema = [
276279
checkArguments: {
277280
type: 'boolean',
278281
},
282+
checkArrowFunctionBody: {
283+
type: 'boolean',
284+
},
279285
},
280286
},
281287
];

‎test/no-useless-undefined.mjs

+7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const messageId = 'no-useless-undefined';
77

88
const errors = [{messageId}];
99
const optionsIgnoreArguments = [{checkArguments: false}];
10+
const optionsIgnoreArrowFunctionBody = [{checkArrowFunctionBody: false}];
1011

1112
test({
1213
valid: [
@@ -77,6 +78,12 @@ test({
7778
code: 'foo.bind(undefined);',
7879
options: optionsIgnoreArguments,
7980
},
81+
82+
// `checkArrowFunctionBody: false`
83+
{
84+
code: 'const foo = () => undefined',
85+
options: optionsIgnoreArrowFunctionBody,
86+
},
8087
],
8188
invalid: [
8289
{

0 commit comments

Comments
 (0)
Please sign in to comment.