index.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _helperPluginUtils = require("@babel/helper-plugin-utils");
  7. var _helperRemapAsyncToGenerator = require("@babel/helper-remap-async-to-generator");
  8. var _helperModuleImports = require("@babel/helper-module-imports");
  9. var _core = require("@babel/core");
  10. var _default = (0, _helperPluginUtils.declare)((api, options) => {
  11. api.assertVersion(7);
  12. const {
  13. method,
  14. module
  15. } = options;
  16. const noNewArrows = api.assumption("noNewArrows");
  17. const ignoreFunctionLength = api.assumption("ignoreFunctionLength");
  18. if (method && module) {
  19. return {
  20. name: "transform-async-to-generator",
  21. visitor: {
  22. Function(path, state) {
  23. if (!path.node.async || path.node.generator) return;
  24. let wrapAsync = state.methodWrapper;
  25. if (wrapAsync) {
  26. wrapAsync = _core.types.cloneNode(wrapAsync);
  27. } else {
  28. wrapAsync = state.methodWrapper = (0, _helperModuleImports.addNamed)(path, method, module);
  29. }
  30. (0, _helperRemapAsyncToGenerator.default)(path, {
  31. wrapAsync
  32. }, noNewArrows, ignoreFunctionLength);
  33. }
  34. }
  35. };
  36. }
  37. return {
  38. name: "transform-async-to-generator",
  39. visitor: {
  40. Function(path, state) {
  41. if (!path.node.async || path.node.generator) return;
  42. (0, _helperRemapAsyncToGenerator.default)(path, {
  43. wrapAsync: state.addHelper("asyncToGenerator")
  44. }, noNewArrows, ignoreFunctionLength);
  45. }
  46. }
  47. };
  48. });
  49. exports.default = _default;