1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = wrapFunction;
|
---|
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, hadName) {
|
---|
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(hadName ? 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 (hadName) {
|
---|
96 | container = buildNamedExpressionWrapper(wrapperArgs);
|
---|
97 | } else {
|
---|
98 | container = buildAnonymousExpressionWrapper(wrapperArgs);
|
---|
99 | }
|
---|
100 | if (functionId || !ignoreFunctionLength && params.length) {
|
---|
101 | path.replaceWith(container);
|
---|
102 | } else {
|
---|
103 | path.replaceWith(built);
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 | function wrapFunction(path, callId, noNewArrows = true, ignoreFunctionLength = false) {
|
---|
108 | if (path.isMethod()) {
|
---|
109 | classOrObjectMethod(path, callId);
|
---|
110 | } else {
|
---|
111 | const hadName = "id" in path.node && !!path.node.id;
|
---|
112 | {
|
---|
113 | var _path, _path$ensureFunctionN;
|
---|
114 | (_path$ensureFunctionN = (_path = path).ensureFunctionName) != null ? _path$ensureFunctionN : _path.ensureFunctionName = require("@babel/traverse").NodePath.prototype.ensureFunctionName;
|
---|
115 | }
|
---|
116 | path = path.ensureFunctionName(false);
|
---|
117 | plainFunction(path, callId, noNewArrows, ignoreFunctionLength, hadName);
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | //# sourceMappingURL=index.js.map
|
---|