index.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. if (path.parentPath.isYieldExpression()) {
  25. path.replaceWith(argument.node);
  26. return;
  27. }
  28. path.replaceWith(yieldExpression(wrapAwait ? callExpression(cloneNode(wrapAwait), [argument.node]) : argument.node));
  29. }
  30. };
  31. function _default(path, helpers, noNewArrows) {
  32. path.traverse(awaitVisitor, {
  33. wrapAwait: helpers.wrapAwait
  34. });
  35. const isIIFE = checkIsIIFE(path);
  36. path.node.async = false;
  37. path.node.generator = true;
  38. (0, _helperWrapFunction.default)(path, cloneNode(helpers.wrapAsync), noNewArrows);
  39. const isProperty = path.isObjectMethod() || path.isClassMethod() || path.parentPath.isObjectProperty() || path.parentPath.isClassProperty();
  40. if (!isProperty && !isIIFE && path.isExpression()) {
  41. (0, _helperAnnotateAsPure.default)(path);
  42. }
  43. function checkIsIIFE(path) {
  44. if (path.parentPath.isCallExpression({
  45. callee: path.node
  46. })) {
  47. return true;
  48. }
  49. const {
  50. parentPath
  51. } = path;
  52. if (parentPath.isMemberExpression() && isIdentifier(parentPath.node.property, {
  53. name: "bind"
  54. })) {
  55. const {
  56. parentPath: bindCall
  57. } = parentPath;
  58. return bindCall.isCallExpression() && bindCall.node.arguments.length === 1 && isThisExpression(bindCall.node.arguments[0]) && bindCall.parentPath.isCallExpression({
  59. callee: bindCall.node
  60. });
  61. }
  62. return false;
  63. }
  64. }