index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. } = _t;
  18. const buildAnonymousExpressionWrapper = _template.default.expression(`
  19. (function () {
  20. var REF = FUNCTION;
  21. return function NAME(PARAMS) {
  22. return REF.apply(this, arguments);
  23. };
  24. })()
  25. `);
  26. const buildNamedExpressionWrapper = _template.default.expression(`
  27. (function () {
  28. var REF = FUNCTION;
  29. function NAME(PARAMS) {
  30. return REF.apply(this, arguments);
  31. }
  32. return NAME;
  33. })()
  34. `);
  35. const buildDeclarationWrapper = _template.default.statements(`
  36. function NAME(PARAMS) { return REF.apply(this, arguments); }
  37. function REF() {
  38. REF = FUNCTION;
  39. return REF.apply(this, arguments);
  40. }
  41. `);
  42. function classOrObjectMethod(path, callId) {
  43. const node = path.node;
  44. const body = node.body;
  45. const container = functionExpression(null, [], blockStatement(body.body), true);
  46. body.body = [returnStatement(callExpression(callExpression(callId, [container]), []))];
  47. node.async = false;
  48. node.generator = false;
  49. path.get("body.body.0.argument.callee.arguments.0").unwrapFunctionEnvironment();
  50. }
  51. function plainFunction(path, callId, noNewArrows, ignoreFunctionLength) {
  52. let functionId = null;
  53. let node;
  54. if (path.isArrowFunctionExpression()) {
  55. {
  56. var _path$arrowFunctionTo;
  57. path = (_path$arrowFunctionTo = path.arrowFunctionToExpression({
  58. noNewArrows
  59. })) != null ? _path$arrowFunctionTo : path;
  60. }
  61. node = path.node;
  62. } else {
  63. node = path.node;
  64. }
  65. const isDeclaration = isFunctionDeclaration(node);
  66. functionId = node.id;
  67. node.id = null;
  68. node.type = "FunctionExpression";
  69. const built = callExpression(callId, [node]);
  70. const params = [];
  71. for (const param of node.params) {
  72. if (isAssignmentPattern(param) || isRestElement(param)) {
  73. break;
  74. }
  75. params.push(path.scope.generateUidIdentifier("x"));
  76. }
  77. const wrapperArgs = {
  78. NAME: functionId || null,
  79. REF: path.scope.generateUidIdentifier(functionId ? functionId.name : "ref"),
  80. FUNCTION: built,
  81. PARAMS: params
  82. };
  83. if (isDeclaration) {
  84. const container = buildDeclarationWrapper(wrapperArgs);
  85. path.replaceWith(container[0]);
  86. path.insertAfter(container[1]);
  87. } else {
  88. let container;
  89. if (functionId) {
  90. container = buildNamedExpressionWrapper(wrapperArgs);
  91. } else {
  92. container = buildAnonymousExpressionWrapper(wrapperArgs);
  93. const returnFn = container.callee.body.body[1].argument;
  94. (0, _helperFunctionName.default)({
  95. node: returnFn,
  96. parent: path.parent,
  97. scope: path.scope
  98. });
  99. functionId = returnFn.id;
  100. }
  101. if (functionId || !ignoreFunctionLength && params.length) {
  102. path.replaceWith(container);
  103. } else {
  104. path.replaceWith(built);
  105. }
  106. }
  107. }
  108. function wrapFunction(path, callId, noNewArrows = true, ignoreFunctionLength = false) {
  109. if (path.isMethod()) {
  110. classOrObjectMethod(path, callId);
  111. } else {
  112. plainFunction(path, callId, noNewArrows, ignoreFunctionLength);
  113. }
  114. }