Skip to content

Commit a994d43

Browse files
committed
Did you forget to use await? on arguments of function calls
1 parent 63dd5bd commit a994d43

7 files changed

+38
-15
lines changed

src/compiler/checker.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11700,10 +11700,10 @@ namespace ts {
1170011700
return checkTypeRelatedToAndOptionallyElaborate(source, target, assignableRelation, errorNode, expr, headMessage, containingMessageChain);
1170111701
}
1170211702

11703-
function checkTypeRelatedToAndOptionallyElaborate(source: Type, target: Type, relation: Map<RelationComparisonResult>, errorNode: Node | undefined, expr: Expression | undefined, headMessage?: DiagnosticMessage, containingMessageChain?: () => DiagnosticMessageChain | undefined): boolean {
11703+
function checkTypeRelatedToAndOptionallyElaborate(source: Type, target: Type, relation: Map<RelationComparisonResult>, errorNode: Node | undefined, expr: Expression | undefined, headMessage?: DiagnosticMessage, containingMessageChain?: () => DiagnosticMessageChain | undefined, errorOutputContainer?: { error?: Diagnostic | undefined }): boolean {
1170411704
if (isTypeRelatedTo(source, target, relation)) return true;
1170511705
if (!errorNode || !elaborateError(expr, source, target, relation, headMessage)) {
11706-
return checkTypeRelatedTo(source, target, relation, errorNode, headMessage, containingMessageChain);
11706+
return checkTypeRelatedTo(source, target, relation, errorNode, headMessage, containingMessageChain, errorOutputContainer);
1170711707
}
1170811708
return false;
1170911709
}
@@ -21185,6 +21185,7 @@ namespace ts {
2118521185
const headMessage = Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1;
2118621186
const restType = getNonArrayRestType(signature);
2118721187
const argCount = restType ? Math.min(getParameterCount(signature) - 1, args.length) : args.length;
21188+
const errorContainer: { error?: Diagnostic } = {};
2118821189
for (let i = 0; i < argCount; i++) {
2118921190
const arg = args[i];
2119021191
if (arg.kind !== SyntaxKind.OmittedExpression) {
@@ -21194,17 +21195,29 @@ namespace ts {
2119421195
// we obtain the regular type of any object literal arguments because we may not have inferred complete
2119521196
// parameter types yet and therefore excess property checks may yield false positives (see #17041).
2119621197
const checkArgType = checkMode & CheckMode.SkipContextSensitive ? getRegularTypeOfObjectLiteral(argType) : argType;
21197-
if (!checkTypeRelatedToAndOptionallyElaborate(checkArgType, paramType, relation, reportErrors ? arg : undefined, arg, headMessage)) {
21198+
if (!checkTypeRelatedToAndOptionallyElaborate(checkArgType, paramType, relation, reportErrors ? arg : undefined, arg, headMessage, /*containingMessageChain*/ undefined, errorContainer)) {
21199+
maybeAddMissingAwaitInfo(arg, checkArgType, paramType);
2119821200
return false;
2119921201
}
2120021202
}
2120121203
}
2120221204
if (restType) {
2120321205
const spreadType = getSpreadArgumentType(args, argCount, args.length, restType, /*context*/ undefined);
2120421206
const errorNode = reportErrors ? argCount < args.length ? args[argCount] : node : undefined;
21205-
return checkTypeRelatedTo(spreadType, restType, relation, errorNode, headMessage);
21207+
const result = checkTypeRelatedTo(spreadType, restType, relation, errorNode, headMessage, /*containingMessageChain*/ undefined, errorContainer);
21208+
maybeAddMissingAwaitInfo(errorNode, spreadType, restType);
21209+
return result;
2120621210
}
2120721211
return true;
21212+
21213+
function maybeAddMissingAwaitInfo(errorNode: Node | undefined, source: Type, target: Type) {
21214+
if (errorNode && reportErrors && errorContainer.error) {
21215+
const awaitedTypeOfSource = getAwaitedTypeOfPromise(source);
21216+
if (awaitedTypeOfSource && isTypeRelatedTo(awaitedTypeOfSource, target, relation)) {
21217+
addRelatedInfo(errorContainer.error, createDiagnosticForNode(errorNode, Diagnostics.Did_you_forget_to_use_await));
21218+
}
21219+
}
21220+
}
2120821221
}
2120921222

2121021223
/**

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2709,6 +2709,10 @@
27092709
"category": "Error",
27102710
"code": 2776
27112711
},
2712+
"Did you forget to use 'await'?": {
2713+
"category": "Error",
2714+
"code": 2777
2715+
},
27122716

27132717
"Import declaration '{0}' is using private name '{1}'.": {
27142718
"category": "Error",

tests/baselines/reference/operationsAvailableOnPromisedType.errors.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ tests/cases/compiler/operationsAvailableOnPromisedType.ts(16,7): error TS2764: A
77
tests/cases/compiler/operationsAvailableOnPromisedType.ts(17,5): error TS2776: This condition will always return 'false' since the types 'number' and 'Promise<number>' have no overlap. Did you forget to use 'await'?
88
tests/cases/compiler/operationsAvailableOnPromisedType.ts(18,9): error TS2769: Type 'Promise<string[]>' is not an array type. Did you forget to use 'await'?
99
tests/cases/compiler/operationsAvailableOnPromisedType.ts(19,21): error TS2770: Type 'Promise<string[]>' is not an array type or a string type. Did you forget to use 'await'?
10-
tests/cases/compiler/operationsAvailableOnPromisedType.ts(20,9): error TS2554: Expected 7 arguments, but got 4.
10+
tests/cases/compiler/operationsAvailableOnPromisedType.ts(20,12): error TS2345: Argument of type 'Promise<number>' is not assignable to parameter of type 'number'.
1111
tests/cases/compiler/operationsAvailableOnPromisedType.ts(21,11): error TS2570: Property 'prop' does not exist on type 'Promise<{ prop: string; }>'. Did you forget to use 'await'?
1212
tests/cases/compiler/operationsAvailableOnPromisedType.ts(23,27): error TS2770: Type 'Promise<string[]>' is not an array type or a string type. Did you forget to use 'await'?
1313
tests/cases/compiler/operationsAvailableOnPromisedType.ts(24,5): error TS2774: This expression is not callable. Did you forget to use 'await'?
@@ -59,10 +59,10 @@ tests/cases/compiler/operationsAvailableOnPromisedType.ts(27,5): error TS2349: T
5959
for (const s of c) {
6060
~
6161
!!! error TS2770: Type 'Promise<string[]>' is not an array type or a string type. Did you forget to use 'await'?
62-
fn(b, b, c, d);
63-
~~~~~~~~~~~~~~
64-
!!! error TS2554: Expected 7 arguments, but got 4.
65-
!!! related TS6210 tests/cases/compiler/operationsAvailableOnPromisedType.ts:6:5: An argument for 'e' was not provided.
62+
fn(b, b, c, d, e, f, g);
63+
~
64+
!!! error TS2345: Argument of type 'Promise<number>' is not assignable to parameter of type 'number'.
65+
!!! related TS2777 tests/cases/compiler/operationsAvailableOnPromisedType.ts:20:12: Did you forget to use 'await'?
6666
d.prop;
6767
~~~~
6868
!!! error TS2570: Property 'prop' does not exist on type 'Promise<{ prop: string; }>'. Did you forget to use 'await'?

tests/baselines/reference/operationsAvailableOnPromisedType.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ async function fn(
1818
a === b;
1919
[...c];
2020
for (const s of c) {
21-
fn(b, b, c, d);
21+
fn(b, b, c, d, e, f, g);
2222
d.prop;
2323
}
2424
for await (const s of c) {}
@@ -98,7 +98,7 @@ function fn(a, b, c, d, e, f, g) {
9898
__spreadArrays(c);
9999
for (_i = 0, c_2 = c; _i < c_2.length; _i++) {
100100
s = c_2[_i];
101-
fn(b, b, c, d);
101+
fn(b, b, c, d, e, f, g);
102102
d.prop;
103103
}
104104
_b.label = 1;

tests/baselines/reference/operationsAvailableOnPromisedType.symbols

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,15 @@ async function fn(
6565
>s : Symbol(s, Decl(operationsAvailableOnPromisedType.ts, 18, 14))
6666
>c : Symbol(c, Decl(operationsAvailableOnPromisedType.ts, 2, 23))
6767

68-
fn(b, b, c, d);
68+
fn(b, b, c, d, e, f, g);
6969
>fn : Symbol(fn, Decl(operationsAvailableOnPromisedType.ts, 0, 0))
7070
>b : Symbol(b, Decl(operationsAvailableOnPromisedType.ts, 1, 14))
7171
>b : Symbol(b, Decl(operationsAvailableOnPromisedType.ts, 1, 14))
7272
>c : Symbol(c, Decl(operationsAvailableOnPromisedType.ts, 2, 23))
7373
>d : Symbol(d, Decl(operationsAvailableOnPromisedType.ts, 3, 25))
74+
>e : Symbol(e, Decl(operationsAvailableOnPromisedType.ts, 4, 33))
75+
>f : Symbol(f, Decl(operationsAvailableOnPromisedType.ts, 5, 27))
76+
>g : Symbol(g, Decl(operationsAvailableOnPromisedType.ts, 6, 42))
7477

7578
d.prop;
7679
>d : Symbol(d, Decl(operationsAvailableOnPromisedType.ts, 3, 25))

tests/baselines/reference/operationsAvailableOnPromisedType.types

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,16 @@ async function fn(
6868
>s : any
6969
>c : Promise<string[]>
7070

71-
fn(b, b, c, d);
72-
>fn(b, b, c, d) : Promise<void>
71+
fn(b, b, c, d, e, f, g);
72+
>fn(b, b, c, d, e, f, g) : Promise<void>
7373
>fn : (a: number, b: Promise<number>, c: Promise<string[]>, d: Promise<{ prop: string; }>, e: Promise<() => void>, f: Promise<() => void> | (() => void), g: Promise<new () => any>) => Promise<void>
7474
>b : Promise<number>
7575
>b : Promise<number>
7676
>c : Promise<string[]>
7777
>d : Promise<{ prop: string; }>
78+
>e : Promise<() => void>
79+
>f : Promise<() => void> | (() => void)
80+
>g : Promise<new () => any>
7881

7982
d.prop;
8083
>d.prop : any

tests/cases/compiler/operationsAvailableOnPromisedType.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async function fn(
1717
a === b;
1818
[...c];
1919
for (const s of c) {
20-
fn(b, b, c, d);
20+
fn(b, b, c, d, e, f, g);
2121
d.prop;
2222
}
2323
for await (const s of c) {}

0 commit comments

Comments
 (0)