Debian Codegraph @

packages/babel-plugin-proposal-async-generator-functions/src/index.ts

raw
1 import { declare } from "@babel/helper-plugin-utils";
2 import remapAsyncToGenerator from "@babel/helper-remap-async-to-generator";
3 import syntaxAsyncGenerators from "@babel/plugin-syntax-async-generators";
4 import type { NodePath, Visitor } from "@babel/traverse";
5 import { traverse, types as t, type PluginPass } from "@babel/core";
6 import rewriteForAwait from "./for-await";
7 import environmentVisitor from "@babel/helper-environment-visitor";
8
9 export default declare(api => {
10 api.assertVersion(7);
11
12 const yieldStarVisitor = traverse.visitors.merge<PluginPass>([
13 {
14 ArrowFunctionExpression(path) {
15 path.skip();
16 },
17
18 YieldExpression({ node }, state) {
19 if (!node.delegate) return;
20 const asyncIter = t.callExpression(state.addHelper("asyncIterator"), [
21 node.argument,
22 ]);
23 node.argument = t.callExpression(
24 state.addHelper("asyncGeneratorDelegate"),
25 process.env.BABEL_8_BREAKING
26 ? [asyncIter]
27 : [asyncIter, state.addHelper("awaitAsyncGenerator")],
28 );
29 },
30 },
31 environmentVisitor,
32 ]);
33
34 const forAwaitVisitor = traverse.visitors.merge<PluginPass>([
35 {
36 ArrowFunctionExpression(path) {
37 path.skip();
38 },
39
40 ForOfStatement(path: NodePath<t.ForOfStatement>, { file }) {
41 const { node } = path;
42 if (!node.await) return;
43
44 const build = rewriteForAwait(path, {
45 getAsyncIterator: file.addHelper("asyncIterator"),
46 });
47
48 const { declar, loop } = build;
49 const block = loop.body as t.BlockStatement;
50
51 // ensure that it's a block so we can take all its statements
52 path.ensureBlock();
53
54 // add the value declaration to the new loop body
55 if (declar) {
56 block.body.push(declar);
57 if (path.node.body.body.length) {
58 block.body.push(t.blockStatement(path.node.body.body));
59 }
60 } else {
61 block.body.push(...path.node.body.body);
62 }
63
64 t.inherits(loop, node);
65 t.inherits(loop.body, node.body);
66
67 const p = build.replaceParent ? path.parentPath : path;
68 p.replaceWithMultiple(build.node);
69
70 // TODO: Avoid crawl
71 p.scope.parent.crawl();
72 },
73 },
74 environmentVisitor,
75 ]);
76
77 const visitor: Visitor<PluginPass> = {
78 Function(path, state) {
79 if (!path.node.async) return;
80
81 path.traverse(forAwaitVisitor, state);
82
83 if (!path.node.generator) return;
84
85 path.traverse(yieldStarVisitor, state);
86
87 // We don't need to pass the noNewArrows assumption, since
88 // async generators are never arrow functions.
89 remapAsyncToGenerator(path, {
90 wrapAsync: state.addHelper("wrapAsyncGenerator"),
91 wrapAwait: state.addHelper("awaitAsyncGenerator"),
92 });
93 },
94 };
95
96 return {
97 name: "proposal-async-generator-functions",
98 inherits: syntaxAsyncGenerators.default,
99
100 visitor: {
101 Program(path, state) {
102 // We need to traverse the ast here (instead of just vising Function
103 // in the top level visitor) because for-await needs to run before the
104 // async-to-generator plugin. This is because for-await is transpiled
105 // using "await" expressions, which are then converted to "yield".
106 //
107 // This is bad for performance, but plugin ordering will allow as to
108 // directly visit Function in the top level visitor.
109 path.traverse(visitor, state);
110 },
111 },
112 };
113 });