index.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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()) {
  50. if (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. }
  61. return true;
  62. }
  63. return false;
  64. }
  65. }
  66. //# sourceMappingURL=index.js.map