Skip to content

Commit

Permalink
fix(eslint-plugin): [no-misused-promises] avoid unnecessary calls to …
Browse files Browse the repository at this point in the history
…getContextualType (#6193)

* avoid unnecessary calls to getContextualType

* Removed a few more

* Added back necessary untested logic, with tests

---------

Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>
  • Loading branch information
scottarver and JoshuaKGoldberg authored Mar 13, 2023
1 parent cea05c8 commit 745cfe4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
11 changes: 5 additions & 6 deletions packages/eslint-plugin/src/rules/no-misused-promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ export default util.createRule<Options, MessageId>({
return;
}

if (!returnsThenable(checker, tsNode)) {
return;
}
const objType = checker.getContextualType(obj);
if (objType === undefined) {
return;
Expand All @@ -329,10 +332,7 @@ export default util.createRule<Options, MessageId>({
tsNode.name,
);

if (
isVoidReturningFunctionType(checker, tsNode.name, contextualType) &&
returnsThenable(checker, tsNode)
) {
if (isVoidReturningFunctionType(checker, tsNode.name, contextualType)) {
context.report({
messageId: 'voidReturnProperty',
node: node.value,
Expand Down Expand Up @@ -378,8 +378,7 @@ export default util.createRule<Options, MessageId>({
const contextualType = checker.getContextualType(value);
if (
contextualType !== undefined &&
isVoidReturningFunctionType(checker, value, contextualType) &&
returnsThenable(checker, value.expression)
isVoidReturningFunctionType(checker, value, contextualType)
) {
context.report({
messageId: 'voidReturnAttribute',
Expand Down
62 changes: 62 additions & 0 deletions packages/eslint-plugin/tests/rules/no-misused-promises.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,39 @@ function restTuple(..._args: string[]): void {}
restTuple();
restTuple('Hello');
`,
`
let value: Record<string, () => void>;
value.sync = () => {};
`,
`
type ReturnsRecord = () => Record<string, () => void>;
const test: ReturnsRecord = () => {
return { sync: () => {} };
};
`,
`
type ReturnsRecord = () => Record<string, () => void>;
function sync() {}
const test: ReturnsRecord = () => {
return { sync };
};
`,
`
function withTextRecurser<Text extends string>(
recurser: (text: Text) => void,
): (text: Text) => void {
return (text: Text): void => {
if (text.length) {
return;
}
return recurser(node);
};
}
`,
],

invalid: [
Expand Down Expand Up @@ -1118,5 +1151,34 @@ restTuple(true, () => Promise.resolve(1));
`,
errors: [{ line: 7, messageId: 'voidReturnArgument' }],
},
{
code: `
type ReturnsRecord = () => Record<string, () => void>;
const test: ReturnsRecord = () => {
return { asynchronous: async () => {} };
};
`,
errors: [{ line: 5, messageId: 'voidReturnProperty' }],
},
{
code: `
let value: Record<string, () => void>;
value.asynchronous = async () => {};
`,
errors: [{ line: 3, messageId: 'voidReturnVariable' }],
},
{
code: `
type ReturnsRecord = () => Record<string, () => void>;
async function asynchronous() {}
const test: ReturnsRecord = () => {
return { asynchronous };
};
`,
errors: [{ line: 7, messageId: 'voidReturnProperty' }],
},
],
});

0 comments on commit 745cfe4

Please sign in to comment.