index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = function (path, callId) {
  4. var node = path.node;
  5. if (node.generator) return;
  6. path.traverse(awaitVisitor);
  7. if (path.isClassMethod() || path.isObjectMethod()) {
  8. return classOrObjectMethod(path, callId);
  9. } else {
  10. return plainFunction(path, callId);
  11. }
  12. };
  13. var _babelHelperFunctionName = require("babel-helper-function-name");
  14. var _babelHelperFunctionName2 = _interopRequireDefault(_babelHelperFunctionName);
  15. var _babelTemplate = require("babel-template");
  16. var _babelTemplate2 = _interopRequireDefault(_babelTemplate);
  17. var _babelTypes = require("babel-types");
  18. var t = _interopRequireWildcard(_babelTypes);
  19. 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; } }
  20. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  21. /* @noflow */
  22. var buildWrapper = (0, _babelTemplate2.default)("\n (() => {\n var REF = FUNCTION;\n return function NAME(PARAMS) {\n return REF.apply(this, arguments);\n };\n })\n");
  23. 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");
  24. var awaitVisitor = {
  25. ArrowFunctionExpression: function ArrowFunctionExpression(path) {
  26. if (!path.node.async) {
  27. path.arrowFunctionToShadowed();
  28. }
  29. },
  30. AwaitExpression: function AwaitExpression(_ref) {
  31. var node = _ref.node;
  32. node.type = "YieldExpression";
  33. }
  34. };
  35. function classOrObjectMethod(path, callId) {
  36. var node = path.node;
  37. var body = node.body;
  38. node.async = false;
  39. var container = t.functionExpression(null, [], t.blockStatement(body.body), true);
  40. container.shadow = true;
  41. body.body = [t.returnStatement(t.callExpression(t.callExpression(callId, [container]), []))];
  42. }
  43. function plainFunction(path, callId) {
  44. var node = path.node;
  45. var isDeclaration = path.isFunctionDeclaration();
  46. var asyncFnId = node.id;
  47. var wrapper = buildWrapper;
  48. if (path.isArrowFunctionExpression()) {
  49. path.arrowFunctionToShadowed();
  50. } else if (!isDeclaration && asyncFnId) {
  51. wrapper = namedBuildWrapper;
  52. }
  53. node.async = false;
  54. node.generator = true;
  55. node.id = null;
  56. if (isDeclaration) {
  57. node.type = "FunctionExpression";
  58. }
  59. var built = t.callExpression(callId, [node]);
  60. var container = wrapper({
  61. NAME: asyncFnId,
  62. REF: path.scope.generateUidIdentifier("ref"),
  63. FUNCTION: built,
  64. PARAMS: node.params.map(function () {
  65. return path.scope.generateUidIdentifier("x");
  66. })
  67. }).expression;
  68. if (isDeclaration) {
  69. var declar = t.variableDeclaration("let", [t.variableDeclarator(t.identifier(asyncFnId.name), t.callExpression(container, []))]);
  70. declar._blockHoist = true;
  71. path.replaceWith(declar);
  72. } else {
  73. var retFunction = container.body.body[1].argument;
  74. if (!asyncFnId) {
  75. (0, _babelHelperFunctionName2.default)({
  76. node: retFunction,
  77. parent: path.parent,
  78. scope: path.scope
  79. });
  80. }
  81. if (!retFunction || retFunction.id || node.params.length) {
  82. // we have an inferred function id or params so we need this wrapper
  83. path.replaceWith(t.callExpression(container, []));
  84. } else {
  85. // we can omit this wrapper as the conditions it protects for do not apply
  86. path.replaceWith(built);
  87. }
  88. }
  89. }
  90. module.exports = exports["default"];