You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.5 KiB

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