1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = void 0;
|
---|
7 | var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
---|
8 | var _helperRemapAsyncToGenerator = require("@babel/helper-remap-async-to-generator");
|
---|
9 | var _core = require("@babel/core");
|
---|
10 | var _traverse = require("@babel/traverse");
|
---|
11 | var _forAwait = require("./for-await.js");
|
---|
12 | var _default = exports.default = (0, _helperPluginUtils.declare)(api => {
|
---|
13 | api.assertVersion("^7.0.0-0 || >8.0.0-alpha <8.0.0-beta");
|
---|
14 | const yieldStarVisitor = _traverse.visitors.environmentVisitor({
|
---|
15 | ArrowFunctionExpression(path) {
|
---|
16 | path.skip();
|
---|
17 | },
|
---|
18 | YieldExpression({
|
---|
19 | node
|
---|
20 | }, state) {
|
---|
21 | if (!node.delegate) return;
|
---|
22 | const asyncIter = _core.types.callExpression(state.addHelper("asyncIterator"), [node.argument]);
|
---|
23 | node.argument = _core.types.callExpression(state.addHelper("asyncGeneratorDelegate"), [asyncIter, state.addHelper("awaitAsyncGenerator")]);
|
---|
24 | }
|
---|
25 | });
|
---|
26 | const forAwaitVisitor = _traverse.visitors.environmentVisitor({
|
---|
27 | ArrowFunctionExpression(path) {
|
---|
28 | path.skip();
|
---|
29 | },
|
---|
30 | ForOfStatement(path, {
|
---|
31 | file
|
---|
32 | }) {
|
---|
33 | const {
|
---|
34 | node
|
---|
35 | } = path;
|
---|
36 | if (!node.await) return;
|
---|
37 | const build = (0, _forAwait.default)(path, {
|
---|
38 | getAsyncIterator: file.addHelper("asyncIterator")
|
---|
39 | });
|
---|
40 | const {
|
---|
41 | declar,
|
---|
42 | loop
|
---|
43 | } = build;
|
---|
44 | const block = loop.body;
|
---|
45 | path.ensureBlock();
|
---|
46 | if (declar) {
|
---|
47 | block.body.push(declar);
|
---|
48 | if (path.node.body.body.length) {
|
---|
49 | block.body.push(_core.types.blockStatement(path.node.body.body));
|
---|
50 | }
|
---|
51 | } else {
|
---|
52 | block.body.push(...path.node.body.body);
|
---|
53 | }
|
---|
54 | _core.types.inherits(loop, node);
|
---|
55 | _core.types.inherits(loop.body, node.body);
|
---|
56 | const p = build.replaceParent ? path.parentPath : path;
|
---|
57 | p.replaceWithMultiple(build.node);
|
---|
58 | p.scope.parent.crawl();
|
---|
59 | }
|
---|
60 | });
|
---|
61 | const visitor = {
|
---|
62 | Function(path, state) {
|
---|
63 | if (!path.node.async) return;
|
---|
64 | path.traverse(forAwaitVisitor, state);
|
---|
65 | if (!path.node.generator) return;
|
---|
66 | path.traverse(yieldStarVisitor, state);
|
---|
67 | path.setData("@babel/plugin-transform-async-generator-functions/async_generator_function", true);
|
---|
68 | (0, _helperRemapAsyncToGenerator.default)(path, {
|
---|
69 | wrapAsync: state.addHelper("wrapAsyncGenerator"),
|
---|
70 | wrapAwait: state.addHelper("awaitAsyncGenerator")
|
---|
71 | });
|
---|
72 | }
|
---|
73 | };
|
---|
74 | return {
|
---|
75 | name: "transform-async-generator-functions",
|
---|
76 | manipulateOptions: (_, parser) => parser.plugins.push("asyncGenerators"),
|
---|
77 | visitor: {
|
---|
78 | Program(path, state) {
|
---|
79 | path.traverse(visitor, state);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | };
|
---|
83 | });
|
---|
84 |
|
---|
85 | //# sourceMappingURL=index.js.map
|
---|