Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(eslint-plugin): [no-misused-promises] prioritize false returns wh…
…en checking whether a function returns only void (#4841)

* fix(eslint-plugin): prioritize false returns when checking whether a function returns only void

* Update packages/eslint-plugin/tests/rules/no-misused-promises.test.ts

Co-authored-by: Brad Zacher <brad.zacher@gmail.com>

Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
  • Loading branch information
JoshuaKGoldberg and bradzacher committed Apr 22, 2022
1 parent 4e7c9be commit ccadb60
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/eslint-plugin/src/rules/no-misused-promises.ts
Expand Up @@ -537,6 +537,8 @@ function isVoidReturningFunctionType(
node: ts.Node,
type: ts.Type,
): boolean {
let hadVoidReturn = false;

for (const subType of tsutils.unionTypeParts(type)) {
for (const signature of subType.getCallSignatures()) {
const returnType = signature.getReturnType();
Expand All @@ -547,12 +549,11 @@ function isVoidReturningFunctionType(
return false;
}

if (tsutils.isTypeFlagSet(returnType, ts.TypeFlags.Void)) {
return true;
}
hadVoidReturn ||= tsutils.isTypeFlagSet(returnType, ts.TypeFlags.Void);
}
}
return false;

return hadVoidReturn;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions packages/eslint-plugin/tests/rules/no-misused-promises.test.ts
Expand Up @@ -304,6 +304,18 @@ declare const it: ItLike;
it('', async () => {});
`,
},
{
code: `
interface Props {
onEvent: (() => void) | (() => Promise<void>);
}
declare function Component(props: Props): any;
const _ = <Component onEvent={async () => {}} />;
`,
filename: 'react.tsx',
},
],

invalid: [
Expand Down

0 comments on commit ccadb60

Please sign in to comment.