index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = function (path, file, helpers) {
  4. if (!helpers) {
  5. helpers = { wrapAsync: file };
  6. file = null;
  7. }
  8. path.traverse(awaitVisitor, {
  9. file: file,
  10. wrapAwait: helpers.wrapAwait
  11. });
  12. if (path.isClassMethod() || path.isObjectMethod()) {
  13. classOrObjectMethod(path, helpers.wrapAsync);
  14. } else {
  15. plainFunction(path, helpers.wrapAsync);
  16. }
  17. };
  18. var _babelHelperFunctionName = require("babel-helper-function-name");
  19. var _babelHelperFunctionName2 = _interopRequireDefault(_babelHelperFunctionName);
  20. var _babelTemplate = require("babel-template");
  21. var _babelTemplate2 = _interopRequireDefault(_babelTemplate);
  22. var _babelTypes = require("babel-types");
  23. var t = _interopRequireWildcard(_babelTypes);
  24. var _forAwait = require("./for-await");
  25. var _forAwait2 = _interopRequireDefault(_forAwait);
  26. 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; } }
  27. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  28. var buildWrapper = (0, _babelTemplate2.default)("\n (() => {\n var REF = FUNCTION;\n return function NAME(PARAMS) {\n return REF.apply(this, arguments);\n };\n })\n");
  29. var namedBuildWrapper = (0, _babelTemplate2.default)("\n (() => {\n var REF = FUNCTION;\n function NAME(PARAMS) {\n return REF.apply(this, arguments);\n }\n return NAME;\n })\n");
  30. var awaitVisitor = {
  31. Function: function Function(path) {
  32. if (path.isArrowFunctionExpression() && !path.node.async) {
  33. path.arrowFunctionToShadowed();
  34. return;
  35. }
  36. path.skip();
  37. },
  38. AwaitExpression: function AwaitExpression(_ref, _ref2) {
  39. var node = _ref.node;
  40. var wrapAwait = _ref2.wrapAwait;
  41. node.type = "YieldExpression";
  42. if (wrapAwait) {
  43. node.argument = t.callExpression(wrapAwait, [node.argument]);
  44. }
  45. },
  46. ForAwaitStatement: function ForAwaitStatement(path, _ref3) {
  47. var file = _ref3.file,
  48. wrapAwait = _ref3.wrapAwait;
  49. var node = path.node;
  50. var build = (0, _forAwait2.default)(path, {
  51. getAsyncIterator: file.addHelper("asyncIterator"),
  52. wrapAwait: wrapAwait
  53. });
  54. var declar = build.declar,
  55. loop = build.loop;
  56. var block = loop.body;
  57. path.ensureBlock();
  58. if (declar) {
  59. block.body.push(declar);
  60. }
  61. block.body = block.body.concat(node.body.body);
  62. t.inherits(loop, node);
  63. t.inherits(loop.body, node.body);
  64. if (build.replaceParent) {
  65. path.parentPath.replaceWithMultiple(build.node);
  66. path.remove();
  67. } else {
  68. path.replaceWithMultiple(build.node);
  69. }
  70. }
  71. };
  72. function classOrObjectMethod(path, callId) {
  73. var node = path.node;
  74. var body = node.body;
  75. node.async = false;
  76. var container = t.functionExpression(null, [], t.blockStatement(body.body), true);
  77. container.shadow = true;
  78. body.body = [t.returnStatement(t.callExpression(t.callExpression(callId, [container]), []))];
  79. node.generator = false;
  80. }
  81. function plainFunction(path, callId) {
  82. var node = path.node;
  83. var isDeclaration = path.isFunctionDeclaration();
  84. var asyncFnId = node.id;
  85. var wrapper = buildWrapper;
  86. if (path.isArrowFunctionExpression()) {
  87. path.arrowFunctionToShadowed();
  88. } else if (!isDeclaration && asyncFnId) {
  89. wrapper = namedBuildWrapper;
  90. }
  91. node.async = false;
  92. node.generator = true;
  93. node.id = null;
  94. if (isDeclaration) {
  95. node.type = "FunctionExpression";
  96. }
  97. var built = t.callExpression(callId, [node]);
  98. var container = wrapper({
  99. NAME: asyncFnId,
  100. REF: path.scope.generateUidIdentifier("ref"),
  101. FUNCTION: built,
  102. PARAMS: node.params.reduce(function (acc, param) {
  103. acc.done = acc.done || t.isAssignmentPattern(param) || t.isRestElement(param);
  104. if (!acc.done) {
  105. acc.params.push(path.scope.generateUidIdentifier("x"));
  106. }
  107. return acc;
  108. }, {
  109. params: [],
  110. done: false
  111. }).params
  112. }).expression;
  113. if (isDeclaration) {
  114. var declar = t.variableDeclaration("let", [t.variableDeclarator(t.identifier(asyncFnId.name), t.callExpression(container, []))]);
  115. declar._blockHoist = true;
  116. path.replaceWith(declar);
  117. } else {
  118. var retFunction = container.body.body[1].argument;
  119. if (!asyncFnId) {
  120. (0, _babelHelperFunctionName2.default)({
  121. node: retFunction,
  122. parent: path.parent,
  123. scope: path.scope
  124. });
  125. }
  126. if (!retFunction || retFunction.id || node.params.length) {
  127. path.replaceWith(t.callExpression(container, []));
  128. } else {
  129. path.replaceWith(built);
  130. }
  131. }
  132. }
  133. module.exports = exports["default"];