diff --git a/packages/babel-plugin-proposal-async-generator-functions/src/index.ts b/packages/babel-plugin-proposal-async-generator-functions/src/index.ts index 432400f21d1c..d09042f72254 100644 --- a/packages/babel-plugin-proposal-async-generator-functions/src/index.ts +++ b/packages/babel-plugin-proposal-async-generator-functions/src/index.ts @@ -2,28 +2,36 @@ import { declare } from "@babel/helper-plugin-utils"; import remapAsyncToGenerator from "@babel/helper-remap-async-to-generator"; import syntaxAsyncGenerators from "@babel/plugin-syntax-async-generators"; <<<<<<< HEAD +<<<<<<< HEAD import { types as t } from "@babel/core"; import type { PluginPass } from "@babel/core"; import type { Visitor } from "@babel/traverse"; ======= import { traverse, types as t } from "@babel/core"; >>>>>>> 6548c38378 (environmentVisitor merged but issue still persists) +======= +import { traverse, types as t } from "@babel/core"; +>>>>>>> 0ac261115a1fa1edea3e2adb7b21d401f8b5fdf6 import rewriteForAwait from "./for-await"; import environmentVisitor from "@babel/helper-environment-visitor"; export default declare(api => { api.assertVersion(7); +<<<<<<< HEAD <<<<<<< HEAD const yieldStarVisitor: Visitor = { Function(path) { path.skip(); ======= +======= +>>>>>>> 0ac261115a1fa1edea3e2adb7b21d401f8b5fdf6 const yieldStarVisitor = traverse.visitors.merge([ { Function(path) { path.skip(); }, +<<<<<<< HEAD YieldExpression({ node }, state) { if (!node.delegate) return; @@ -120,6 +128,61 @@ export default declare(api => { environmentVisitor, ]); >>>>>>> 6548c38378 (environmentVisitor merged but issue still persists) +======= + + YieldExpression({ node }, state) { + if (!node.delegate) return; + const callee = state.addHelper("asyncGeneratorDelegate"); + node.argument = t.callExpression(callee, [ + t.callExpression(state.addHelper("asyncIterator"), [node.argument]), + state.addHelper("awaitAsyncGenerator"), + ]); + }, + }, + environmentVisitor, + ]); + + const forAwaitVisitor = traverse.visitors.merge([ + { + Function(path) { + path.skip(); + }, + + ForOfStatement(path, { file }) { + const { node } = path; + if (!node.await) return; + + const build = rewriteForAwait(path, { + getAsyncIterator: file.addHelper("asyncIterator"), + }); + + const { declar, loop } = build; + const block = loop.body; + + // ensure that it's a block so we can take all its statements + path.ensureBlock(); + + // add the value declaration to the new loop body + if (declar) { + block.body.push(declar); + } + + // push the rest of the original loop body onto our new body + block.body.push(...node.body.body); + + t.inherits(loop, node); + t.inherits(loop.body, node.body); + + if (build.replaceParent) { + path.parentPath.replaceWithMultiple(build.node); + } else { + path.replaceWithMultiple(build.node); + } + }, + }, + environmentVisitor, + ]); +>>>>>>> 0ac261115a1fa1edea3e2adb7b21d401f8b5fdf6 const visitor: Visitor = { Function(path, state) {