index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = wrapFunction;
  6. var _template = require("@babel/template");
  7. var _t = require("@babel/types");
  8. const {
  9. blockStatement,
  10. callExpression,
  11. functionExpression,
  12. isAssignmentPattern,
  13. isFunctionDeclaration,
  14. isRestElement,
  15. returnStatement,
  16. isCallExpression
  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(inPath, callId, noNewArrows, ignoreFunctionLength, hadName) {
  52. let path = inPath;
  53. let node;
  54. let functionId = null;
  55. const nodeParams = inPath.node.params;
  56. if (path.isArrowFunctionExpression()) {
  57. {
  58. var _path$arrowFunctionTo;
  59. path = (_path$arrowFunctionTo = path.arrowFunctionToExpression({
  60. noNewArrows
  61. })) != null ? _path$arrowFunctionTo : path;
  62. }
  63. node = path.node;
  64. } else {
  65. node = path.node;
  66. }
  67. const isDeclaration = isFunctionDeclaration(node);
  68. let built = node;
  69. if (!isCallExpression(node)) {
  70. functionId = node.id;
  71. node.id = null;
  72. node.type = "FunctionExpression";
  73. built = callExpression(callId, [node]);
  74. }
  75. const params = [];
  76. for (const param of nodeParams) {
  77. if (isAssignmentPattern(param) || isRestElement(param)) {
  78. break;
  79. }
  80. params.push(path.scope.generateUidIdentifier("x"));
  81. }
  82. const wrapperArgs = {
  83. NAME: functionId || null,
  84. REF: path.scope.generateUidIdentifier(hadName ? functionId.name : "ref"),
  85. FUNCTION: built,
  86. PARAMS: params
  87. };
  88. if (isDeclaration) {
  89. const container = buildDeclarationWrapper(wrapperArgs);
  90. path.replaceWith(container[0]);
  91. path.insertAfter(container[1]);
  92. } else {
  93. let container;
  94. if (hadName) {
  95. container = buildNamedExpressionWrapper(wrapperArgs);
  96. } else {
  97. container = buildAnonymousExpressionWrapper(wrapperArgs);
  98. }
  99. if (functionId || !ignoreFunctionLength && params.length) {
  100. path.replaceWith(container);
  101. } else {
  102. path.replaceWith(built);
  103. }
  104. }
  105. }
  106. function wrapFunction(path, callId, noNewArrows = true, ignoreFunctionLength = false) {
  107. if (path.isMethod()) {
  108. classOrObjectMethod(path, callId);
  109. } else {
  110. const hadName = "id" in path.node && !!path.node.id;
  111. {
  112. var _path, _path$ensureFunctionN;
  113. (_path$ensureFunctionN = (_path = path).ensureFunctionName) != null ? _path$ensureFunctionN : _path.ensureFunctionName = require("@babel/traverse").NodePath.prototype.ensureFunctionName;
  114. }
  115. path = path.ensureFunctionName(false);
  116. plainFunction(path, callId, noNewArrows, ignoreFunctionLength, hadName);
  117. }
  118. }
  119. //# sourceMappingURL=index.js.map