Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: codefix for for await of #50623

Merged
merged 1 commit into from Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/compiler/checker.ts
Expand Up @@ -642,6 +642,11 @@ namespace ts {
getOptionalType: () => optionalType,
getPromiseType: () => getGlobalPromiseType(/*reportErrors*/ false),
getPromiseLikeType: () => getGlobalPromiseLikeType(/*reportErrors*/ false),
getAsyncIterableType: () => {
const type = getGlobalAsyncIterableType(/*reportErrors*/ false);
if (type === emptyGenericType) return undefined;
return type;
},
isSymbolAccessible,
isArrayType,
isTupleType,
Expand Down Expand Up @@ -39214,7 +39219,18 @@ namespace ts {
const message = 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;
return errorAndMaybeSuggestAwait(errorNode, !!getAwaitedTypeOfPromise(type), message, typeToString(type));
const suggestAwait =
// for (const x of Promise<...>) or [...Promise<...>]
!!getAwaitedTypeOfPromise(type)
// for (const x of AsyncIterable<...>)
|| (
!allowAsyncIterables &&
isForOfStatement(errorNode.parent) &&
errorNode.parent.expression === errorNode &&
getGlobalAsyncIterableType(/** reportErrors */ false) !== emptyGenericType &&
isTypeAssignableTo(type, getGlobalAsyncIterableType(/** reportErrors */ false)
));
return errorAndMaybeSuggestAwait(errorNode, suggestAwait, message, typeToString(type));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this adds an error, can you add a compiler test that shows the code that is now an error and wasn't before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does not add a new error. it changes how suggestAwait is computed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I see. errorAndMaybeSuggestAwait always errors, but only suggests adding await if suggestAwait is true.

}

/**
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Expand Up @@ -4733,6 +4733,7 @@ namespace ts {
/* @internal */ createPromiseType(type: Type): Type;
/* @internal */ getPromiseType(): Type;
/* @internal */ getPromiseLikeType(): Type;
/* @internal */ getAsyncIterableType(): Type | undefined;

/* @internal */ isTypeAssignableTo(source: Type, target: Type): boolean;
/* @internal */ createAnonymousType(symbol: Symbol | undefined, members: SymbolTable, callSignatures: Signature[], constructSignatures: Signature[], indexInfos: IndexInfo[]): Type;
Expand Down
9 changes: 9 additions & 0 deletions src/services/codefixes/addMissingAwait.ts
Expand Up @@ -226,6 +226,15 @@ namespace ts.codefix {
}

function makeChange(changeTracker: textChanges.ChangeTracker, errorCode: number, sourceFile: SourceFile, checker: TypeChecker, insertionSite: Expression, fixedDeclarations?: Set<number>) {
if (isForOfStatement(insertionSite.parent) && !insertionSite.parent.awaitModifier) {
const exprType = checker.getTypeAtLocation(insertionSite);
const asyncIter = checker.getAsyncIterableType();
if (asyncIter && checker.isTypeAssignableTo(exprType, asyncIter)) {
const forOf = insertionSite.parent;
changeTracker.replaceNode(sourceFile, forOf, factory.updateForOfStatement(forOf, factory.createToken(SyntaxKind.AwaitKeyword), forOf.initializer, forOf.expression, forOf.statement));
return;
}
}
if (isBinaryExpression(insertionSite)) {
for (const side of [insertionSite.left, insertionSite.right]) {
if (fixedDeclarations && isIdentifier(side)) {
Expand Down
17 changes: 17 additions & 0 deletions tests/cases/fourslash/codeFixAddMissingAwait_forAwaitOf.ts
@@ -0,0 +1,17 @@
/// <reference path="fourslash.ts" />
// @lib: es2020
// @target: es2020
////async function* g() {}
////async function fn() {
//// for (const { } of g()) { }
////}

verify.codeFix({
description: ts.Diagnostics.Add_await.message,
index: 0,
newFileContent:
`async function* g() {}
async function fn() {
for await (const { } of g()) { }
}`
});