index.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 _core = require("@babel/core");
  9. var _traverse = require("@babel/traverse");
  10. const {
  11. callExpression,
  12. cloneNode,
  13. isIdentifier,
  14. isThisExpression,
  15. yieldExpression
  16. } = _core.types;
  17. const awaitVisitor = _traverse.visitors.environmentVisitor({
  18. ArrowFunctionExpression(path) {
  19. path.skip();
  20. },
  21. AwaitExpression(path, {
  22. wrapAwait
  23. }) {
  24. const argument = path.get("argument");
  25. path.replaceWith(yieldExpression(wrapAwait ? callExpression(cloneNode(wrapAwait), [argument.node]) : argument.node));
  26. }
  27. });
  28. function _default(path, helpers, noNewArrows, ignoreFunctionLength) {
  29. path.traverse(awaitVisitor, {
  30. wrapAwait: helpers.wrapAwait
  31. });
  32. const isIIFE = checkIsIIFE(path);
  33. path.node.async = false;
  34. path.node.generator = true;
  35. (0, _helperWrapFunction.default)(path, cloneNode(helpers.wrapAsync), noNewArrows, ignoreFunctionLength);
  36. const isProperty = path.isObjectMethod() || path.isClassMethod() || path.parentPath.isObjectProperty() || path.parentPath.isClassProperty();
  37. if (!isProperty && !isIIFE && path.isExpression()) {
  38. (0, _helperAnnotateAsPure.default)(path);
  39. }
  40. function checkIsIIFE(path) {
  41. if (path.parentPath.isCallExpression({
  42. callee: path.node
  43. })) {
  44. return true;
  45. }
  46. const {
  47. parentPath
  48. } = path;
  49. if (parentPath.isMemberExpression() && isIdentifier(parentPath.node.property, {
  50. name: "bind"
  51. })) {
  52. const {
  53. parentPath: bindCall
  54. } = parentPath;
  55. return bindCall.isCallExpression() && bindCall.node.arguments.length === 1 && isThisExpression(bindCall.node.arguments[0]) && bindCall.parentPath.isCallExpression({
  56. callee: bindCall.node
  57. });
  58. }
  59. return false;
  60. }
  61. }
  62. //# sourceMappingURL=index.js.map