for-await.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_ABRUPT_COMPLETION = false;
  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;
  15. ITERATOR_ABRUPT_COMPLETION = !(STEP_KEY = await ITERATOR_KEY.next()).done;
  16. ITERATOR_ABRUPT_COMPLETION = false
  17. ) {
  18. }
  19. } catch (err) {
  20. ITERATOR_HAD_ERROR_KEY = true;
  21. ITERATOR_ERROR_KEY = err;
  22. } finally {
  23. try {
  24. if (ITERATOR_ABRUPT_COMPLETION && ITERATOR_KEY.return != null) {
  25. await ITERATOR_KEY.return();
  26. }
  27. } finally {
  28. if (ITERATOR_HAD_ERROR_KEY) {
  29. throw ITERATOR_ERROR_KEY;
  30. }
  31. }
  32. }
  33. }
  34. `);
  35. function _default(path, {
  36. getAsyncIterator
  37. }) {
  38. const {
  39. node,
  40. scope,
  41. parent
  42. } = path;
  43. const stepKey = scope.generateUidIdentifier("step");
  44. const stepValue = _core.types.memberExpression(stepKey, _core.types.identifier("value"));
  45. const left = node.left;
  46. let declar;
  47. if (_core.types.isIdentifier(left) || _core.types.isPattern(left) || _core.types.isMemberExpression(left)) {
  48. declar = _core.types.expressionStatement(_core.types.assignmentExpression("=", left, stepValue));
  49. } else if (_core.types.isVariableDeclaration(left)) {
  50. declar = _core.types.variableDeclaration(left.kind, [_core.types.variableDeclarator(left.declarations[0].id, stepValue)]);
  51. }
  52. let template = buildForAwait({
  53. ITERATOR_HAD_ERROR_KEY: scope.generateUidIdentifier("didIteratorError"),
  54. ITERATOR_ABRUPT_COMPLETION: scope.generateUidIdentifier("iteratorAbruptCompletion"),
  55. ITERATOR_ERROR_KEY: scope.generateUidIdentifier("iteratorError"),
  56. ITERATOR_KEY: scope.generateUidIdentifier("iterator"),
  57. GET_ITERATOR: getAsyncIterator,
  58. OBJECT: node.right,
  59. STEP_KEY: _core.types.cloneNode(stepKey)
  60. });
  61. template = template.body.body;
  62. const isLabeledParent = _core.types.isLabeledStatement(parent);
  63. const tryBody = template[3].block.body;
  64. const loop = tryBody[0];
  65. if (isLabeledParent) {
  66. tryBody[0] = _core.types.labeledStatement(parent.label, loop);
  67. }
  68. return {
  69. replaceParent: isLabeledParent,
  70. node: template,
  71. declar,
  72. loop
  73. };
  74. }
  75. //# sourceMappingURL=for-await.js.map