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,82 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _helperPluginUtils = require("@babel/helper-plugin-utils");
var _default = exports.default = (0, _helperPluginUtils.declare)(api => {
api.assertVersion("^7.0.0-0 || ^8.0.0-0 || >8.0.0-alpha <8.0.0-beta");
const {
types: t,
template
} = api;
function build(left, right) {
return t.callExpression(t.memberExpression(t.identifier("Math"), t.identifier("pow")), [left, right]);
}
function maybeMemoize(node, scope) {
if (scope.isStatic(node)) {
return {
assign: node,
ref: t.cloneNode(node)
};
}
if (scope.path.isPattern()) {
return null;
}
const id = scope.generateUidIdentifierBasedOnNode(node);
scope.push({
id
});
return {
assign: t.assignmentExpression("=", t.cloneNode(id), node),
ref: t.cloneNode(id)
};
}
return {
name: "transform-exponentiation-operator",
visitor: {
AssignmentExpression(path) {
const {
node,
scope
} = path;
if (node.operator !== "**=") return;
if (t.isMemberExpression(node.left)) {
let member1;
let member2;
const object = maybeMemoize(node.left.object, scope);
if (!object) {
path.replaceWith(template.expression.ast`(() => ${path.node})()`);
return;
}
const {
property,
computed
} = node.left;
if (computed) {
const prop = maybeMemoize(property, scope);
member1 = t.memberExpression(object.assign, prop.assign, true);
member2 = t.memberExpression(object.ref, prop.ref, true);
} else {
member1 = t.memberExpression(object.assign, property, false);
member2 = t.memberExpression(object.ref, t.cloneNode(property), false);
}
path.replaceWith(t.assignmentExpression("=", member1, build(member2, node.right)));
} else {
path.replaceWith(t.assignmentExpression("=", node.left, build(t.cloneNode(node.left), node.right)));
}
},
BinaryExpression(path) {
const {
node
} = path;
if (node.operator === "**") {
path.replaceWith(build(node.left, node.right));
}
}
}
};
});
//# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
{"version":3,"names":["_helperPluginUtils","require","_default","exports","default","declare","api","assertVersion","types","t","template","build","left","right","callExpression","memberExpression","identifier","maybeMemoize","node","scope","isStatic","assign","ref","cloneNode","path","isPattern","id","generateUidIdentifierBasedOnNode","push","assignmentExpression","name","visitor","AssignmentExpression","operator","isMemberExpression","member1","member2","object","replaceWith","expression","ast","property","computed","prop","BinaryExpression"],"sources":["../src/index.ts"],"sourcesContent":["import { declare } from \"@babel/helper-plugin-utils\";\nimport type { types as t, Scope } from \"@babel/core\";\n\nexport default declare(api => {\n api.assertVersion(REQUIRED_VERSION(\"^7.0.0-0 || ^8.0.0-0\"));\n\n const { types: t, template } = api;\n\n function build(left: t.Expression, right: t.Expression) {\n return t.callExpression(\n t.memberExpression(t.identifier(\"Math\"), t.identifier(\"pow\")),\n [left, right],\n );\n }\n\n function maybeMemoize<T extends t.Expression | t.Super>(\n node: T,\n scope: Scope,\n ) {\n if (scope.isStatic(node)) {\n return { assign: node, ref: t.cloneNode(node) };\n }\n\n if (scope.path.isPattern()) {\n // We cannot inject a temp var in function arguments.\n return null;\n }\n\n const id = scope.generateUidIdentifierBasedOnNode(node);\n scope.push({ id });\n return {\n assign: t.assignmentExpression(\n \"=\",\n t.cloneNode(id),\n // This is not t.Super, because otherwise the .isStatic check above\n // would have returned true.\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion\n node as t.Expression,\n ),\n ref: t.cloneNode(id),\n };\n }\n\n return {\n name: \"transform-exponentiation-operator\",\n\n visitor: {\n AssignmentExpression(path) {\n const { node, scope } = path;\n if (node.operator !== \"**=\") return;\n\n if (t.isMemberExpression(node.left)) {\n let member1: t.Expression;\n let member2: t.Expression;\n\n const object = maybeMemoize(node.left.object, scope);\n if (!object) {\n // We need to inject a temp var, but we are in function parameters\n // and thus cannot. Wrap the expression in an IIFE. It will be\n // eventually requeued and transformed.\n path.replaceWith(template.expression.ast`(() => ${path.node})()`);\n return;\n }\n\n const { property, computed } = node.left;\n\n if (computed) {\n // @ts-ignore(Babel 7 vs Babel 8) Babel 8 has better type definitions\n const prop = maybeMemoize(property, scope);\n member1 = t.memberExpression(object.assign, prop.assign, true);\n member2 = t.memberExpression(object.ref, prop.ref, true);\n } else {\n member1 = t.memberExpression(object.assign, property, false);\n member2 = t.memberExpression(\n object.ref,\n t.cloneNode(property),\n false,\n );\n }\n\n path.replaceWith(\n t.assignmentExpression(\"=\", member1, build(member2, node.right)),\n );\n } else {\n path.replaceWith(\n t.assignmentExpression(\n \"=\",\n node.left,\n build(\n // todo: it could be a t.AsExpression\n t.cloneNode(node.left) as t.Identifier,\n node.right,\n ),\n ),\n );\n }\n },\n\n BinaryExpression(path) {\n const { node } = path;\n if (node.operator === \"**\") {\n path.replaceWith(\n build(\n // left can be PrivateName only if operator is `\"in\"`\n node.left as t.Expression,\n node.right,\n ),\n );\n }\n },\n },\n };\n});\n"],"mappings":";;;;;;AAAA,IAAAA,kBAAA,GAAAC,OAAA;AAAqD,IAAAC,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAGtC,IAAAC,0BAAO,EAACC,GAAG,IAAI;EAC5BA,GAAG,CAACC,aAAa,mDAAyC,CAAC;EAE3D,MAAM;IAAEC,KAAK,EAAEC,CAAC;IAAEC;EAAS,CAAC,GAAGJ,GAAG;EAElC,SAASK,KAAKA,CAACC,IAAkB,EAAEC,KAAmB,EAAE;IACtD,OAAOJ,CAAC,CAACK,cAAc,CACrBL,CAAC,CAACM,gBAAgB,CAACN,CAAC,CAACO,UAAU,CAAC,MAAM,CAAC,EAAEP,CAAC,CAACO,UAAU,CAAC,KAAK,CAAC,CAAC,EAC7D,CAACJ,IAAI,EAAEC,KAAK,CACd,CAAC;EACH;EAEA,SAASI,YAAYA,CACnBC,IAAO,EACPC,KAAY,EACZ;IACA,IAAIA,KAAK,CAACC,QAAQ,CAACF,IAAI,CAAC,EAAE;MACxB,OAAO;QAAEG,MAAM,EAAEH,IAAI;QAAEI,GAAG,EAAEb,CAAC,CAACc,SAAS,CAACL,IAAI;MAAE,CAAC;IACjD;IAEA,IAAIC,KAAK,CAACK,IAAI,CAACC,SAAS,CAAC,CAAC,EAAE;MAE1B,OAAO,IAAI;IACb;IAEA,MAAMC,EAAE,GAAGP,KAAK,CAACQ,gCAAgC,CAACT,IAAI,CAAC;IACvDC,KAAK,CAACS,IAAI,CAAC;MAAEF;IAAG,CAAC,CAAC;IAClB,OAAO;MACLL,MAAM,EAAEZ,CAAC,CAACoB,oBAAoB,CAC5B,GAAG,EACHpB,CAAC,CAACc,SAAS,CAACG,EAAE,CAAC,EAIfR,IACF,CAAC;MACDLine truncated