index.js 2.7 KB

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