From 48fc6b8b1775b632c285c401d9487db5c89eff10 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Tue, 2 Jul 2019 18:04:55 -0700 Subject: [PATCH] Did you forget to use await? on iterables --- src/compiler/checker.ts | 33 ++++++++++++------- src/compiler/diagnosticMessages.json | 26 ++++++++++++--- .../operationsAvailableOnPromisedType.ts | 4 ++- 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index f4bee2eef4f..63fa0f7e889 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -27922,18 +27922,28 @@ namespace ts { // number and string input is allowed, we want to say that number is not an // array type or a string type. const yieldType = getIterationTypeOfIterable(use, IterationTypeKind.Yield, inputType, /*errorNode*/ undefined); - const diagnostic = !(use & IterationUse.AllowsStringInputFlag) || hasStringConstituent + const [defaultDiagnostic, missingAwaitDiagnostic]: [DiagnosticMessage, DiagnosticMessage | undefined] = !(use & IterationUse.AllowsStringInputFlag) || hasStringConstituent ? downlevelIteration - ? Diagnostics.Type_0_is_not_an_array_type_or_does_not_have_a_Symbol_iterator_method_that_returns_an_iterator + ? [Diagnostics.Type_0_is_not_an_array_type_or_does_not_have_a_Symbol_iterator_method_that_returns_an_iterator, Diagnostics.Type_0_is_not_an_array_type_or_does_not_have_a_Symbol_iterator_method_that_returns_an_iterator_Did_you_forget_to_use_await] : yieldType - ? Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_Use_compiler_option_downlevelIteration_to_allow_iterating_of_iterators - : Diagnostics.Type_0_is_not_an_array_type + ? [Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_Use_compiler_option_downlevelIteration_to_allow_iterating_of_iterators, undefined] + : [Diagnostics.Type_0_is_not_an_array_type, Diagnostics.Type_0_is_not_an_array_type_Did_you_forget_to_use_await] : downlevelIteration - ? Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_or_does_not_have_a_Symbol_iterator_method_that_returns_an_iterator + ? [Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_or_does_not_have_a_Symbol_iterator_method_that_returns_an_iterator, Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_or_does_not_have_a_Symbol_iterator_method_that_returns_an_iterator_Did_you_forget_to_use_await] : yieldType - ? Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_Use_compiler_option_downlevelIteration_to_allow_iterating_of_iterators - : Diagnostics.Type_0_is_not_an_array_type_or_a_string_type; - error(errorNode, diagnostic, typeToString(arrayType)); + ? [Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_Use_compiler_option_downlevelIteration_to_allow_iterating_of_iterators, undefined] + : [Diagnostics.Type_0_is_not_an_array_type_or_a_string_type, Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_Did_you_forget_to_use_await]; + if (missingAwaitDiagnostic) { + errorAndMaybeSuggestAwait( + errorNode, + !!getAwaitedTypeOfPromise(arrayType), + defaultDiagnostic, + missingAwaitDiagnostic, + typeToString(arrayType)); + } + else { + error(errorNode, defaultDiagnostic, typeToString(arrayType)); + } } return hasStringConstituent ? stringType : undefined; } @@ -28232,9 +28242,10 @@ namespace ts { } function reportTypeNotIterableError(errorNode: Node, type: Type, allowAsyncIterables: boolean): void { - error(errorNode, allowAsyncIterables - ? Diagnostics.Type_0_must_have_a_Symbol_asyncIterator_method_that_returns_an_async_iterator - : Diagnostics.Type_0_must_have_a_Symbol_iterator_method_that_returns_an_iterator, typeToString(type)); + const [defaultDiagnostic, missingAwaitDiagnostic] = allowAsyncIterables + ? [Diagnostics.Type_0_must_have_a_Symbol_asyncIterator_method_that_returns_an_async_iterator, Diagnostics.Type_0_must_have_a_Symbol_asyncIterator_method_that_returns_an_async_iterator_Did_you_forget_to_use_await] + : [Diagnostics.Type_0_must_have_a_Symbol_iterator_method_that_returns_an_iterator, Diagnostics.Type_0_must_have_a_Symbol_iterator_method_that_returns_an_iterator_Did_you_forget_to_use_await]; + errorAndMaybeSuggestAwait(errorNode, !!getAwaitedTypeOfPromise(type), defaultDiagnostic, missingAwaitDiagnostic, typeToString(type)); } /** diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 446d4294272..a7b62838249 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2713,26 +2713,42 @@ "category": "Error", "code": 2777 }, - "Type '{0}' is not an array type or a string type. Did you forget to use 'await'?": { + "Type '{0}' must have a '[Symbol.asyncIterator]()' method that returns an async iterator. Did you forget to use 'await'?": { "category": "Error", "code": 2778 }, - "Argument of type '{0}' is not assignable to parameter of type '{1}'. Did you forget to use 'await'?": { + "Type '{0}' is not an array type. Did you forget to use 'await'?": { "category": "Error", "code": 2779 }, - "Type '{0}' has no call signatures. Did you forget to use 'await'?": { + "Type '{0}' is not an array type or a string type. Did you forget to use 'await'?": { "category": "Error", "code": 2780 }, - "Type '{0}' has no construct signatures. Did you forget to use 'await'?": { + "Type '{0}' is not an array type or does not have a '[Symbol.iterator]()' method that returns an iterator. Did you forget to use 'await'?": { "category": "Error", "code": 2781 }, - "This condition will always return '{0}' since the types '{1}' and '{2}' have no overlap. Did you forget to use 'await'?": { + "Type '{0}' is not an array type or a string type or does not have a '[Symbol.iterator]()' method that returns an iterator. Did you forget to use 'await'?": { "category": "Error", "code": 2782 }, + "Argument of type '{0}' is not assignable to parameter of type '{1}'. Did you forget to use 'await'?": { + "category": "Error", + "code": 2783 + }, + "Type '{0}' has no call signatures. Did you forget to use 'await'?": { + "category": "Error", + "code": 2784 + }, + "Type '{0}' has no construct signatures. Did you forget to use 'await'?": { + "category": "Error", + "code": 2785 + }, + "This condition will always return '{0}' since the types '{1}' and '{2}' have no overlap. Did you forget to use 'await'?": { + "category": "Error", + "code": 2786 + }, "Import declaration '{0}' is using private name '{1}'.": { "category": "Error", diff --git a/tests/cases/compiler/operationsAvailableOnPromisedType.ts b/tests/cases/compiler/operationsAvailableOnPromisedType.ts index 24e3f891fe4..76e2cea6dd8 100644 --- a/tests/cases/compiler/operationsAvailableOnPromisedType.ts +++ b/tests/cases/compiler/operationsAvailableOnPromisedType.ts @@ -1,4 +1,4 @@ -function fn( +async function fn( a: number, b: Promise, c: Promise, @@ -15,10 +15,12 @@ function fn( b++; --b; a === b; + [...c]; for (const s of c) { fn(b, b, c, d); d.prop; } + for await (const s of c) {} e(); f(); new g();