index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = wrapFunction;
  6. var _helperFunctionName = require("@babel/helper-function-name");
  7. var _template = require("@babel/template");
  8. var _t = require("@babel/types");
  9. const {
  10. blockStatement,
  11. callExpression,
  12. functionExpression,
  13. isAssignmentPattern,
  14. isFunctionDeclaration,
  15. isRestElement,
  16. returnStatement,
  17. isCallExpression
  18. } = _t;
  19. const buildAnonymousExpressionWrapper = _template.default.expression(`
  20. (function () {
  21. var REF = FUNCTION;
  22. return function NAME(PARAMS) {
  23. return REF.apply(this, arguments);
  24. };
  25. })()
  26. `);
  27. const buildNamedExpressionWrapper = _template.default.expression(`
  28. (function () {
  29. var REF = FUNCTION;
  30. function NAME(PARAMS) {
  31. return REF.apply(this, arguments);
  32. }
  33. return NAME;
  34. })()
  35. `);
  36. const buildDeclarationWrapper = _template.default.statements(`
  37. function NAME(PARAMS) { return REF.apply(this, arguments); }
  38. function REF() {
  39. REF = FUNCTION;
  40. return REF.apply(this, arguments);
  41. }
  42. `);
  43. function classOrObjectMethod(path, callId) {
  44. const node = path.node;
  45. const body = node.body;
  46. const container = functionExpression(null, [], blockStatement(body.body), true);
  47. body.body = [returnStatement(callExpression(callExpression(callId, [container]), []))];
  48. node.async = false;
  49. node.generator = false;
  50. path.get("body.body.0.argument.callee.arguments.0").unwrapFunctionEnvironment();
  51. }
  52. function plainFunction(inPath, callId, noNewArrows, ignoreFunctionLength) {
  53. let path = inPath;
  54. let node;
  55. let functionId = null;
  56. const nodeParams = inPath.node.params;
  57. if (path.isArrowFunctionExpression()) {
  58. {
  59. var _path$arrowFunctionTo;
  60. path = (_path$arrowFunctionTo = path.arrowFunctionToExpression({
  61. noNewArrows
  62. })) != null ? _path$arrowFunctionTo : path;
  63. }
  64. node = path.node;
  65. } else {
  66. node = path.node;
  67. }
  68. const isDeclaration = isFunctionDeclaration(node);
  69. let built = node;
  70. if (!isCallExpression(node)) {
  71. functionId = node.id;
  72. node.id = null;
  73. node.type = "FunctionExpression";
  74. built = callExpression(callId, [node]);
  75. }
  76. const params = [];
  77. for (const param of nodeParams) {
  78. if (isAssignmentPattern(param) || isRestElement(param)) {
  79. break;
  80. }
  81. params.push(path.scope.generateUidIdentifier("x"));
  82. }
  83. const wrapperArgs = {
  84. NAME: functionId || null,
  85. REF: path.scope.generateUidIdentifier(functionId ? functionId.name : "ref"),
  86. FUNCTION: built,
  87. PARAMS: params
  88. };
  89. if (isDeclaration) {
  90. const container = buildDeclarationWrapper(wrapperArgs);
  91. path.replaceWith(container[0]);
  92. path.insertAfter(container[1]);
  93. } else {
  94. let container;
  95. if (functionId) {
  96. container = buildNamedExpressionWrapper(wrapperArgs);
  97. } else {
  98. container = buildAnonymousExpressionWrapper(wrapperArgs);
  99. const returnFn = container.callee.body.body[1].argument;
  100. (0, _helperFunctionName.default)({
  101. node: returnFn,
  102. parent: path.parent,
  103. scope: path.scope
  104. });
  105. functionId = returnFn.id;
  106. }
  107. if (functionId || !ignoreFunctionLength && params.length) {
  108. path.replaceWith(container);
  109. } else {
  110. path.replaceWith(built);
  111. }
  112. }
  113. }
  114. function wrapFunction(path, callId, noNewArrows = true, ignoreFunctionLength = false) {
  115. if (path.isMethod()) {
  116. classOrObjectMethod(path, callId);
  117. } else {
  118. plainFunction(path, callId, noNewArrows, ignoreFunctionLength);
  119. }
  120. }
  121. //# sourceMappingURL=index.js.map