From c5beaa2ea340985211ca5c12821842c54f5170f0 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Tue, 11 Oct 2022 11:59:04 -0500 Subject: [PATCH] fix(eslint-plugin): Skip missing 'rest' tuple type arguments in no-misused-promises (#5809) Fixes #5807 When linting a function with a 'rest' tuple argument, we might have different numbers of arguments between signatures: ``` function foo(...args: []); function foo(...args: [string]); ``` This PR makes no-misused-promises handle this code correctly, by ensuring that we only check an argument when we have a corresponding type parameter in the tuple type arguments. --- .../src/rules/no-misused-promises.ts | 6 +++++- .../tests/rules/no-misused-promises.test.ts | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/no-misused-promises.ts b/packages/eslint-plugin/src/rules/no-misused-promises.ts index 7afb62785b5..b6914ae2c39 100644 --- a/packages/eslint-plugin/src/rules/no-misused-promises.ts +++ b/packages/eslint-plugin/src/rules/no-misused-promises.ts @@ -564,7 +564,11 @@ function voidFunctionArguments( // Check each type in the tuple - for example, [boolean, () => void] would // add the index of the second tuple parameter to 'voidReturnIndices' const typeArgs = checker.getTypeArguments(type); - for (let i = index; i < node.arguments.length; i++) { + for ( + let i = index; + i < node.arguments.length && i - index < typeArgs.length; + i++ + ) { checkThenableOrVoidArgument( checker, node, diff --git a/packages/eslint-plugin/tests/rules/no-misused-promises.test.ts b/packages/eslint-plugin/tests/rules/no-misused-promises.test.ts index e333e1f4e67..c962761717d 100644 --- a/packages/eslint-plugin/tests/rules/no-misused-promises.test.ts +++ b/packages/eslint-plugin/tests/rules/no-misused-promises.test.ts @@ -419,6 +419,14 @@ new TakeCallbacks( () => true, ); `, + ` +function restTuple(...args: []): void; +function restTuple(...args: [string]): void; +function restTuple(..._args: string[]): void {} + +restTuple(); +restTuple('Hello'); + `, ], invalid: [ @@ -1099,5 +1107,16 @@ new TakesVoidCb( `, errors: [{ line: 11, messageId: 'voidReturnArgument' }], }, + { + code: ` +function restTuple(...args: []): void; +function restTuple(...args: [boolean, () => void]): void; +function restTuple(..._args: any[]): void {} + +restTuple(); +restTuple(true, () => Promise.resolve(1)); + `, + errors: [{ line: 7, messageId: 'voidReturnArgument' }], + }, ], });