Skip to content

Commit

Permalink
no-useless-undefined: Ignore arguments in Function#bind() (#1762)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Mar 29, 2022
1 parent 3b9ffbf commit c501243
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
13 changes: 13 additions & 0 deletions rules/no-useless-undefined.js
Expand Up @@ -91,6 +91,13 @@ const getFunction = scope => {
}
};

const isFunctionBindCall = node =>
!node.optional
&& node.callee.type === 'MemberExpression'
&& !node.callee.computed
&& node.callee.property.type === 'Identifier'
&& node.callee.property.name === 'bind';

/** @param {import('eslint').Rule.RuleContext} context */
const create = context => {
const listener = (fix, checkFunctionReturnType) => node => {
Expand Down Expand Up @@ -142,6 +149,12 @@ const create = context => {
}

const argumentNodes = node.arguments;

// Ignore arguments in `Function#bind()`, but not `this` argument
if (isFunctionBindCall(node) && argumentNodes.length !== 1) {
return;
}

const undefinedArguments = [];
for (let index = argumentNodes.length - 1; index >= 0; index--) {
const node = argumentNodes[index];
Expand Down
17 changes: 17 additions & 0 deletions test/no-useless-undefined.mjs
Expand Up @@ -54,11 +54,23 @@ test({
'createContext(undefined);',
'React.createContext(undefined);',

// `Function#bind()`
'foo.bind(bar, undefined)',
'foo.bind(...bar, undefined)',
'foo.bind(...[], undefined)',
'foo.bind(...[undefined], undefined)',
'foo.bind(bar, baz, undefined)',
'foo?.bind(bar, undefined)',

// `checkArguments: false`
{
code: 'foo(undefined, undefined);',
options: optionsIgnoreArguments,
},
{
code: 'foo.bind(undefined);',
options: optionsIgnoreArguments,
},
],
invalid: [
{
Expand Down Expand Up @@ -417,5 +429,10 @@ test.snapshot({
/* */
);
`,
'foo.bind(undefined)',
'bind(foo, undefined)',
'foo.bind?.(bar, undefined)',
'foo[bind](bar, undefined)',
'foo.notBind(bar, undefined)',
],
});
80 changes: 80 additions & 0 deletions test/snapshots/no-useless-undefined.mjs.md
Expand Up @@ -219,3 +219,83 @@ Generated by [AVA](https://avajs.dev).
8 | /* */␊
9 | );␊
`

## Invalid #8
1 | foo.bind(undefined)

> Output
`␊
1 | foo.bind()␊
`

> Error 1/1
`␊
> 1 | foo.bind(undefined)␊
| ^^^^^^^^^ Do not use useless \`undefined\`.␊
`

## Invalid #9
1 | bind(foo, undefined)

> Output
`␊
1 | bind(foo)␊
`

> Error 1/1
`␊
> 1 | bind(foo, undefined)␊
| ^^^^^^^^^ Do not use useless \`undefined\`.␊
`

## Invalid #10
1 | foo.bind?.(bar, undefined)

> Output
`␊
1 | foo.bind?.(bar)␊
`

> Error 1/1
`␊
> 1 | foo.bind?.(bar, undefined)␊
| ^^^^^^^^^ Do not use useless \`undefined\`.␊
`

## Invalid #11
1 | foo[bind](bar, undefined)

> Output
`␊
1 | foo[bind](bar)␊
`

> Error 1/1
`␊
> 1 | foo[bind](bar, undefined)␊
| ^^^^^^^^^ Do not use useless \`undefined\`.␊
`

## Invalid #12
1 | foo.notBind(bar, undefined)

> Output
`␊
1 | foo.notBind(bar)␊
`

> Error 1/1
`␊
> 1 | foo.notBind(bar, undefined)␊
| ^^^^^^^^^ Do not use useless \`undefined\`.␊
`
Binary file modified test/snapshots/no-useless-undefined.mjs.snap
Binary file not shown.

0 comments on commit c501243

Please sign in to comment.