index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 _pluginSyntaxAsyncGenerators = require("@babel/plugin-syntax-async-generators");
  9. var _core = require("@babel/core");
  10. var _forAwait = require("./for-await");
  11. var _default = (0, _helperPluginUtils.declare)(api => {
  12. api.assertVersion(7);
  13. const yieldStarVisitor = {
  14. Function(path) {
  15. path.skip();
  16. },
  17. YieldExpression({
  18. node
  19. }, state) {
  20. if (!node.delegate) return;
  21. const callee = state.addHelper("asyncGeneratorDelegate");
  22. node.argument = _core.types.callExpression(callee, [_core.types.callExpression(state.addHelper("asyncIterator"), [node.argument]), state.addHelper("awaitAsyncGenerator")]);
  23. }
  24. };
  25. const forAwaitVisitor = {
  26. Function(path) {
  27. path.skip();
  28. },
  29. ForOfStatement(path, {
  30. file
  31. }) {
  32. const {
  33. node
  34. } = path;
  35. if (!node.await) return;
  36. const build = (0, _forAwait.default)(path, {
  37. getAsyncIterator: file.addHelper("asyncIterator")
  38. });
  39. const {
  40. declar,
  41. loop
  42. } = build;
  43. const block = loop.body;
  44. path.ensureBlock();
  45. if (declar) {
  46. block.body.push(declar);
  47. }
  48. block.body.push(...node.body.body);
  49. _core.types.inherits(loop, node);
  50. _core.types.inherits(loop.body, node.body);
  51. if (build.replaceParent) {
  52. path.parentPath.replaceWithMultiple(build.node);
  53. } else {
  54. path.replaceWithMultiple(build.node);
  55. }
  56. }
  57. };
  58. const visitor = {
  59. Function(path, state) {
  60. if (!path.node.async) return;
  61. path.traverse(forAwaitVisitor, state);
  62. if (!path.node.generator) return;
  63. path.traverse(yieldStarVisitor, state);
  64. (0, _helperRemapAsyncToGenerator.default)(path, {
  65. wrapAsync: state.addHelper("wrapAsyncGenerator"),
  66. wrapAwait: state.addHelper("awaitAsyncGenerator")
  67. });
  68. }
  69. };
  70. return {
  71. name: "proposal-async-generator-functions",
  72. inherits: _pluginSyntaxAsyncGenerators.default,
  73. visitor: {
  74. Program(path, state) {
  75. path.traverse(visitor, state);
  76. }
  77. }
  78. };
  79. });
  80. exports.default = _default;