diff --git a/rules/no-useless-undefined.js b/rules/no-useless-undefined.js index e962d112a5..9ee5319342 100644 --- a/rules/no-useless-undefined.js +++ b/rules/no-useless-undefined.js @@ -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 => { @@ -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]; diff --git a/test/no-useless-undefined.mjs b/test/no-useless-undefined.mjs index 9eeca640b7..830a87dcae 100644 --- a/test/no-useless-undefined.mjs +++ b/test/no-useless-undefined.mjs @@ -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: [ { @@ -417,5 +429,10 @@ test.snapshot({ /* */ ); `, + 'foo.bind(undefined)', + 'bind(foo, undefined)', + 'foo.bind?.(bar, undefined)', + 'foo[bind](bar, undefined)', + 'foo.notBind(bar, undefined)', ], }); diff --git a/test/snapshots/no-useless-undefined.mjs.md b/test/snapshots/no-useless-undefined.mjs.md index eb0f109a60..a960ffc2da 100644 --- a/test/snapshots/no-useless-undefined.mjs.md +++ b/test/snapshots/no-useless-undefined.mjs.md @@ -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\`.␊ + ` diff --git a/test/snapshots/no-useless-undefined.mjs.snap b/test/snapshots/no-useless-undefined.mjs.snap index 573e569bf5..9424edf245 100644 Binary files a/test/snapshots/no-useless-undefined.mjs.snap and b/test/snapshots/no-useless-undefined.mjs.snap differ