1
0
Fork 0
management/front/dkha-web-sz-main/node_modules/@babel/helper-wrap-function/lib/index.js

124 lines
3.5 KiB
JavaScript
Raw Permalink Normal View History

2023-12-18 13:12:25 +08:00
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = wrapFunction;
2024-01-16 21:26:16 +08:00
var _helperFunctionName = require("@babel/helper-function-name");
var _template = require("@babel/template");
var _t = require("@babel/types");
const {
blockStatement,
callExpression,
functionExpression,
isAssignmentPattern,
isFunctionDeclaration,
isRestElement,
returnStatement,
isCallExpression
} = _t;
const buildAnonymousExpressionWrapper = _template.default.expression(`
2023-12-18 13:12:25 +08:00
(function () {
var REF = FUNCTION;
return function NAME(PARAMS) {
return REF.apply(this, arguments);
};
})()
`);
2024-01-16 21:26:16 +08:00
const buildNamedExpressionWrapper = _template.default.expression(`
2023-12-18 13:12:25 +08:00
(function () {
var REF = FUNCTION;
function NAME(PARAMS) {
return REF.apply(this, arguments);
}
return NAME;
})()
`);
2024-01-16 21:26:16 +08:00
const buildDeclarationWrapper = _template.default.statements(`
2023-12-18 13:12:25 +08:00
function NAME(PARAMS) { return REF.apply(this, arguments); }
function REF() {
REF = FUNCTION;
return REF.apply(this, arguments);
}
`);
function classOrObjectMethod(path, callId) {
const node = path.node;
const body = node.body;
2024-01-16 21:26:16 +08:00
const container = functionExpression(null, [], blockStatement(body.body), true);
body.body = [returnStatement(callExpression(callExpression(callId, [container]), []))];
2023-12-18 13:12:25 +08:00
node.async = false;
node.generator = false;
path.get("body.body.0.argument.callee.arguments.0").unwrapFunctionEnvironment();
}
2024-01-16 21:26:16 +08:00
function plainFunction(inPath, callId, noNewArrows, ignoreFunctionLength) {
let path = inPath;
let node;
let functionId = null;
const nodeParams = inPath.node.params;
2023-12-18 13:12:25 +08:00
if (path.isArrowFunctionExpression()) {
2024-01-16 21:26:16 +08:00
{
var _path$arrowFunctionTo;
path = (_path$arrowFunctionTo = path.arrowFunctionToExpression({
noNewArrows
})) != null ? _path$arrowFunctionTo : path;
}
node = path.node;
} else {
node = path.node;
2023-12-18 13:12:25 +08:00
}
2024-01-16 21:26:16 +08:00
const isDeclaration = isFunctionDeclaration(node);
let built = node;
if (!isCallExpression(node)) {
functionId = node.id;
node.id = null;
2023-12-18 13:12:25 +08:00
node.type = "FunctionExpression";
2024-01-16 21:26:16 +08:00
built = callExpression(callId, [node]);
2023-12-18 13:12:25 +08:00
}
2024-01-16 21:26:16 +08:00
const params = [];
for (const param of nodeParams) {
if (isAssignmentPattern(param) || isRestElement(param)) {
break;
}
params.push(path.scope.generateUidIdentifier("x"));
}
const wrapperArgs = {
2023-12-18 13:12:25 +08:00
NAME: functionId || null,
REF: path.scope.generateUidIdentifier(functionId ? functionId.name : "ref"),
FUNCTION: built,
2024-01-16 21:26:16 +08:00
PARAMS: params
};
2023-12-18 13:12:25 +08:00
if (isDeclaration) {
2024-01-16 21:26:16 +08:00
const container = buildDeclarationWrapper(wrapperArgs);
2023-12-18 13:12:25 +08:00
path.replaceWith(container[0]);
path.insertAfter(container[1]);
} else {
2024-01-16 21:26:16 +08:00
let container;
if (functionId) {
container = buildNamedExpressionWrapper(wrapperArgs);
} else {
container = buildAnonymousExpressionWrapper(wrapperArgs);
const returnFn = container.callee.body.body[1].argument;
(0, _helperFunctionName.default)({
node: returnFn,
2023-12-18 13:12:25 +08:00
parent: path.parent,
scope: path.scope
});
2024-01-16 21:26:16 +08:00
functionId = returnFn.id;
2023-12-18 13:12:25 +08:00
}
2024-01-16 21:26:16 +08:00
if (functionId || !ignoreFunctionLength && params.length) {
2023-12-18 13:12:25 +08:00
path.replaceWith(container);
} else {
path.replaceWith(built);
}
}
}
2024-01-16 21:26:16 +08:00
function wrapFunction(path, callId, noNewArrows = true, ignoreFunctionLength = false) {
if (path.isMethod()) {
2023-12-18 13:12:25 +08:00
classOrObjectMethod(path, callId);
} else {
2024-01-16 21:26:16 +08:00
plainFunction(path, callId, noNewArrows, ignoreFunctionLength);
2023-12-18 13:12:25 +08:00
}
2024-01-16 21:26:16 +08:00
}
//# sourceMappingURL=index.js.map