You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.8 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = _default;
  6. var _helperWrapFunction = _interopRequireDefault(require("@babel/helper-wrap-function"));
  7. var _helperAnnotateAsPure = _interopRequireDefault(require("@babel/helper-annotate-as-pure"));
  8. var t = _interopRequireWildcard(require("@babel/types"));
  9. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  10. function _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; }
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. const awaitVisitor = {
  13. Function(path) {
  14. path.skip();
  15. },
  16. AwaitExpression(path, {
  17. wrapAwait
  18. }) {
  19. const argument = path.get("argument");
  20. if (path.parentPath.isYieldExpression()) {
  21. path.replaceWith(argument.node);
  22. return;
  23. }
  24. path.replaceWith(t.yieldExpression(wrapAwait ? t.callExpression(t.cloneNode(wrapAwait), [argument.node]) : argument.node));
  25. }
  26. };
  27. function _default(path, helpers) {
  28. path.traverse(awaitVisitor, {
  29. wrapAwait: helpers.wrapAwait
  30. });
  31. const isIIFE = checkIsIIFE(path);
  32. path.node.async = false;
  33. path.node.generator = true;
  34. (0, _helperWrapFunction.default)(path, t.cloneNode(helpers.wrapAsync));
  35. const isProperty = path.isObjectMethod() || path.isClassMethod() || path.parentPath.isObjectProperty() || path.parentPath.isClassProperty();
  36. if (!isProperty && !isIIFE && path.isExpression()) {
  37. (0, _helperAnnotateAsPure.default)(path);
  38. }
  39. function checkIsIIFE(path) {
  40. if (path.parentPath.isCallExpression({
  41. callee: path.node
  42. })) {
  43. return true;
  44. }
  45. const {
  46. parentPath
  47. } = path;
  48. if (parentPath.isMemberExpression() && t.isIdentifier(parentPath.node.property, {
  49. name: "bind"
  50. })) {
  51. const {
  52. parentPath: bindCall
  53. } = parentPath;
  54. return bindCall.isCallExpression() && bindCall.node.arguments.length === 1 && t.isThisExpression(bindCall.node.arguments[0]) && bindCall.parentPath.isCallExpression({
  55. callee: bindCall.node
  56. });
  57. }
  58. return false;
  59. }
  60. }