for-await.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = function (path, helpers) {
  4. var node = path.node,
  5. scope = path.scope,
  6. parent = path.parent;
  7. var stepKey = scope.generateUidIdentifier("step");
  8. var stepValue = scope.generateUidIdentifier("value");
  9. var left = node.left;
  10. var declar = void 0;
  11. if (t.isIdentifier(left) || t.isPattern(left) || t.isMemberExpression(left)) {
  12. declar = t.expressionStatement(t.assignmentExpression("=", left, stepValue));
  13. } else if (t.isVariableDeclaration(left)) {
  14. declar = t.variableDeclaration(left.kind, [t.variableDeclarator(left.declarations[0].id, stepValue)]);
  15. }
  16. var template = buildForAwait();
  17. (0, _babelTraverse2.default)(template, forAwaitVisitor, null, {
  18. ITERATOR_HAD_ERROR_KEY: scope.generateUidIdentifier("didIteratorError"),
  19. ITERATOR_COMPLETION: scope.generateUidIdentifier("iteratorNormalCompletion"),
  20. ITERATOR_ERROR_KEY: scope.generateUidIdentifier("iteratorError"),
  21. ITERATOR_KEY: scope.generateUidIdentifier("iterator"),
  22. GET_ITERATOR: helpers.getAsyncIterator,
  23. OBJECT: node.right,
  24. STEP_VALUE: stepValue,
  25. STEP_KEY: stepKey,
  26. AWAIT: helpers.wrapAwait
  27. });
  28. template = template.body.body;
  29. var isLabeledParent = t.isLabeledStatement(parent);
  30. var tryBody = template[3].block.body;
  31. var loop = tryBody[0];
  32. if (isLabeledParent) {
  33. tryBody[0] = t.labeledStatement(parent.label, loop);
  34. }
  35. return {
  36. replaceParent: isLabeledParent,
  37. node: template,
  38. declar: declar,
  39. loop: loop
  40. };
  41. };
  42. var _babelTypes = require("babel-types");
  43. var t = _interopRequireWildcard(_babelTypes);
  44. var _babelTemplate = require("babel-template");
  45. var _babelTemplate2 = _interopRequireDefault(_babelTemplate);
  46. var _babelTraverse = require("babel-traverse");
  47. var _babelTraverse2 = _interopRequireDefault(_babelTraverse);
  48. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  49. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  50. var buildForAwait = (0, _babelTemplate2.default)("\n function* wrapper() {\n var ITERATOR_COMPLETION = true;\n var ITERATOR_HAD_ERROR_KEY = false;\n var ITERATOR_ERROR_KEY = undefined;\n try {\n for (\n var ITERATOR_KEY = GET_ITERATOR(OBJECT), STEP_KEY, STEP_VALUE;\n (\n STEP_KEY = yield AWAIT(ITERATOR_KEY.next()),\n ITERATOR_COMPLETION = STEP_KEY.done,\n STEP_VALUE = yield AWAIT(STEP_KEY.value),\n !ITERATOR_COMPLETION\n );\n ITERATOR_COMPLETION = true) {\n }\n } catch (err) {\n ITERATOR_HAD_ERROR_KEY = true;\n ITERATOR_ERROR_KEY = err;\n } finally {\n try {\n if (!ITERATOR_COMPLETION && ITERATOR_KEY.return) {\n yield AWAIT(ITERATOR_KEY.return());\n }\n } finally {\n if (ITERATOR_HAD_ERROR_KEY) {\n throw ITERATOR_ERROR_KEY;\n }\n }\n }\n }\n");
  51. var forAwaitVisitor = {
  52. noScope: true,
  53. Identifier: function Identifier(path, replacements) {
  54. if (path.node.name in replacements) {
  55. path.replaceInline(replacements[path.node.name]);
  56. }
  57. },
  58. CallExpression: function CallExpression(path, replacements) {
  59. var callee = path.node.callee;
  60. if (t.isIdentifier(callee) && callee.name === "AWAIT" && !replacements.AWAIT) {
  61. path.replaceWith(path.node.arguments[0]);
  62. }
  63. }
  64. };
  65. module.exports = exports["default"];