Befor generating

This commit is contained in:
marys committed 2026-06-01 13:17:37 +02:00
1 parent 3383f4bf4a
commit 1aa1b5f625
6756 files changed
+649946 -1

No files matched your search

+140
View File
@@ -0,0 +1,140 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = wrapFunction;
var _template = require("@babel/template");
var _t = require("@babel/types");
const {
blockStatement,
callExpression,
functionExpression,
isAssignmentPattern,
isFunctionDeclaration,
isRestElement,
returnStatement,
isCallExpression,
memberExpression,
identifier,
thisExpression,
isPattern
} = _t;
const buildAnonymousExpressionWrapper = _template.default.expression(`
(function () {
var REF = FUNCTION;
return function NAME(PARAMS) {
return REF.apply(this, arguments);
};
})()
`);
const buildNamedExpressionWrapper = _template.default.expression(`
(function () {
var REF = FUNCTION;
function NAME(PARAMS) {
return REF.apply(this, arguments);
}
return NAME;
})()
`);
const buildDeclarationWrapper = _template.default.statements(`
function NAME(PARAMS) { return REF.apply(this, arguments); }
function REF() {
REF = FUNCTION;
return REF.apply(this, arguments);
}
`);
function classOrObjectMethod(path, callId, ignoreFunctionLength) {
const node = path.node;
const body = node.body;
let params = [];
const shouldForwardParams = node.params.some(p => isPattern(p));
if (shouldForwardParams) {
params = node.params;
node.params = [];
if (!ignoreFunctionLength) {
for (const param of params) {
if (isAssignmentPattern(param) || isRestElement(param)) {
break;
}
node.params.push(path.scope.generateUidIdentifier("x"));
}
}
}
const container = functionExpression(null, params, blockStatement(body.body), true);
if (shouldForwardParams) {
body.body = [returnStatement(callExpression(memberExpression(callExpression(callId, [container]), identifier("apply")), [thisExpression(), identifier("arguments")]))];
path.get("body.body.0.argument.callee.object.arguments.0").unwrapFunctionEnvironment();
} else {
body.body = [returnStatement(callExpression(callExpression(callId, [container]), []))];
path.get("body.body.0.argument.callee.arguments.0").unwrapFunctionEnvironment();
}
node.async = false;
node.generator = false;
}
function plainFunction(inPath, callId, noNewArrows, ignoreFunctionLength, hadName) {
let path = inPath;
let node;
let functionId = null;
const nodeParams = inPath.node.params;
if (path.isArrowFunctionExpression()) {
var _path$arrowFunctionTo;
path = (_path$arrowFunctionTo = path.arrowFunctionToExpression({
noNewArrows
})) != null ? _path$arrowFunctionTo : path;
node = path.node;
} else {
node = path.node;
}
const isDeclaration = isFunctionDeclaration(node);
let built = node;
if (!isCallExpression(node)) {
functionId = node.id;
node.id = null;
node.type = "FunctionExpression";
built = callExpression(callId, [node]);
}
const params = [];
for (const param of nodeParams) {
if (isAssignmentPattern(param) || isRestElement(param)) {
break;
}
params.push(path.scope.generateUidIdentifier("x"));
}
const wrapperArgs = {
NAME: functionId || null,
REF: path.scope.generateUidIdentifier(hadName ? functionId.name : "ref"),
FUNCTION: built,
PARAMS: params
};
if (isDeclaration) {
const container = buildDeclarationWrapper(wrapperArgs);
path.replaceWith(container[0]);
path.insertAfter(container[1]);
} else {
let container;
if (hadName) {
container = buildNamedExpressionWrapper(wrapperArgs);
} else {
container = buildAnonymousExpressionWrapper(wrapperArgs);
}
if (functionId || !ignoreFunctionLength && params.length) {
path.replaceWith(container);
} else {
path.replaceWith(built);
}
}
}
function wrapFunction(path, callId, noNewArrows = true, ignoreFunctionLength = false) {
if (path.isMethod()) {
classOrObjectMethod(path, callId, ignoreFunctionLength);
} else {
var _path, _path$ensureFunctionN;
const hadName = "id" in path.node && !!path.node.id;
(_path$ensureFunctionN = (_path = path).ensureFunctionName) != null ? _path$ensureFunctionN : _path.ensureFunctionName = require("@babel/traverse").NodePath.prototype.ensureFunctionName;
path = path.ensureFunctionName(false);
plainFunction(path, callId, noNewArrows, ignoreFunctionLength, hadName);
}
}
//# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
{"version":3,"names":["_template","require","_t","blockStatement","callExpression","functionExpression","isAssignmentPattern","isFunctionDeclaration","isRestElement","returnStatement","isCallExpression","memberExpression","identifier","thisExpression","isPattern","buildAnonymousExpressionWrapper","template","expression","buildNamedExpressionWrapper","buildDeclarationWrapper","statements","classOrObjectMethod","path","callId","ignoreFunctionLength","node","body","params","shouldForwardParams","some","p","param","push","scope","generateUidIdentifier","container","get","unwrapFunctionEnvironment","async","generator","plainFunction","inPath","noNewArrows","hadName","functionId","nodeParams","isArrowFunctionExpression","_path$arrowFunctionTo","arrowFunctionToExpression","isDeclaration","built","id","type","wrapperArgs","NAME","REF","name","FUNCTION","PARAMS","replaceWith","insertAfter","length","wrapFunction","isMethod","_path","_path$ensureFunctionN","ensureFunctionName","NodePath","prototype"],"sources":["../src/index.ts"],"sourcesContent":["import type { NodePath } from \"@babel/traverse\";\nimport template from \"@babel/template\";\nimport {\n blockStatement,\n callExpression,\n functionExpression,\n isAssignmentPattern,\n isFunctionDeclaration,\n isRestElement,\n returnStatement,\n isCallExpression,\n memberExpression,\n identifier,\n thisExpression,\n isPattern,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\n\ntype ExpressionWrapperBuilder<ExtraBody extends t.Node[]> = (\n replacements?: Parameters<ReturnType<typeof template.expression>>[0],\n) => t.CallExpression & {\n callee: t.FunctionExpression & {\n body: {\n body: [\n t.VariableDeclaration & {\n declarations: [\n { init: t.FunctionExpression | t.ArrowFunctionExpression },\n ];\n },\n ...ExtraBody,\n ];\n };\n };\n};\n\nconst buildAnonymousExpressionWrapper = template.expression(`\n (function () {\n var REF = FUNCTION;\n return function NAME(PARAMS) {\n return REF.apply(this, arguments);\n };\n })()\n`) as ExpressionWrapperBuilder<\n [t.ReturnStatement & { argument: t.FunctionExpression }]\n>;\n\nconst buildNamedExpressionWrapper = template.expression(`\n (function () {\n var REF = FUNCTION;\n function NAME(PARAMS) {\n return REF.apply(this, arguments);\n }\n return NAME;\n })()\n`) as ExpressionWrapperBuilder<\n [t.FunctionDeclaration, t.ReturnStatement & { argument: t.Identifier }]\n>;\n\nconst buildDeclarationWrapper = template.statements(`\n function NAME(PARAMS) { return REF.apply(this, arguments); }\n function REF() {\n REF = FUNCTION;\n return REF.apply(this, arguments);\n }\n`);\n\nfunction classOrObjectMethod(\n path: NodePath<t.ClassMethod | t.ClassPrivateMethod | t.ObjectMethod>,\n callId: t.Expression,\n ignoreFunctionLength: boolean,\n) {\n const node = path.node;\n const body = node.body;\n\n let params: (t.Identifier | t.Pattern | t.RestElement)[] = [];\n\n // Errors thrown during argument evaluation must reject the resulting promise\n const shouldForwardParams = node.params.some(p => isPattern(p));\n\n if (shouldForwardParams) {\n params = node.params as typeof params;\n node.params = [];\n if (!ignoreFunctionLength) {\n for (const param of params) {\n if (isAssignmentPattern(param) || isRestElement(param)) {\n break;\n }\n node.params.push(path.scope.generateUidIdentifier(\"x\"));\n }\n }\n }\n\n const container = functionExpression(\n null,\n params,\n blockStatement(body.body),\n true,\n );\n\n if (shouldForwardParams) {\n // return asyncToGenerator(function*() { ... }).apply(this, arguments);\n body.body = [\n returnStatement(\n callExpression(\n memberExpression(\n callExpression(callId, [container]),\n identifier(\"apply\"),\n ),\n [thisExpression(), identifier(\"arguments\")],\n ),\n ),\n ];\n\n (\n path.get(\"body.body.0.argument.callee.object.arguments.0\") as NodePath\n ).unwrapFunctionEnvironment();\n } else {\n // return asyncToGenerator(function*() { ... })();\n body.body = [\n returnStatement(callExpression(callExpression(callId, [container]), [])),\n ];\n\n // Unwrap the wrapper IIFE's environment so super and this and such still work.\n (\n path.get(\"body.body.0.argument.callee.arguments.0\") as NodePath\n ).unwrapFunctionEnvironment();\n }\n\n // Regardless of whether or not the wrapped function is a an async method\n // or generator the outer function should not be\n node.async = false;\n node.generator = false;\n}\n\nfunction plainFunction(\n inPath: NodePath<Exclude<t.Function, t.Method>>,\n callId: t.Expression,\n noNewArrows: boolean,\n ignoreFunctionLength: boolean,\n hadName: boolean,\n) {\n let path: NodePath<\n | t.FunctionDeclaration\n | t.FunctionLine truncated