1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = void 0;
|
---|
7 |
|
---|
8 | var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
---|
9 |
|
---|
10 | var _helperRemapAsyncToGenerator = require("@babel/helper-remap-async-to-generator");
|
---|
11 |
|
---|
12 | var _pluginSyntaxAsyncGenerators = require("@babel/plugin-syntax-async-generators");
|
---|
13 |
|
---|
14 | var _core = require("@babel/core");
|
---|
15 |
|
---|
16 | var _forAwait = require("./for-await");
|
---|
17 |
|
---|
18 | var _default = (0, _helperPluginUtils.declare)(api => {
|
---|
19 | api.assertVersion(7);
|
---|
20 | const yieldStarVisitor = {
|
---|
21 | Function(path) {
|
---|
22 | path.skip();
|
---|
23 | },
|
---|
24 |
|
---|
25 | YieldExpression({
|
---|
26 | node
|
---|
27 | }, state) {
|
---|
28 | if (!node.delegate) return;
|
---|
29 | const callee = state.addHelper("asyncGeneratorDelegate");
|
---|
30 | node.argument = _core.types.callExpression(callee, [_core.types.callExpression(state.addHelper("asyncIterator"), [node.argument]), state.addHelper("awaitAsyncGenerator")]);
|
---|
31 | }
|
---|
32 |
|
---|
33 | };
|
---|
34 | const forAwaitVisitor = {
|
---|
35 | Function(path) {
|
---|
36 | path.skip();
|
---|
37 | },
|
---|
38 |
|
---|
39 | ForOfStatement(path, {
|
---|
40 | file
|
---|
41 | }) {
|
---|
42 | const {
|
---|
43 | node
|
---|
44 | } = path;
|
---|
45 | if (!node.await) return;
|
---|
46 | const build = (0, _forAwait.default)(path, {
|
---|
47 | getAsyncIterator: file.addHelper("asyncIterator")
|
---|
48 | });
|
---|
49 | const {
|
---|
50 | declar,
|
---|
51 | loop
|
---|
52 | } = build;
|
---|
53 | const block = loop.body;
|
---|
54 | path.ensureBlock();
|
---|
55 |
|
---|
56 | if (declar) {
|
---|
57 | block.body.push(declar);
|
---|
58 | }
|
---|
59 |
|
---|
60 | block.body = block.body.concat(node.body.body);
|
---|
61 |
|
---|
62 | _core.types.inherits(loop, node);
|
---|
63 |
|
---|
64 | _core.types.inherits(loop.body, node.body);
|
---|
65 |
|
---|
66 | if (build.replaceParent) {
|
---|
67 | path.parentPath.replaceWithMultiple(build.node);
|
---|
68 | } else {
|
---|
69 | path.replaceWithMultiple(build.node);
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | };
|
---|
74 | const visitor = {
|
---|
75 | Function(path, state) {
|
---|
76 | if (!path.node.async) return;
|
---|
77 | path.traverse(forAwaitVisitor, state);
|
---|
78 | if (!path.node.generator) return;
|
---|
79 | path.traverse(yieldStarVisitor, state);
|
---|
80 | (0, _helperRemapAsyncToGenerator.default)(path, {
|
---|
81 | wrapAsync: state.addHelper("wrapAsyncGenerator"),
|
---|
82 | wrapAwait: state.addHelper("awaitAsyncGenerator")
|
---|
83 | });
|
---|
84 | }
|
---|
85 |
|
---|
86 | };
|
---|
87 | return {
|
---|
88 | name: "proposal-async-generator-functions",
|
---|
89 | inherits: _pluginSyntaxAsyncGenerators.default,
|
---|
90 | visitor: {
|
---|
91 | Program(path, state) {
|
---|
92 | path.traverse(visitor, state);
|
---|
93 | }
|
---|
94 |
|
---|
95 | }
|
---|
96 | };
|
---|
97 | });
|
---|
98 |
|
---|
99 | exports.default = _default; |
---|