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,153 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _helperPluginUtils = require("@babel/helper-plugin-utils");
var _helperCreateClassFeaturesPlugin = require("@babel/helper-create-class-features-plugin");
function generateUid(scope, denyList) {
const name = "";
let uid;
let i = 1;
do {
uid = `_${name}`;
if (i > 1) uid += i;
i++;
} while (denyList.has(uid));
return uid;
}
function mapLast(arr, fn) {
if (arr.length === 0) return arr;
return [...arr.slice(0, -1), fn(arr[arr.length - 1])];
}
var _default = exports.default = (0, _helperPluginUtils.declare)(({
types: t,
template,
traverse,
assertVersion
}) => {
assertVersion("^7.12.0 || ^8.0.0-0 || >8.0.0-alpha <8.0.0-beta");
const rawNamedEvaluationVisitor = (0, _helperCreateClassFeaturesPlugin.buildNamedEvaluationVisitor)(path => {
if (!path.isClassExpression()) return false;
for (let i = path.node.body.body.length - 1; i >= 0; i--) {
const el = path.node.body.body[i];
if (t.isStaticBlock(el)) {
return true;
}
if ((t.isClassProperty(el) || t.isClassPrivateProperty(el)) && el.static) {
break;
}
}
return false;
}, (classPath, state, name) => {
const nameNode = typeof name === "string" ? t.stringLiteral(name) : name;
classPath.get("body").unshiftContainer("body", t.staticBlock([template.statement.ast`
${state.addHelper("setFunctionName")}(this, ${nameNode});
`]));
});
if (!t.classAccessorProperty) {
delete rawNamedEvaluationVisitor.ClassAccessorProperty;
}
const namedEvaluationVisitor = traverse.visitors.explode(rawNamedEvaluationVisitor);
const maybeSequenceExpression = expressions => {
if (expressions.length === 1) {
return expressions[0];
} else {
return t.sequenceExpression(expressions);
}
};
const blocksToExpressions = blocks => blocks.map(block => {
const {
body
} = block;
if (body.length === 1 && t.isExpressionStatement(body[0])) {
return t.inheritsComments(t.inheritsComments(body[0].expression, body[0]), block);
}
return t.inheritsComments(template.expression.ast`(() => { ${body} })()`, block);
});
const prependToInitializer = (prop, expressions) => {
prop.value = prop.value ? t.sequenceExpression([...expressions, prop.value]) : maybeSequenceExpression(mapLast(expressions, expr => t.unaryExpression("void", expr)));
};
return {
name: "transform-class-static-block",
manipulateOptions: (_, parser) => parser.plugins.push("classStaticBlock"),
pre() {
(0, _helperCreateClassFeaturesPlugin.enableFeature)(this.file, _helperCreateClassFeaturesPlugin.FEATURES.staticBlocks, false);
},
visitor: {
ClassBody(classBody) {
const {
scope
} = classBody;
let parentPath = classBody.parentPath;
if (parentPath.isClassExpression() && !parentPath.node.id) {
do ({
parentPath
} = parentPath); while (parentPath && !namedEvaluationVisitor[parentPath.type] && !parentPath.isStatement());
if (parentPath) {
var _namedEvaluationVisit;
(_namedEvaluationVisit = namedEvaluationVisitor[parentPath.type]) == null || _namedEvaluationVisit.enter.forEach(f => f.call(this, parentPath, this));
}
}
const pendingStaticBlocks = [];
let lastStaticProp = null;
for (const path of classBody.get("body")) {
if (path.isStaticBlock()) {
pendingStaticBlocks.push(path.node);
path.remove();
} else if (path.isClassProperty({
static: true
}) || path.isClassPrivateProperty({
static: true
})) {
lastStaticProp = path;
if (pendingStaticBlocks.length > 0) {
prependToInitializer(path.node, blocksToExpressions(pendingStaticBlocks));
pendingStaticBlocks.length = 0;
}
}
}
if (pendingStaticBlocks.length > 0) {
const tmp = scope.generateDeclaredUidIdentifier("staticBlock");
let arrowBody;
const needsCompletionValue = classBody.parentPath.isExpression();
if (pendingStaticBlocks.length > 1 || pendingStaticBlocks[0].body.length === 1 && t.isExpressionStatement(pendingStaticBlocks[0].body[0])) {
const expressions = blocksToExpressions(pendingStaticBlocks);
if (needsCompletionValue) {
expressions.push(t.thisExpression());
}
arrowBody = maybeSequenceExpression(expressions);
} else {
arrowBody = t.blockStatement(pendingStaticBlocks[0].body);
if (needsCompletionValue) {
arrowBody.body.push(t.returnStatement(t.thisExpression()));
}
}
const init = template.expression.ast`${tmp} = () => ${arrowBody}`;
if (lastStaticProp) {
prependToInitializer(lastStaticProp.node, [init]);
} else {
const privateNames = new Set();
for (const path of classBody.get("body")) {
if (path.isPrivate()) {
privateNames.add(path.get("key.id").node.name);
}
}
const staticBlockPrivateId = generateUid(scope, privateNames);
const staticBlockRef = t.privateName(t.identifier(staticBlockPrivateId));
classBody.pushContainer("body", [t.classPrivateProperty(staticBlockRef, init, [], true)]);
}
const staticBlockClosureCall = t.callExpression(t.cloneNode(tmp), []);
if (classBody.parentPath.isClassExpression()) {
classBody.parentPath.replaceWith(t.sequenceExpression([classBody.parent, staticBlockClosureCall]));
} else {
classBody.parentPath.insertAfter(t.expressionStatement(staticBlockClosureCall));
}
}
}
}
};
});
//# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
{"version":3,"names":["_helperPluginUtils","require","_helperCreateClassFeaturesPlugin","generateUid","scope","denyList","name","uid","i","has","mapLast","arr","fn","length","slice","_default","exports","default","declare","types","t","template","traverse","assertVersion","rawNamedEvaluationVisitor","buildNamedEvaluationVisitor","path","isClassExpression","node","body","el","isStaticBlock","isClassProperty","isClassPrivateProperty","static","classPath","state","nameNode","stringLiteral","get","unshiftContainer","staticBlock","statement","ast","addHelper","classAccessorProperty","ClassAccessorProperty","namedEvaluationVisitor","visitors","explode","maybeSequenceExpression","expressions","sequenceExpression","blocksToExpressions","blocks","map","block","isExpressionStatement","inheritsComments","expression","prependToInitializer","prop","value","expr","unaryExpression","manipulateOptions","_","parser","plugins","push","pre","enableFeature","file","FEATURES","staticBlocks","visitor","ClassBody","classBody","parentPath","id","type","isStatement","_namedEvaluationVisit","enter","forEach","f","call","pendingStaticBlocks","lastStaticProp","remove","tmp","generateDeclaredUidIdentifier","arrowBody","needsCompletionValue","isExpression","thisExpression","blockStatement","returnStatement","init","privateNames","Set","isPrivate","add","staticBlockPrivateId","staticBlockRef","privateName","identifier","pushContainer","classPrivateProperty","staticBlockClosureCall","callExpression","cloneNode","replaceWith","parent","insertAfter","expressionStatement"],"sources":["../src/index.ts"],"sourcesContent":["import { declare } from \"@babel/helper-plugin-utils\";\nimport type { NodePath, Scope, types as t } from \"@babel/core\";\n\nimport {\n buildNamedEvaluationVisitor,\n enableFeature,\n FEATURES,\n} from \"@babel/helper-create-class-features-plugin\";\n\n/**\n * Generate a uid that is not in `denyList`\n *\n * @param {Scope} scope\n * @param {Set<string>} denyList a deny list that the generated uid should avoid\n * @returns\n */\nfunction generateUid(scope: Scope, denyList: Set<string>) {\n const name = \"\";\n let uid;\n let i = 1;\n do {\n uid = `_${name}`;\n if (i > 1) uid += i;\n i++;\n } while (denyList.has(uid));\n return uid;\n}\n\nfunction mapLast<T>(arr: T[], fn: (value: T) => T): T[] {\n if (arr.length === 0) return arr;\n return [...arr.slice(0, -1), fn(arr[arr.length - 1])];\n}\n\nexport default declare(({ types: t, template, traverse, assertVersion }) => {\n assertVersion(REQUIRED_VERSION(\"^7.12.0 || ^8.0.0-0\"));\n\n const rawNamedEvaluationVisitor = buildNamedEvaluationVisitor(\n (path: NodePath) => {\n if (!path.isClassExpression()) return false;\n for (let i = path.node.body.body.length - 1; i >= 0; i--) {\n const el = path.node.body.body[i];\n if (t.isStaticBlock(el)) {\n return true;\n }\n if (\n (t.isClassProperty(el) || t.isClassPrivateProperty(el)) &&\n el.static\n ) {\n break;\n }\n }\n return false;\n },\n (classPath: NodePath<t.ClassExpression>, state, name) => {\n const nameNode = typeof name === \"string\" ? t.stringLiteral(name) : name;\n\n classPath.get(\"body\").unshiftContainer(\n \"body\",\n t.staticBlock([\n template.statement.ast`\n ${state.addHelper(\"setFunctionName\")}(this, ${nameNode});\n `,\n ]),\n );\n },\n );\n\n if (!process.env.BABEL_8_BREAKING && !t.classAccessorProperty) {\n // For old versions of Babel 7, with no ClassAccessorProperty support.\n delete rawNamedEvaluationVisitor.ClassAccessorProperty;\n }\n\n const namedEvaluationVisitor = traverse.visitors.explode(\n rawNamedEvaluationVisitor,\n );\n\n const maybeSequenceExpression = (\n expressions: t.Expression[],\n ): t.Expression => {\n if (expressions.length === 1) {\n return expressions[0];\n } else {\n return t.sequenceExpression(expressions);\n }\n };\n\n const blocksToExpressions = (blocks: t.StaticBlock[]) =>\n blocks.map(block => {\n const { body } = block;\n if (body.length === 1 && t.isExpressionStatement(body[0])) {\n // We special-case the single expression case to avoid the iife, since\n // it's common.\n return t.inheritsComments(\n t.inheritsComments(body[0].expression, body[0]),\n block,\n );\n }\n return t.inheritsComments(\n template.expression.ast`(() => { ${body} })()`,\n block,\n );\n });\n\n const prependToInitializer = (\n prop: t.ClassProperty | t.ClassPrivateProperty,\n expressions: t.Expression[],\n ) => {\n prop.value = prop.value\n ? t.sequenceExpression([...expressions, prop.value])\n : maybeSequenceExpression(\n mapLast(expressions, expr => t.unaryExpression(\"void\", expr)),\n );\n };\n\n return {\n name: \"transform-class-static-bLine truncated