From d2a0bd363a1cd3f467578d4a131bc52b74676912 Mon Sep 17 00:00:00 2001 From: fisker Date: Tue, 20 Oct 2020 16:01:50 +0800 Subject: [PATCH 1/8] `no-useless-undefined`: Add `ignoreArguments` option --- docs/rules/no-useless-undefined.md | 25 +++++++++++++++++++++++++ rules/no-useless-undefined.js | 26 ++++++++++++++++++++++++-- test/no-useless-undefined.js | 9 ++++++++- 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/docs/rules/no-useless-undefined.md b/docs/rules/no-useless-undefined.md index 07d39eba3b..124cb8e5fc 100644 --- a/docs/rules/no-useless-undefined.md +++ b/docs/rules/no-useless-undefined.md @@ -82,6 +82,31 @@ function foo({bar}) { foo(); ``` +## Options + +Type: `object` + +### ignoreArguments + +Type: `boolean`
+Default: `false` + +Pass `"ignoreArguments": true` to disable check on function call. + +#### Fail + +```js +// eslint unicorn/no-useless-undefined: ["error", {ignoreArguments: false}] +foo(undefined); +``` + +#### Pass + +```js +// eslint unicorn/no-useless-undefined: ["error", {ignoreArguments: true}] +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 450741843f..a4843378a3 100644 --- a/rules/no-useless-undefined.js +++ b/rules/no-useless-undefined.js @@ -82,6 +82,10 @@ const create = context => { }; const code = context.getSourceCode().text; + const options = { + ignoreArguments: false, + ...context.options[0] + }; const removeNodeAndLeadingSpace = (node, fixer) => { const textBefore = code.slice(0, node.range[0]); @@ -91,7 +95,7 @@ const create = context => { ]); }; - return { + const listeners = { [returnSelector]: listener(removeNodeAndLeadingSpace), [yieldSelector]: listener(removeNodeAndLeadingSpace), [arrowFunctionSelector]: listener( @@ -103,7 +107,10 @@ const create = context => { [assignmentPatternSelector]: listener( (node, fixer) => fixer.removeRange([node.parent.left.range[1], node.range[1]]) ), - CallExpression: node => { + }; + + if (!options.ignoreArguments) { + listeners.CallExpression = node => { if (isCompareFunction(node.callee)) { return; } @@ -153,8 +160,22 @@ const create = context => { }); } }; + + return listeners; }; +const schema = [ + { + type: 'object', + properties: { + ignoreArguments: { + type: 'boolean' + }, + }, + additionalProperties: false, + } +]; + module.exports = { create, meta: { @@ -163,6 +184,7 @@ module.exports = { url: getDocumentationUrl(__filename) }, messages, + schema, fixable: 'code' } }; diff --git a/test/no-useless-undefined.js b/test/no-useless-undefined.js index 8e4acde656..b154e3d081 100644 --- a/test/no-useless-undefined.js +++ b/test/no-useless-undefined.js @@ -4,6 +4,7 @@ import {test} from './utils/test'; const messageId = 'no-useless-undefined'; const errors = [{messageId}]; +const optionsIgnoreArguments = [{ignoreArguments: true}]; test({ valid: [ @@ -40,7 +41,13 @@ test({ 't.same(foo, undefined)', 't.notSame(foo, undefined)', 't.strictSame(foo, undefined)', - 't.strictNotSame(foo, undefined)' + 't.strictNotSame(foo, undefined)', + + // `ignoreArguments: true` + { + code: 'foo(undefined, undefined);', + options: optionsIgnoreArguments, + }, ], invalid: [ { From 8d09a63917ec3ad62d8d96533796e6de6c491d8d Mon Sep 17 00:00:00 2001 From: fisker Date: Tue, 20 Oct 2020 16:07:59 +0800 Subject: [PATCH 2/8] Fix code style --- rules/no-useless-undefined.js | 10 +++++----- test/no-useless-undefined.js | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/rules/no-useless-undefined.js b/rules/no-useless-undefined.js index a4843378a3..1eeb67b170 100644 --- a/rules/no-useless-undefined.js +++ b/rules/no-useless-undefined.js @@ -106,7 +106,7 @@ const create = context => { ), [assignmentPatternSelector]: listener( (node, fixer) => fixer.removeRange([node.parent.left.range[1], node.range[1]]) - ), + ) }; if (!options.ignoreArguments) { @@ -158,8 +158,8 @@ const create = context => { return fixer.removeRange([start, end]); } }); - } - }; + }; + } return listeners; }; @@ -170,9 +170,9 @@ const schema = [ properties: { ignoreArguments: { type: 'boolean' - }, + } }, - additionalProperties: false, + additionalProperties: false } ]; diff --git a/test/no-useless-undefined.js b/test/no-useless-undefined.js index b154e3d081..2d38a4ea3b 100644 --- a/test/no-useless-undefined.js +++ b/test/no-useless-undefined.js @@ -47,7 +47,7 @@ test({ { code: 'foo(undefined, undefined);', options: optionsIgnoreArguments, - }, + } ], invalid: [ { From 0aa9b3ddff565a0bfe6cc4b15610cc985e37f282 Mon Sep 17 00:00:00 2001 From: fisker Date: Tue, 20 Oct 2020 16:10:00 +0800 Subject: [PATCH 3/8] Fix code style --- test/no-useless-undefined.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/no-useless-undefined.js b/test/no-useless-undefined.js index 2d38a4ea3b..c6c91636fa 100644 --- a/test/no-useless-undefined.js +++ b/test/no-useless-undefined.js @@ -46,7 +46,7 @@ test({ // `ignoreArguments: true` { code: 'foo(undefined, undefined);', - options: optionsIgnoreArguments, + options: optionsIgnoreArguments } ], invalid: [ From 803d979d1f03f7f19a0b6086bc61edf0ec7b8afa Mon Sep 17 00:00:00 2001 From: fisker Date: Tue, 20 Oct 2020 21:30:15 +0800 Subject: [PATCH 4/8] `ignoreArguments` -> `checkArguments` --- docs/rules/no-useless-undefined.md | 12 ++++++------ rules/no-useless-undefined.js | 6 +++--- test/no-useless-undefined.js | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/rules/no-useless-undefined.md b/docs/rules/no-useless-undefined.md index 124cb8e5fc..29d40812b3 100644 --- a/docs/rules/no-useless-undefined.md +++ b/docs/rules/no-useless-undefined.md @@ -86,24 +86,24 @@ foo(); Type: `object` -### ignoreArguments +### checkArguments -Type: `boolean`
-Default: `false` +Type: `boolean`\ +Default: `true` -Pass `"ignoreArguments": true` to disable check on function call. +Pass `checkArguments: false` to disable check on function call. #### Fail ```js -// eslint unicorn/no-useless-undefined: ["error", {ignoreArguments: false}] +// eslint unicorn/no-useless-undefined: ["error", {checkArguments: true}] foo(undefined); ``` #### Pass ```js -// eslint unicorn/no-useless-undefined: ["error", {ignoreArguments: true}] +// eslint unicorn/no-useless-undefined: ["error", {checkArguments: false}] foo(undefined); ``` diff --git a/rules/no-useless-undefined.js b/rules/no-useless-undefined.js index 1eeb67b170..08e671bb83 100644 --- a/rules/no-useless-undefined.js +++ b/rules/no-useless-undefined.js @@ -83,7 +83,7 @@ const create = context => { const code = context.getSourceCode().text; const options = { - ignoreArguments: false, + checkArguments: true, ...context.options[0] }; @@ -109,7 +109,7 @@ const create = context => { ) }; - if (!options.ignoreArguments) { + if (options.checkArguments) { listeners.CallExpression = node => { if (isCompareFunction(node.callee)) { return; @@ -168,7 +168,7 @@ const schema = [ { type: 'object', properties: { - ignoreArguments: { + checkArguments: { type: 'boolean' } }, diff --git a/test/no-useless-undefined.js b/test/no-useless-undefined.js index c6c91636fa..44ddef3248 100644 --- a/test/no-useless-undefined.js +++ b/test/no-useless-undefined.js @@ -4,7 +4,7 @@ import {test} from './utils/test'; const messageId = 'no-useless-undefined'; const errors = [{messageId}]; -const optionsIgnoreArguments = [{ignoreArguments: true}]; +const optionsIgnoreArguments = [{checkArguments: false}]; test({ valid: [ From 003d9774bda66d63f31c0ad8c6f1cda613a7fbef Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 20 Oct 2020 22:52:12 +0800 Subject: [PATCH 5/8] Update docs/rules/no-useless-undefined.md Co-authored-by: Sindre Sorhus --- docs/rules/no-useless-undefined.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rules/no-useless-undefined.md b/docs/rules/no-useless-undefined.md index 29d40812b3..7aa447c784 100644 --- a/docs/rules/no-useless-undefined.md +++ b/docs/rules/no-useless-undefined.md @@ -91,7 +91,7 @@ Type: `object` Type: `boolean`\ Default: `true` -Pass `checkArguments: false` to disable check on function call. +Pass `checkArguments: false` to disable check on function call arguments. #### Fail From fe5073296bc81337dddbd4986e6c8dc79ea41ff4 Mon Sep 17 00:00:00 2001 From: fisker Date: Wed, 21 Oct 2020 09:39:58 +0800 Subject: [PATCH 6/8] Update docs --- docs/rules/no-useless-undefined.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/rules/no-useless-undefined.md b/docs/rules/no-useless-undefined.md index 7aa447c784..f8a747b3da 100644 --- a/docs/rules/no-useless-undefined.md +++ b/docs/rules/no-useless-undefined.md @@ -91,20 +91,20 @@ Type: `object` Type: `boolean`\ Default: `true` -Pass `checkArguments: false` to disable check on function call arguments. +Forbid use `undefined` at the end of function call arguments, pass `checkArguments: false` to disable check on them. #### Fail ```js -// eslint unicorn/no-useless-undefined: ["error", {checkArguments: true}] -foo(undefined); +// eslint unicorn/no-useless-undefined: ["error", {"checkArguments": true}] +foo(bar, baz, undefined); ``` #### Pass ```js -// eslint unicorn/no-useless-undefined: ["error", {checkArguments: false}] -foo(undefined); +// eslint unicorn/no-useless-undefined: ["error", {"checkArguments": false}] +foo(bar, baz, undefined); ``` ## Conflict with ESLint `array-callback-return` rule From 7d9e6b689b24599ce50248e4d9a7780a92255410 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 21 Oct 2020 12:12:53 +0200 Subject: [PATCH 7/8] Update no-useless-undefined.md --- docs/rules/no-useless-undefined.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rules/no-useless-undefined.md b/docs/rules/no-useless-undefined.md index f8a747b3da..18aae8730a 100644 --- a/docs/rules/no-useless-undefined.md +++ b/docs/rules/no-useless-undefined.md @@ -91,7 +91,7 @@ Type: `object` Type: `boolean`\ Default: `true` -Forbid use `undefined` at the end of function call arguments, pass `checkArguments: false` to disable check on them. +Forbid the use of `undefined` at the end of function call arguments. Pass `checkArguments: false` to disable check on them. #### Fail From d82f01189af2e88d4e012fc49fad427c6cae8da1 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 21 Oct 2020 12:13:42 +0200 Subject: [PATCH 8/8] Update no-useless-undefined.md --- docs/rules/no-useless-undefined.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rules/no-useless-undefined.md b/docs/rules/no-useless-undefined.md index 18aae8730a..6d60261863 100644 --- a/docs/rules/no-useless-undefined.md +++ b/docs/rules/no-useless-undefined.md @@ -91,7 +91,7 @@ Type: `object` Type: `boolean`\ Default: `true` -Forbid the use of `undefined` at the end of function call arguments. Pass `checkArguments: false` to disable check on them. +Forbid the use of `undefined` at the end of function call arguments. Pass `checkArguments: false` to disable checking them. #### Fail