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

@@ -0,0 +1,155 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _helperPluginUtils = require("@babel/helper-plugin-utils");
var _pluginTransformDestructuring = require("@babel/plugin-transform-destructuring");
var _core = require("@babel/core");
function isAnonymousFunctionDefinition(node) {
return _core.types.isArrowFunctionExpression(node) || (_core.types.isFunctionExpression(node) || _core.types.isClassExpression(node)) && !node.id;
}
function emitSetFunctionNameCall(state, expression, name) {
return _core.types.callExpression(state.addHelper("setFunctionName"), [expression, _core.types.stringLiteral(name)]);
}
var _default = exports.default = (0, _helperPluginUtils.declare)(api => {
api.assertVersion("^7.23.9 || ^8.0.0-0 || >8.0.0-alpha <8.0.0-beta");
const TOP_LEVEL_USING = new Map();
function isUsingDeclaration(node) {
if (!_core.types.isVariableDeclaration(node)) return false;
return node.kind === "using" || node.kind === "await using" || TOP_LEVEL_USING.has(node);
}
const transformUsingDeclarationsVisitor = {
ForOfStatement(path) {
const {
left
} = path.node;
if (!isUsingDeclaration(left)) return;
const {
id
} = left.declarations[0];
const tmpId = path.scope.generateUidIdentifierBasedOnNode(id);
left.declarations[0].id = tmpId;
left.kind = "const";
path.ensureBlock();
(0, _pluginTransformDestructuring.unshiftForXStatementBody)(path, [_core.types.variableDeclaration("using", [_core.types.variableDeclarator(id, _core.types.cloneNode(tmpId))])]);
},
"BlockStatement|StaticBlock"(path, state) {
let ctx = null;
let needsAwait = false;
const scope = path.scope;
for (const node of path.node.body) {
if (!isUsingDeclaration(node)) continue;
ctx != null ? ctx : ctx = scope.generateUidIdentifier("usingCtx");
const isAwaitUsing = node.kind === "await using" || TOP_LEVEL_USING.get(node) === 1;
needsAwait || (needsAwait = isAwaitUsing);
if (!TOP_LEVEL_USING.delete(node)) {
node.kind = "const";
}
for (const decl of node.declarations) {
const currentInit = decl.init;
decl.init = _core.types.callExpression(_core.types.memberExpression(_core.types.cloneNode(ctx), isAwaitUsing ? _core.types.identifier("a") : _core.types.identifier("u")), [isAnonymousFunctionDefinition(currentInit) && _core.types.isIdentifier(decl.id) ? emitSetFunctionNameCall(state, currentInit, decl.id.name) : currentInit]);
}
}
if (!ctx) return;
const disposeCall = _core.types.callExpression(_core.types.memberExpression(_core.types.cloneNode(ctx), _core.types.identifier("d")), []);
const replacement = _core.template.statement.ast`
try {
var ${_core.types.cloneNode(ctx)} = ${state.addHelper("usingCtx")}();
${path.node.body}
} catch (_) {
${_core.types.cloneNode(ctx)}.e = _;
} finally {
${needsAwait ? _core.types.awaitExpression(disposeCall) : disposeCall}
}
`;
_core.types.inherits(replacement, path.node);
const {
parentPath
} = path;
if (parentPath.isFunction() || parentPath.isTryStatement() || parentPath.isCatchClause()) {
path.replaceWith(_core.types.blockStatement([replacement]));
} else if (path.isStaticBlock()) {
path.node.body = [replacement];
} else {
path.replaceWith(replacement);
}
}
};
const transformUsingDeclarationsVisitorSkipFn = _core.traverse.visitors.merge([transformUsingDeclarationsVisitor, {
Function(path) {
path.skip();
}
}]);
return {
name: "transform-explicit-resource-management",
manipulateOptions: (_, p) => p.plugins.push("explicitResourceManagement"),
visitor: _core.traverse.visitors.merge([transformUsingDeclarationsVisitor, {
Program(path) {
TOP_LEVEL_USING.clear();
if (path.node.sourceType !== "module") return;
if (!path.node.body.some(isUsingDeclaration)) return;
const innerBlockBody = [];
for (const stmt of path.get("body")) {
if (stmt.isFunctionDeclaration() || stmt.isImportDeclaration()) {
continue;
}
let node = stmt.node;
let shouldRemove = true;
if (stmt.isExportDefaultDeclaration()) {
let {
declaration
} = stmt.node;
let varId;
if (_core.types.isClassDeclaration(declaration)) {
varId = declaration.id;
declaration.id = _core.types.cloneNode(varId);
declaration = _core.types.toExpression(declaration);
} else if (!_core.types.isExpression(declaration)) {
continue;
}
varId != null ? varId : varId = path.scope.generateUidIdentifier("_default");
innerBlockBody.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(varId, declaration)]));
stmt.replaceWith(_core.types.exportNamedDeclaration(null, [_core.types.exportSpecifier(_core.types.cloneNode(varId), _core.types.identifier("default"))]));
continue;
}
if (stmt.isExportNamedDeclaration()) {
node = stmt.node.declaration;
if (!node || _core.types.isFunction(node)) continue;
stmt.replaceWith(_core.types.exportNamedDeclaration(null, Object.keys(_core.types.getOuterBindingIdentifiers(node, false)).map(id => _core.types.exportSpecifier(_core.types.identifier(id), _core.types.identifier(id)))));
shouldRemove = false;
} else if (stmt.isExportDeclaration()) {
continue;
}
if (_core.types.isClassDeclaration(node)) {
const {
id
} = node;
node.id = _core.types.cloneNode(id);
innerBlockBody.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(id, _core.types.toExpression(node))]));
} else if (_core.types.isVariableDeclaration(node)) {
if (node.kind === "using") {
TOP_LEVEL_USING.set(stmt.node, 0);
} else if (node.kind === "await using") {
TOP_LEVEL_USING.set(stmt.node, 1);
}
node.kind = "var";
innerBlockBody.push(node);
} else {
innerBlockBody.push(stmt.node);
}
if (shouldRemove) stmt.remove();
}
path.pushContainer("body", _core.types.blockStatement(innerBlockBody));
},
Function(path, state) {
if (path.node.async) {
path.traverse(transformUsingDeclarationsVisitorSkipFn, state);
}
}
}])
};
});
//# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
{"version":3,"names":["_helperPluginUtils","require","_pluginTransformDestructuring","_core","isAnonymousFunctionDefinition","node","t","isArrowFunctionExpression","isFunctionExpression","isClassExpression","id","emitSetFunctionNameCall","state","expression","name","callExpression","addHelper","stringLiteral","_default","exports","default","declare","api","assertVersion","TOP_LEVEL_USING","Map","isUsingDeclaration","isVariableDeclaration","kind","has","transformUsingDeclarationsVisitor","ForOfStatement","path","left","declarations","tmpId","scope","generateUidIdentifierBasedOnNode","ensureBlock","unshiftForXStatementBody","variableDeclaration","variableDeclarator","cloneNode","BlockStatement|StaticBlock","ctx","needsAwait","body","generateUidIdentifier","isAwaitUsing","get","delete","decl","currentInit","init","memberExpression","identifier","isIdentifier","disposeCall","replacement","template","statement","ast","awaitExpression","inherits","parentPath","isFunction","isTryStatement","isCatchClause","replaceWith","blockStatement","isStaticBlock","transformUsingDeclarationsVisitorSkipFn","traverse","visitors","merge","Function","skip","manipulateOptions","_","p","plugins","push","visitor","Program","clear","sourceType","some","innerBlockBody","stmt","isFunctionDeclaration","isImportDeclaration","shouldRemove","isExportDefaultDeclaration","declaration","varId","isClassDeclaration","toExpression","isExpression","exportNamedDeclaration","exportSpecifier","isExportNamedDeclaration","Object","keys","getOuterBindingIdentifiers","map","isExportDeclaration","set","remove","pushContainer","async"],"sources":["../src/index.ts"],"sourcesContent":["import { declare } from \"@babel/helper-plugin-utils\";\nimport { unshiftForXStatementBody } from \"@babel/plugin-transform-destructuring\";\nimport { types as t, template, traverse } from \"@babel/core\";\nimport type { NodePath, Visitor, PluginPass } from \"@babel/core\";\n\nconst enum USING_KIND {\n NORMAL,\n AWAIT,\n}\n\n// https://tc39.es/ecma262/#sec-isanonymousfunctiondefinition\nfunction isAnonymousFunctionDefinition(\n node: t.Node,\n): node is\n | t.ClassExpression\n | t.ArrowFunctionExpression\n | t.FunctionExpression {\n return (\n t.isArrowFunctionExpression(node) ||\n ((t.isFunctionExpression(node) || t.isClassExpression(node)) && !node.id)\n );\n}\n\nfunction emitSetFunctionNameCall(\n state: PluginPass,\n expression: t.Expression,\n name: string,\n) {\n return t.callExpression(state.addHelper(\"setFunctionName\"), [\n expression,\n t.stringLiteral(name),\n ]);\n}\n\nexport default declare(api => {\n // The first Babel 7 version with usingCtx helper support.\n api.assertVersion(REQUIRED_VERSION(\"^7.23.9 || ^8.0.0-0\"));\n\n const TOP_LEVEL_USING = new Map<t.Node, USING_KIND>();\n\n function isUsingDeclaration(node: t.Node): node is t.VariableDeclaration {\n if (!t.isVariableDeclaration(node)) return false;\n return (\n node.kind === \"using\" ||\n node.kind === \"await using\" ||\n TOP_LEVEL_USING.has(node)\n );\n }\n\n const transformUsingDeclarationsVisitor: Visitor<PluginPass> = {\n ForOfStatement(path: NodePath<t.ForOfStatement>) {\n const { left } = path.node;\n if (!isUsingDeclaration(left)) return;\n\n const { id } = left.declarations[0];\n const tmpId = path.scope.generateUidIdentifierBasedOnNode(id);\n left.declarations[0].id = tmpId;\n left.kind = \"const\";\n\n path.ensureBlock();\n unshiftForXStatementBody(path, [\n t.variableDeclaration(\"using\", [\n t.variableDeclarator(id, t.cloneNode(tmpId)),\n ]),\n ]);\n },\n \"BlockStatement|StaticBlock\"(\n path: NodePath<t.BlockStatement | t.StaticBlock>,\n state,\n ) {\n let ctx: t.Identifier | null = null;\n let needsAwait = false;\n const scope = path.scope;\n\n for (const node of path.node.body) {\n if (!isUsingDeclaration(node)) continue;\n ctx ??= scope.generateUidIdentifier(\"usingCtx\");\n const isAwaitUsing =\n node.kind === \"await using\" ||\n TOP_LEVEL_USING.get(node) === USING_KIND.AWAIT;\n needsAwait ||= isAwaitUsing;\n\n if (!TOP_LEVEL_USING.delete(node)) {\n node.kind = \"const\";\n }\n for (const decl of node.declarations) {\n const currentInit = decl.init;\n decl.init = t.callExpression(\n t.memberExpression(\n t.cloneNode(ctx),\n isAwaitUsing ? t.identifier(\"a\") : t.identifier(\"u\"),\n ),\n [\n isAnonymousFunctionDefinition(currentInit) &&\n t.isIdentifier(decl.id)\n ? emitSetFunctionNameCall(state, currentInit, decl.id.name)\n : currentInit,\n ],\n );\n }\n }\n if (!ctx) return;\n\n const disposeCall = t.callExpression(\n t.memberExpression(t.cloneNode(ctx), t.identifLine truncated