Skip to content

Commit

Permalink
fix(eslint-plugin): Skip missing 'rest' tuple type arguments in no-mi…
Browse files Browse the repository at this point in the history
…sused-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.
  • Loading branch information
Aaron1011 committed Oct 11, 2022
1 parent d1f3c53 commit c5beaa2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/eslint-plugin/src/rules/no-misused-promises.ts
Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions packages/eslint-plugin/tests/rules/no-misused-promises.test.ts
Expand Up @@ -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: [
Expand Down Expand Up @@ -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' }],
},
],
});

0 comments on commit c5beaa2

Please sign in to comment.