index.js 3.0 KB

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