Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no-useless-undefined: Add checkArguments option #877

Merged
Merged
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`

### ignoreArguments

Type: `boolean`<br>
fisker marked this conversation as resolved.
Show resolved Hide resolved
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`:
Expand Down
32 changes: 27 additions & 5 deletions rules/no-useless-undefined.js
Expand Up @@ -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]);
Expand All @@ -91,7 +95,7 @@ const create = context => {
]);
};

return {
const listeners = {
[returnSelector]: listener(removeNodeAndLeadingSpace),
[yieldSelector]: listener(removeNodeAndLeadingSpace),
[arrowFunctionSelector]: listener(
Expand All @@ -102,8 +106,11 @@ 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;
}
Expand Down Expand Up @@ -151,10 +158,24 @@ const create = context => {
return fixer.removeRange([start, end]);
}
});
}
};
};
}

return listeners;
};

const schema = [
{
type: 'object',
properties: {
ignoreArguments: {
type: 'boolean'
}
},
additionalProperties: false
}
];

module.exports = {
create,
meta: {
Expand All @@ -163,6 +184,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 = [{ignoreArguments: true}];

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