index.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. if (method && module) {
  18. return {
  19. name: "transform-async-to-generator",
  20. visitor: {
  21. Function(path, state) {
  22. if (!path.node.async || path.node.generator) return;
  23. let wrapAsync = state.methodWrapper;
  24. if (wrapAsync) {
  25. wrapAsync = _core.types.cloneNode(wrapAsync);
  26. } else {
  27. wrapAsync = state.methodWrapper = (0, _helperModuleImports.addNamed)(path, method, module);
  28. }
  29. (0, _helperRemapAsyncToGenerator.default)(path, {
  30. wrapAsync
  31. }, noNewArrows);
  32. }
  33. }
  34. };
  35. }
  36. return {
  37. name: "transform-async-to-generator",
  38. visitor: {
  39. Function(path, state) {
  40. if (!path.node.async || path.node.generator) return;
  41. (0, _helperRemapAsyncToGenerator.default)(path, {
  42. wrapAsync: state.addHelper("asyncToGenerator")
  43. }, noNewArrows);
  44. }
  45. }
  46. };
  47. });
  48. exports.default = _default;