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.

88 lines
2.4 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = _default;
  6. var _core = require("@babel/core");
  7. const buildForAwait = (0, _core.template)(`
  8. async function wrapper() {
  9. var ITERATOR_COMPLETION = true;
  10. var ITERATOR_HAD_ERROR_KEY = false;
  11. var ITERATOR_ERROR_KEY;
  12. try {
  13. for (
  14. var ITERATOR_KEY = GET_ITERATOR(OBJECT), STEP_KEY, STEP_VALUE;
  15. (
  16. STEP_KEY = await ITERATOR_KEY.next(),
  17. ITERATOR_COMPLETION = STEP_KEY.done,
  18. STEP_VALUE = await STEP_KEY.value,
  19. !ITERATOR_COMPLETION
  20. );
  21. ITERATOR_COMPLETION = true) {
  22. }
  23. } catch (err) {
  24. ITERATOR_HAD_ERROR_KEY = true;
  25. ITERATOR_ERROR_KEY = err;
  26. } finally {
  27. try {
  28. if (!ITERATOR_COMPLETION && ITERATOR_KEY.return != null) {
  29. await ITERATOR_KEY.return();
  30. }
  31. } finally {
  32. if (ITERATOR_HAD_ERROR_KEY) {
  33. throw ITERATOR_ERROR_KEY;
  34. }
  35. }
  36. }
  37. }
  38. `);
  39. function _default(path, {
  40. getAsyncIterator
  41. }) {
  42. const {
  43. node,
  44. scope,
  45. parent
  46. } = path;
  47. const stepKey = scope.generateUidIdentifier("step");
  48. const stepValue = scope.generateUidIdentifier("value");
  49. const left = node.left;
  50. let declar;
  51. if (_core.types.isIdentifier(left) || _core.types.isPattern(left) || _core.types.isMemberExpression(left)) {
  52. declar = _core.types.expressionStatement(_core.types.assignmentExpression("=", left, stepValue));
  53. } else if (_core.types.isVariableDeclaration(left)) {
  54. declar = _core.types.variableDeclaration(left.kind, [_core.types.variableDeclarator(left.declarations[0].id, stepValue)]);
  55. }
  56. let template = buildForAwait({
  57. ITERATOR_HAD_ERROR_KEY: scope.generateUidIdentifier("didIteratorError"),
  58. ITERATOR_COMPLETION: scope.generateUidIdentifier("iteratorNormalCompletion"),
  59. ITERATOR_ERROR_KEY: scope.generateUidIdentifier("iteratorError"),
  60. ITERATOR_KEY: scope.generateUidIdentifier("iterator"),
  61. GET_ITERATOR: getAsyncIterator,
  62. OBJECT: node.right,
  63. STEP_VALUE: stepValue,
  64. STEP_KEY: stepKey
  65. });
  66. template = template.body.body;
  67. const isLabeledParent = _core.types.isLabeledStatement(parent);
  68. const tryBody = template[3].block.body;
  69. const loop = tryBody[0];
  70. if (isLabeledParent) {
  71. tryBody[0] = _core.types.labeledStatement(parent.label, loop);
  72. }
  73. return {
  74. replaceParent: isLabeledParent,
  75. node: template,
  76. declar,
  77. loop
  78. };
  79. }