index.js 1.8 KB

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