Skip to content

Commit

Permalink
no-useless-undefined: Add checkArguments option (#877)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
fisker and sindresorhus committed Oct 21, 2020
1 parent 7a4dbf4 commit a1617b9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
25 changes: 25 additions & 0 deletions docs/rules/no-useless-undefined.md
Expand Up @@ -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`:
Expand Down
32 changes: 27 additions & 5 deletions rules/no-useless-undefined.js
Expand Up @@ -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]);
Expand All @@ -92,7 +96,7 @@ const create = context => {
]);
};

return {
const listeners = {
[returnSelector]: listener(removeNodeAndLeadingSpace),
[yieldSelector]: listener(removeNodeAndLeadingSpace),
[arrowFunctionSelector]: listener(
Expand All @@ -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;
}
Expand Down Expand Up @@ -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: {
Expand All @@ -164,6 +185,7 @@ module.exports = {
url: getDocumentationUrl(__filename)
},
messages,
schema,
fixable: 'code'
}
};
9 changes: 8 additions & 1 deletion test/no-useless-undefined.js
Expand Up @@ -4,6 +4,7 @@ import {test} from './utils/test';
const messageId = 'no-useless-undefined';

const errors = [{messageId}];
const optionsIgnoreArguments = [{checkArguments: false}];

test({
valid: [
Expand Down Expand Up @@ -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: [
{
Expand Down

0 comments on commit a1617b9

Please sign in to comment.