From 9d7048c0d776bf623349a1c2f235a6fc84c42d66 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 20 Dec 2023 19:54:03 +0800 Subject: [PATCH] `no-useless-undefined`: Add `checkArrowFunctionBody` option (#2232) Co-authored-by: Sindre Sorhus --- docs/rules/no-useless-undefined.md | 25 ++++++++++++++++++++++- rules/no-useless-undefined.js | 32 ++++++++++++++++++------------ test/no-useless-undefined.mjs | 7 +++++++ 3 files changed, 50 insertions(+), 14 deletions(-) diff --git a/docs/rules/no-useless-undefined.md b/docs/rules/no-useless-undefined.md index 95855daf36..9ea6680af8 100644 --- a/docs/rules/no-useless-undefined.md +++ b/docs/rules/no-useless-undefined.md @@ -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 @@ -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`: diff --git a/rules/no-useless-undefined.js b/rules/no-useless-undefined.js index 2a4b4f13f8..3aba0e8b2a 100644 --- a/rules/no-useless-undefined.js +++ b/rules/no-useless-undefined.js @@ -106,6 +106,7 @@ const create = context => { const options = { checkArguments: true, + checkArrowFunctionBody: true, ...context.options[0], }; @@ -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 => { @@ -276,6 +279,9 @@ const schema = [ checkArguments: { type: 'boolean', }, + checkArrowFunctionBody: { + type: 'boolean', + }, }, }, ]; diff --git a/test/no-useless-undefined.mjs b/test/no-useless-undefined.mjs index b55756aa47..110099c05f 100644 --- a/test/no-useless-undefined.mjs +++ b/test/no-useless-undefined.mjs @@ -7,6 +7,7 @@ const messageId = 'no-useless-undefined'; const errors = [{messageId}]; const optionsIgnoreArguments = [{checkArguments: false}]; +const optionsIgnoreArrowFunctionBody = [{checkArrowFunctionBody: false}]; test({ valid: [ @@ -77,6 +78,12 @@ test({ code: 'foo.bind(undefined);', options: optionsIgnoreArguments, }, + + // `checkArrowFunctionBody: false` + { + code: 'const foo = () => undefined', + options: optionsIgnoreArrowFunctionBody, + }, ], invalid: [ {