index.js 2.0 KB

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