Skip to content

Commit 745cfe4

Browse files
fix(eslint-plugin): [no-misused-promises] avoid unnecessary calls to 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>
1 parent cea05c8 commit 745cfe4

File tree

2 files changed

+67
-6
lines changed

2 files changed

+67
-6
lines changed

packages/eslint-plugin/src/rules/no-misused-promises.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,9 @@ export default util.createRule<Options, MessageId>({
312312
return;
313313
}
314314

315+
if (!returnsThenable(checker, tsNode)) {
316+
return;
317+
}
315318
const objType = checker.getContextualType(obj);
316319
if (objType === undefined) {
317320
return;
@@ -329,10 +332,7 @@ export default util.createRule<Options, MessageId>({
329332
tsNode.name,
330333
);
331334

332-
if (
333-
isVoidReturningFunctionType(checker, tsNode.name, contextualType) &&
334-
returnsThenable(checker, tsNode)
335-
) {
335+
if (isVoidReturningFunctionType(checker, tsNode.name, contextualType)) {
336336
context.report({
337337
messageId: 'voidReturnProperty',
338338
node: node.value,
@@ -378,8 +378,7 @@ export default util.createRule<Options, MessageId>({
378378
const contextualType = checker.getContextualType(value);
379379
if (
380380
contextualType !== undefined &&
381-
isVoidReturningFunctionType(checker, value, contextualType) &&
382-
returnsThenable(checker, value.expression)
381+
isVoidReturningFunctionType(checker, value, contextualType)
383382
) {
384383
context.report({
385384
messageId: 'voidReturnAttribute',

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

+62
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,39 @@ function restTuple(..._args: string[]): void {}
427427
restTuple();
428428
restTuple('Hello');
429429
`,
430+
`
431+
let value: Record<string, () => void>;
432+
value.sync = () => {};
433+
`,
434+
`
435+
type ReturnsRecord = () => Record<string, () => void>;
436+
437+
const test: ReturnsRecord = () => {
438+
return { sync: () => {} };
439+
};
440+
`,
441+
`
442+
type ReturnsRecord = () => Record<string, () => void>;
443+
444+
function sync() {}
445+
446+
const test: ReturnsRecord = () => {
447+
return { sync };
448+
};
449+
`,
450+
`
451+
function withTextRecurser<Text extends string>(
452+
recurser: (text: Text) => void,
453+
): (text: Text) => void {
454+
return (text: Text): void => {
455+
if (text.length) {
456+
return;
457+
}
458+
459+
return recurser(node);
460+
};
461+
}
462+
`,
430463
],
431464

432465
invalid: [
@@ -1118,5 +1151,34 @@ restTuple(true, () => Promise.resolve(1));
11181151
`,
11191152
errors: [{ line: 7, messageId: 'voidReturnArgument' }],
11201153
},
1154+
{
1155+
code: `
1156+
type ReturnsRecord = () => Record<string, () => void>;
1157+
1158+
const test: ReturnsRecord = () => {
1159+
return { asynchronous: async () => {} };
1160+
};
1161+
`,
1162+
errors: [{ line: 5, messageId: 'voidReturnProperty' }],
1163+
},
1164+
{
1165+
code: `
1166+
let value: Record<string, () => void>;
1167+
value.asynchronous = async () => {};
1168+
`,
1169+
errors: [{ line: 3, messageId: 'voidReturnVariable' }],
1170+
},
1171+
{
1172+
code: `
1173+
type ReturnsRecord = () => Record<string, () => void>;
1174+
1175+
async function asynchronous() {}
1176+
1177+
const test: ReturnsRecord = () => {
1178+
return { asynchronous };
1179+
};
1180+
`,
1181+
errors: [{ line: 7, messageId: 'voidReturnProperty' }],
1182+
},
11211183
],
11221184
});

0 commit comments

Comments
 (0)