UNPKG

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