diff --git a/docs/rules/no-useless-undefined.md b/docs/rules/no-useless-undefined.md index 07d39eba3b..6d60261863 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` + +### checkArguments + +Type: `boolean`\ +Default: `true` + +Forbid the use of `undefined` at the end of function call arguments. Pass `checkArguments: false` to disable checking them. + +#### Fail + +```js +// eslint unicorn/no-useless-undefined: ["error", {"checkArguments": true}] +foo(bar, baz, undefined); +``` + +#### Pass + +```js +// eslint unicorn/no-useless-undefined: ["error", {"checkArguments": false}] +foo(bar, baz, 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 5e25f44870..e1a0da90fb 100644 --- a/rules/no-useless-undefined.js +++ b/rules/no-useless-undefined.js @@ -83,6 +83,10 @@ const create = context => { }; const code = context.getSourceCode().text; + const options = { + checkArguments: true, + ...context.options[0] + }; const removeNodeAndLeadingSpace = (node, fixer) => { const textBefore = code.slice(0, node.range[0]); @@ -92,7 +96,7 @@ const create = context => { ]); }; - return { + const listeners = { [returnSelector]: listener(removeNodeAndLeadingSpace), [yieldSelector]: listener(removeNodeAndLeadingSpace), [arrowFunctionSelector]: listener( @@ -103,8 +107,11 @@ const create = context => { ), [assignmentPatternSelector]: listener( (node, fixer) => fixer.removeRange([node.parent.left.range[1], node.range[1]]) - ), - CallExpression: node => { + ) + }; + + if (options.checkArguments) { + listeners.CallExpression = node => { if (isCompareFunction(node.callee)) { return; } @@ -152,10 +159,24 @@ const create = context => { return fixer.removeRange([start, end]); } }); - } - }; + }; + } + + return listeners; }; +const schema = [ + { + type: 'object', + properties: { + checkArguments: { + type: 'boolean' + } + }, + additionalProperties: false + } +]; + module.exports = { create, meta: { @@ -164,6 +185,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 bc12363eee..c4c1359e18 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 = [{checkArguments: false}]; test({ valid: [ @@ -41,7 +42,13 @@ test({ 't.notSame(foo, undefined)', 't.strictSame(foo, undefined)', 't.strictNotSame(foo, undefined)', - 'expect(someFunction).toHaveBeenCalledWith(1, 2, undefined);' + 'expect(someFunction).toHaveBeenCalledWith(1, 2, undefined);', + + // `checkArguments: false` + { + code: 'foo(undefined, undefined);', + options: optionsIgnoreArguments + } ], invalid: [ {