From c501243e7f9fb012b09a69ffc4ab66fb3376bb7d Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 29 Mar 2022 16:23:45 +0800 Subject: [PATCH] `no-useless-undefined`: Ignore arguments in `Function#bind()` (#1762) --- rules/no-useless-undefined.js | 13 +++ test/no-useless-undefined.mjs | 17 ++++ test/snapshots/no-useless-undefined.mjs.md | 80 +++++++++++++++++++ test/snapshots/no-useless-undefined.mjs.snap | Bin 686 -> 926 bytes 4 files changed, 110 insertions(+) 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 573e569bf586b0e6d05dc58f5fb56ec74b950f28..9424edf2456251f2d6af6a6be5767f880e07192a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