diff --git a/packages/eslint-plugin/src/rules/require-await.ts b/packages/eslint-plugin/src/rules/require-await.ts index f834e17decec..dd033c7cbe2d 100644 --- a/packages/eslint-plugin/src/rules/require-await.ts +++ b/packages/eslint-plugin/src/rules/require-await.ts @@ -112,18 +112,27 @@ export default createRule({ } /** - * mark `scopeInfo.isAsyncYield` to `true` if its a generator - * function and the delegate is `true` + * Mark `scopeInfo.isAsyncYield` to `true` if it + * 1) delegates async generator function + * or + * 2) yields thenable type */ - function markAsHasDelegateGen(node: TSESTree.YieldExpression): void { + function visitYieldExpression(node: TSESTree.YieldExpression): void { if (!scopeInfo?.isGen || !node.argument) { return; } if (node.argument.type === AST_NODE_TYPES.Literal) { - // making this `false` as for literals we don't need to check the definition + // ignoring this as for literals we don't need to check the definition // eg : async function* run() { yield* 1 } - scopeInfo.isAsyncYield ||= false; + return; + } + + if (!node.delegate) { + if (isThenableType(services.esTreeNodeToTSNodeMap.get(node.argument))) { + scopeInfo.isAsyncYield = true; + } + return; } const type = services.getTypeAtLocation(node.argument); @@ -152,7 +161,7 @@ export default createRule({ AwaitExpression: markAsHasAwait, 'VariableDeclaration[kind = "await using"]': markAsHasAwait, 'ForOfStatement[await = true]': markAsHasAwait, - 'YieldExpression[delegate = true]': markAsHasDelegateGen, + YieldExpression: visitYieldExpression, // check body-less async arrow function. // ignore `async () => await foo` because it's obviously correct diff --git a/packages/eslint-plugin/tests/rules/require-await.test.ts b/packages/eslint-plugin/tests/rules/require-await.test.ts index 7bcb1d6a0b4f..f8f9624e26c8 100644 --- a/packages/eslint-plugin/tests/rules/require-await.test.ts +++ b/packages/eslint-plugin/tests/rules/require-await.test.ts @@ -237,6 +237,30 @@ async function* foo(): Promise { await using foo = new Bar(); }; `, + ` + async function* test1() { + yield Promise.resolve(1); + } + `, + ` + function asyncFunction() { + return Promise.resolve(1); + } + async function* test1() { + yield asyncFunction(); + } + `, + ` + declare const asyncFunction: () => Promise; + async function* test1() { + yield asyncFunction(); + } + `, + ` + async function* test1() { + yield new Promise(() => {}); + } + `, ], invalid: [ pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy