[6a3a178] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.default = _default;
|
---|
| 7 |
|
---|
| 8 | var _helperWrapFunction = require("@babel/helper-wrap-function");
|
---|
| 9 |
|
---|
| 10 | var _helperAnnotateAsPure = require("@babel/helper-annotate-as-pure");
|
---|
| 11 |
|
---|
| 12 | var _t = require("@babel/types");
|
---|
| 13 |
|
---|
| 14 | const {
|
---|
| 15 | callExpression,
|
---|
| 16 | cloneNode,
|
---|
| 17 | isIdentifier,
|
---|
| 18 | isThisExpression,
|
---|
| 19 | yieldExpression
|
---|
| 20 | } = _t;
|
---|
| 21 | const awaitVisitor = {
|
---|
| 22 | Function(path) {
|
---|
| 23 | path.skip();
|
---|
| 24 | },
|
---|
| 25 |
|
---|
| 26 | AwaitExpression(path, {
|
---|
| 27 | wrapAwait
|
---|
| 28 | }) {
|
---|
| 29 | const argument = path.get("argument");
|
---|
| 30 |
|
---|
| 31 | if (path.parentPath.isYieldExpression()) {
|
---|
| 32 | path.replaceWith(argument.node);
|
---|
| 33 | return;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | path.replaceWith(yieldExpression(wrapAwait ? callExpression(cloneNode(wrapAwait), [argument.node]) : argument.node));
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | };
|
---|
| 40 |
|
---|
| 41 | function _default(path, helpers, noNewArrows) {
|
---|
| 42 | path.traverse(awaitVisitor, {
|
---|
| 43 | wrapAwait: helpers.wrapAwait
|
---|
| 44 | });
|
---|
| 45 | const isIIFE = checkIsIIFE(path);
|
---|
| 46 | path.node.async = false;
|
---|
| 47 | path.node.generator = true;
|
---|
| 48 | (0, _helperWrapFunction.default)(path, cloneNode(helpers.wrapAsync), noNewArrows);
|
---|
| 49 | const isProperty = path.isObjectMethod() || path.isClassMethod() || path.parentPath.isObjectProperty() || path.parentPath.isClassProperty();
|
---|
| 50 |
|
---|
| 51 | if (!isProperty && !isIIFE && path.isExpression()) {
|
---|
| 52 | (0, _helperAnnotateAsPure.default)(path);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | function checkIsIIFE(path) {
|
---|
| 56 | if (path.parentPath.isCallExpression({
|
---|
| 57 | callee: path.node
|
---|
| 58 | })) {
|
---|
| 59 | return true;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | const {
|
---|
| 63 | parentPath
|
---|
| 64 | } = path;
|
---|
| 65 |
|
---|
| 66 | if (parentPath.isMemberExpression() && isIdentifier(parentPath.node.property, {
|
---|
| 67 | name: "bind"
|
---|
| 68 | })) {
|
---|
| 69 | const {
|
---|
| 70 | parentPath: bindCall
|
---|
| 71 | } = parentPath;
|
---|
| 72 | return bindCall.isCallExpression() && bindCall.node.arguments.length === 1 && isThisExpression(bindCall.node.arguments[0]) && bindCall.parentPath.isCallExpression({
|
---|
| 73 | callee: bindCall.node
|
---|
| 74 | });
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | return false;
|
---|
| 78 | }
|
---|
| 79 | } |
---|