| 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 | memberExpression,
|
|---|
| 19 | identifier,
|
|---|
| 20 | thisExpression,
|
|---|
| 21 | isPattern
|
|---|
| 22 | } = _t;
|
|---|
| 23 | const buildAnonymousExpressionWrapper = _template.default.expression(`
|
|---|
| 24 | (function () {
|
|---|
| 25 | var REF = FUNCTION;
|
|---|
| 26 | return function NAME(PARAMS) {
|
|---|
| 27 | return REF.apply(this, arguments);
|
|---|
| 28 | };
|
|---|
| 29 | })()
|
|---|
| 30 | `);
|
|---|
| 31 | const buildNamedExpressionWrapper = _template.default.expression(`
|
|---|
| 32 | (function () {
|
|---|
| 33 | var REF = FUNCTION;
|
|---|
| 34 | function NAME(PARAMS) {
|
|---|
| 35 | return REF.apply(this, arguments);
|
|---|
| 36 | }
|
|---|
| 37 | return NAME;
|
|---|
| 38 | })()
|
|---|
| 39 | `);
|
|---|
| 40 | const buildDeclarationWrapper = _template.default.statements(`
|
|---|
| 41 | function NAME(PARAMS) { return REF.apply(this, arguments); }
|
|---|
| 42 | function REF() {
|
|---|
| 43 | REF = FUNCTION;
|
|---|
| 44 | return REF.apply(this, arguments);
|
|---|
| 45 | }
|
|---|
| 46 | `);
|
|---|
| 47 | function classOrObjectMethod(path, callId, ignoreFunctionLength) {
|
|---|
| 48 | const node = path.node;
|
|---|
| 49 | const body = node.body;
|
|---|
| 50 | let params = [];
|
|---|
| 51 | const shouldForwardParams = node.params.some(p => isPattern(p));
|
|---|
| 52 | if (shouldForwardParams) {
|
|---|
| 53 | params = node.params;
|
|---|
| 54 | node.params = [];
|
|---|
| 55 | if (!ignoreFunctionLength) {
|
|---|
| 56 | for (const param of params) {
|
|---|
| 57 | if (isAssignmentPattern(param) || isRestElement(param)) {
|
|---|
| 58 | break;
|
|---|
| 59 | }
|
|---|
| 60 | node.params.push(path.scope.generateUidIdentifier("x"));
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 | const container = functionExpression(null, params, blockStatement(body.body), true);
|
|---|
| 65 | if (shouldForwardParams) {
|
|---|
| 66 | body.body = [returnStatement(callExpression(memberExpression(callExpression(callId, [container]), identifier("apply")), [thisExpression(), identifier("arguments")]))];
|
|---|
| 67 | path.get("body.body.0.argument.callee.object.arguments.0").unwrapFunctionEnvironment();
|
|---|
| 68 | } else {
|
|---|
| 69 | body.body = [returnStatement(callExpression(callExpression(callId, [container]), []))];
|
|---|
| 70 | path.get("body.body.0.argument.callee.arguments.0").unwrapFunctionEnvironment();
|
|---|
| 71 | }
|
|---|
| 72 | node.async = false;
|
|---|
| 73 | node.generator = false;
|
|---|
| 74 | }
|
|---|
| 75 | function plainFunction(inPath, callId, noNewArrows, ignoreFunctionLength, hadName) {
|
|---|
| 76 | let path = inPath;
|
|---|
| 77 | let node;
|
|---|
| 78 | let functionId = null;
|
|---|
| 79 | const nodeParams = inPath.node.params;
|
|---|
| 80 | if (path.isArrowFunctionExpression()) {
|
|---|
| 81 | var _path$arrowFunctionTo;
|
|---|
| 82 | path = (_path$arrowFunctionTo = path.arrowFunctionToExpression({
|
|---|
| 83 | noNewArrows
|
|---|
| 84 | })) != null ? _path$arrowFunctionTo : path;
|
|---|
| 85 | node = path.node;
|
|---|
| 86 | } else {
|
|---|
| 87 | node = path.node;
|
|---|
| 88 | }
|
|---|
| 89 | const isDeclaration = isFunctionDeclaration(node);
|
|---|
| 90 | let built = node;
|
|---|
| 91 | if (!isCallExpression(node)) {
|
|---|
| 92 | functionId = node.id;
|
|---|
| 93 | node.id = null;
|
|---|
| 94 | node.type = "FunctionExpression";
|
|---|
| 95 | built = callExpression(callId, [node]);
|
|---|
| 96 | }
|
|---|
| 97 | const params = [];
|
|---|
| 98 | for (const param of nodeParams) {
|
|---|
| 99 | if (isAssignmentPattern(param) || isRestElement(param)) {
|
|---|
| 100 | break;
|
|---|
| 101 | }
|
|---|
| 102 | params.push(path.scope.generateUidIdentifier("x"));
|
|---|
| 103 | }
|
|---|
| 104 | const wrapperArgs = {
|
|---|
| 105 | NAME: functionId || null,
|
|---|
| 106 | REF: path.scope.generateUidIdentifier(hadName ? functionId.name : "ref"),
|
|---|
| 107 | FUNCTION: built,
|
|---|
| 108 | PARAMS: params
|
|---|
| 109 | };
|
|---|
| 110 | if (isDeclaration) {
|
|---|
| 111 | const container = buildDeclarationWrapper(wrapperArgs);
|
|---|
| 112 | path.replaceWith(container[0]);
|
|---|
| 113 | path.insertAfter(container[1]);
|
|---|
| 114 | } else {
|
|---|
| 115 | let container;
|
|---|
| 116 | if (hadName) {
|
|---|
| 117 | container = buildNamedExpressionWrapper(wrapperArgs);
|
|---|
| 118 | } else {
|
|---|
| 119 | container = buildAnonymousExpressionWrapper(wrapperArgs);
|
|---|
| 120 | }
|
|---|
| 121 | if (functionId || !ignoreFunctionLength && params.length) {
|
|---|
| 122 | path.replaceWith(container);
|
|---|
| 123 | } else {
|
|---|
| 124 | path.replaceWith(built);
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 | }
|
|---|
| 128 | function wrapFunction(path, callId, noNewArrows = true, ignoreFunctionLength = false) {
|
|---|
| 129 | if (path.isMethod()) {
|
|---|
| 130 | classOrObjectMethod(path, callId, ignoreFunctionLength);
|
|---|
| 131 | } else {
|
|---|
| 132 | var _path, _path$ensureFunctionN;
|
|---|
| 133 | const hadName = "id" in path.node && !!path.node.id;
|
|---|
| 134 | (_path$ensureFunctionN = (_path = path).ensureFunctionName) != null ? _path$ensureFunctionN : _path.ensureFunctionName = require("@babel/traverse").NodePath.prototype.ensureFunctionName;
|
|---|
| 135 | path = path.ensureFunctionName(false);
|
|---|
| 136 | plainFunction(path, callId, noNewArrows, ignoreFunctionLength, hadName);
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | //# sourceMappingURL=index.js.map
|
|---|