Skip to content

Commit 42f9143

Browse files
authoredSep 29, 2022
feat: codefix for for await of (#50623)
1 parent ecf50e8 commit 42f9143

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed
 

‎src/compiler/checker.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,11 @@ namespace ts {
642642
getOptionalType: () => optionalType,
643643
getPromiseType: () => getGlobalPromiseType(/*reportErrors*/ false),
644644
getPromiseLikeType: () => getGlobalPromiseLikeType(/*reportErrors*/ false),
645+
getAsyncIterableType: () => {
646+
const type = getGlobalAsyncIterableType(/*reportErrors*/ false);
647+
if (type === emptyGenericType) return undefined;
648+
return type;
649+
},
645650
isSymbolAccessible,
646651
isArrayType,
647652
isTupleType,
@@ -39429,7 +39434,18 @@ namespace ts {
3942939434
const message = allowAsyncIterables
3943039435
? Diagnostics.Type_0_must_have_a_Symbol_asyncIterator_method_that_returns_an_async_iterator
3943139436
: Diagnostics.Type_0_must_have_a_Symbol_iterator_method_that_returns_an_iterator;
39432-
return errorAndMaybeSuggestAwait(errorNode, !!getAwaitedTypeOfPromise(type), message, typeToString(type));
39437+
const suggestAwait =
39438+
// for (const x of Promise<...>) or [...Promise<...>]
39439+
!!getAwaitedTypeOfPromise(type)
39440+
// for (const x of AsyncIterable<...>)
39441+
|| (
39442+
!allowAsyncIterables &&
39443+
isForOfStatement(errorNode.parent) &&
39444+
errorNode.parent.expression === errorNode &&
39445+
getGlobalAsyncIterableType(/** reportErrors */ false) !== emptyGenericType &&
39446+
isTypeAssignableTo(type, getGlobalAsyncIterableType(/** reportErrors */ false)
39447+
));
39448+
return errorAndMaybeSuggestAwait(errorNode, suggestAwait, message, typeToString(type));
3943339449
}
3943439450

3943539451
/**

‎src/compiler/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4760,6 +4760,7 @@ namespace ts {
47604760
/* @internal */ createPromiseType(type: Type): Type;
47614761
/* @internal */ getPromiseType(): Type;
47624762
/* @internal */ getPromiseLikeType(): Type;
4763+
/* @internal */ getAsyncIterableType(): Type | undefined;
47634764

47644765
/* @internal */ isTypeAssignableTo(source: Type, target: Type): boolean;
47654766
/* @internal */ createAnonymousType(symbol: Symbol | undefined, members: SymbolTable, callSignatures: Signature[], constructSignatures: Signature[], indexInfos: IndexInfo[]): Type;

‎src/services/codefixes/addMissingAwait.ts

+9
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,15 @@ namespace ts.codefix {
226226
}
227227

228228
function makeChange(changeTracker: textChanges.ChangeTracker, errorCode: number, sourceFile: SourceFile, checker: TypeChecker, insertionSite: Expression, fixedDeclarations?: Set<number>) {
229+
if (isForOfStatement(insertionSite.parent) && !insertionSite.parent.awaitModifier) {
230+
const exprType = checker.getTypeAtLocation(insertionSite);
231+
const asyncIter = checker.getAsyncIterableType();
232+
if (asyncIter && checker.isTypeAssignableTo(exprType, asyncIter)) {
233+
const forOf = insertionSite.parent;
234+
changeTracker.replaceNode(sourceFile, forOf, factory.updateForOfStatement(forOf, factory.createToken(SyntaxKind.AwaitKeyword), forOf.initializer, forOf.expression, forOf.statement));
235+
return;
236+
}
237+
}
229238
if (isBinaryExpression(insertionSite)) {
230239
for (const side of [insertionSite.left, insertionSite.right]) {
231240
if (fixedDeclarations && isIdentifier(side)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path="fourslash.ts" />
2+
// @lib: es2020
3+
// @target: es2020
4+
////async function* g() {}
5+
////async function fn() {
6+
//// for (const { } of g()) { }
7+
////}
8+
9+
verify.codeFix({
10+
description: ts.Diagnostics.Add_await.message,
11+
index: 0,
12+
newFileContent:
13+
`async function* g() {}
14+
async function fn() {
15+
for await (const { } of g()) { }
16+
}`
17+
});

0 commit comments

Comments
 (0)