index.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 _t = require("@babel/types");
  9. const {
  10. callExpression,
  11. cloneNode,
  12. isIdentifier,
  13. isThisExpression,
  14. yieldExpression
  15. } = _t;
  16. const awaitVisitor = {
  17. Function(path) {
  18. path.skip();
  19. },
  20. AwaitExpression(path, {
  21. wrapAwait
  22. }) {
  23. const argument = path.get("argument");
  24. path.replaceWith(yieldExpression(wrapAwait ? callExpression(cloneNode(wrapAwait), [argument.node]) : argument.node));
  25. }
  26. };
  27. function _default(path, helpers, noNewArrows, ignoreFunctionLength) {
  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, cloneNode(helpers.wrapAsync), noNewArrows, ignoreFunctionLength);
  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() && isIdentifier(parentPath.node.property, {
  49. name: "bind"
  50. })) {
  51. const {
  52. parentPath: bindCall
  53. } = parentPath;
  54. return bindCall.isCallExpression() && bindCall.node.arguments.length === 1 && isThisExpression(bindCall.node.arguments[0]) && bindCall.parentPath.isCallExpression({
  55. callee: bindCall.node
  56. });
  57. }
  58. return false;
  59. }
  60. }