index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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) {
  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 container = wrapper({
  66. NAME: functionId || null,
  67. REF: path.scope.generateUidIdentifier(functionId ? functionId.name : "ref"),
  68. FUNCTION: built,
  69. PARAMS: node.params.reduce((acc, param) => {
  70. acc.done = acc.done || isAssignmentPattern(param) || isRestElement(param);
  71. if (!acc.done) {
  72. acc.params.push(path.scope.generateUidIdentifier("x"));
  73. }
  74. return acc;
  75. }, {
  76. params: [],
  77. done: false
  78. }).params
  79. });
  80. if (isDeclaration) {
  81. path.replaceWith(container[0]);
  82. path.insertAfter(container[1]);
  83. } else {
  84. const retFunction = container.callee.body.body[1].argument;
  85. if (!functionId) {
  86. (0, _helperFunctionName.default)({
  87. node: retFunction,
  88. parent: path.parent,
  89. scope: path.scope
  90. });
  91. }
  92. if (!retFunction || retFunction.id || node.params.length) {
  93. path.replaceWith(container);
  94. } else {
  95. path.replaceWith(built);
  96. }
  97. }
  98. }
  99. function wrapFunction(path, callId, noNewArrows = true) {
  100. if (path.isMethod()) {
  101. classOrObjectMethod(path, callId);
  102. } else {
  103. plainFunction(path, callId, noNewArrows);
  104. }
  105. }