Initial commit

This commit is contained in:
schihei committed 2025-03-07 19:22:02 +01:00
commit 4a98255d83
55743 files changed
+5280367

No files matched your search

@@ -0,0 +1,175 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var core = require('@babel/core');
var helperPluginUtils = require('@babel/helper-plugin-utils');
function isNameOrLength(key) {
if (core.types.isIdentifier(key)) {
return key.name === "name" || key.name === "length";
}
if (core.types.isStringLiteral(key)) {
return key.value === "name" || key.value === "length";
}
return false;
}
function isStaticFieldWithValue(node) {
return (core.types.isClassProperty(node) || core.types.isClassPrivateProperty(node)) && node.static && !!node.value;
}
const hasReferenceVisitor = {
ReferencedIdentifier(path, state) {
if (path.node.name === state.name) {
state.ref();
path.stop();
}
},
Scope(path, {
name
}) {
if (path.scope.hasOwnBinding(name)) {
path.skip();
}
}
};
function isReferenceOrThis(node, name) {
return core.types.isThisExpression(node) || name && core.types.isIdentifier(node, {
name
});
}
const hasReferenceOrThisVisitor = {
"ThisExpression|ReferencedIdentifier"(path, state) {
if (isReferenceOrThis(path.node, state.name)) {
state.ref();
path.stop();
}
},
FunctionParent(path, state) {
if (path.isArrowFunctionExpression()) return;
if (state.name && !path.scope.hasOwnBinding(state.name)) {
path.traverse(hasReferenceVisitor, state);
}
path.skip();
if (path.isMethod()) {
if (path.requeueComputedKeyAndDecorators) {
path.requeueComputedKeyAndDecorators();
} else {
require("@babel/traverse").NodePath.prototype.requeueComputedKeyAndDecorators.call(path);
}
}
}
};
function getPotentiallyBuggyFieldsIndexes(path) {
var _path$node$id;
const buggyPublicStaticFieldsIndexes = [];
let classReferenced = false;
const className = (_path$node$id = path.node.id) == null ? void 0 : _path$node$id.name;
const hasReferenceState = {
name: className,
ref: () => classReferenced = true
};
if (className) {
for (const el of path.get("body.body")) {
if (el.node.computed) {
el.get("key").traverse(hasReferenceVisitor, hasReferenceState);
if (classReferenced) break;
}
}
}
let nextPotentiallyBuggy = false;
const {
body
} = path.node.body;
for (let i = 0; i < body.length; i++) {
const node = body[i];
if (!nextPotentiallyBuggy) {
if (core.types.isStaticBlock(node)) {
classReferenced = true;
nextPotentiallyBuggy = true;
} else if (isStaticFieldWithValue(node)) {
if (!classReferenced) {
if (isReferenceOrThis(node.value, className)) {
classReferenced = true;
} else {
path.get(`body.body.${i}.value`).traverse(hasReferenceOrThisVisitor, hasReferenceState);
}
}
if (classReferenced) {
nextPotentiallyBuggy = !path.scope.isPure(node.value);
}
}
}
if (core.types.isClassProperty(node, {
static: true
}) && (nextPotentiallyBuggy || node.computed || isNameOrLength(node.key))) {
buggyPublicStaticFieldsIndexes.push(i);
}
}
return buggyPublicStaticFieldsIndexes;
}
function getNameOrLengthStaticFieldsIndexes(path) {
const indexes = [];
const {
body
} = path.node.body;
for (let i = 0; i < body.length; i++) {
const node = body[i];
if (core.types.isClassProperty(node, {
static: true,
computed: false
}) && isNameOrLength(node.key)) {
indexes.push(i);
}
}
return indexes;
}
function toRanges(nums) {
const ranges = [];
if (nums.length === 0) return ranges;
let start = nums[0];
let end = start + 1;
for (let i = 1; i < nums.length; i++) {
if (nums[i] <= nums[i - 1]) {
throw new Error("Internal Babel error: nums must be in ascending order");
}
if (nums[i] === end) {
end++;
} else {
ranges.push([start, end]);
start = nums[i];
end = start + 1;
}
}
ranges.push([start, end]);
return ranges;
}
function buildFieldsReplacement(fields, scope, file) {
return core.types.staticBlock(fields.map(field => {
const key = field.computed || !core.types.isIdentifier(field.key) ? field.key : core.types.stringLiteral(field.key.name);
return core.types.expressionStatement(core.types.callExpression(file.addHelper("defineProperty"), [core.types.thisExpression(), key, field.value || scope.buildUndefinedNode()]));
}));
}
var index = helperPluginUtils.declare(api => {
api.assertVersion("^7.0.0-0 || >8.0.0-alpha <8.0.0-beta");
const setPublicClassFields = api.assumption("setPublicClassFields");
return {
name: "bugfix-v8-static-class-fields-redefine-readonly",
visitor: {
Class(path) {
const ranges = toRanges(setPublicClassFields ? getNameOrLengthStaticFieldsIndexes(path) : getPotentiallyBuggyFieldsIndexes(path));
for (let i = ranges.length - 1; i >= 0; i--) {
const [start, end] = ranges[i];
const startPath = path.get("body.body")[start];
startPath.replaceWith(buildFieldsReplacement(path.node.body.body.slice(start, end), path.scope, this.file));
for (let j = end - 1; j > start; j--) {
path.get("body.body")[j].remove();
}
}
}
}
};
});
exports.default = index;
//# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sources":["../src/util.ts","../src/index.ts"],"sourcesContent":["import { types as t, type NodePath, type Visitor } from \"@babel/core\";\n\nfunction isNameOrLength(key: t.Node): boolean {\n if (t.isIdentifier(key)) {\n return key.name === \"name\" || key.name === \"length\";\n }\n if (t.isStringLiteral(key)) {\n return key.value === \"name\" || key.value === \"length\";\n }\n return false;\n}\n\nfunction isStaticFieldWithValue(\n node: t.Node,\n): node is t.ClassProperty | t.ClassPrivateProperty {\n return (\n (t.isClassProperty(node) || t.isClassPrivateProperty(node)) &&\n node.static &&\n !!node.value\n );\n}\n\nconst hasReferenceVisitor: Visitor<{ name: string; ref: () => void }> = {\n ReferencedIdentifier(path, state) {\n if (path.node.name === state.name) {\n state.ref();\n path.stop();\n }\n },\n Scope(path, { name }) {\n if (path.scope.hasOwnBinding(name)) {\n path.skip();\n }\n },\n};\n\nfunction isReferenceOrThis(node: t.Node, name?: string) {\n return t.isThisExpression(node) || (name && t.isIdentifier(node, { name }));\n}\n\nconst hasReferenceOrThisVisitor: Visitor<{ name?: string; ref: () => void }> = {\n \"ThisExpression|ReferencedIdentifier\"(path, state) {\n if (isReferenceOrThis(path.node, state.name)) {\n state.ref();\n path.stop();\n }\n },\n FunctionParent(path, state) {\n if (path.isArrowFunctionExpression()) return;\n if (state.name && !path.scope.hasOwnBinding(state.name)) {\n path.traverse(hasReferenceVisitor, state);\n }\n path.skip();\n if (path.isMethod()) {\n if (\n process.env.BABEL_8_BREAKING ||\n USE_ESM ||\n IS_STANDALONE ||\n path.requeueComputedKeyAndDecorators\n ) {\n path.requeueComputedKeyAndDecorators();\n } else {\n // eslint-disable-next-line no-restricted-globals\n require(\"@babel/traverse\").NodePath.prototype.requeueComputedKeyAndDecorators.call(\n path,\n );\n }\n }\n },\n};\n\ntype ClassElementWithComputedKeySupport = Extract<\n t.ClassBody[\"body\"][number],\n { computed?: boolean }\n>;\n\n/**\n * This function returns an array containing the indexes of class elements\n * that might be affected by https://crbug.com/v8/12421 bug.\n *\n * This bug affects public static class fields that have the same name as an\n * existing non-writable property with the same name. This usually happens when\n * the static field is named 'length' or 'name', since it clashes with the\n * predefined fn.length and fn.name properties. We must also compile static\n * fields with computed key, because they might end up being named 'length' or\n * 'name'.\n *\n * However, this bug can potentially affect public static fields with any name.\n * Consider this example:\n *\n * class A {\n * static {\n * Object.defineProperty(A, \"readonly\", {\n * value: 1,\n * writable: false,\n * configurable: true\n * })\n * }\n *\n * static readonly = 2;\n * }\n *\n * When initializing the 'static readonly' field, the class already has a\n * non-writable property named 'readonly' and thus V8 9.7 incorrectly throws.\n *\n * To avoid unconditionally compiling every public static field, we track how\n * the class is referenced during definition & static evaluation: any side\n * effect after a reference to the class can potentially define a non-writable\n * conficting property, so subsequent public static fields must be compiled.\n * The class could be referenced using the class name in computed keys, which\n * run before static fields, or using either the class name or 'this' in static\n * fields (both public and private) and static blocks.\n *\n * We don't need to check if computed keys referencing the class have any side\n * effect, because during the computed keys evaluation the internal class\n * binding is in TDZ. However, the first side effect in a static field/block\n * could have access to a function defined in a computed key that modifies the\n * class.\n *\n * This logic is already quite complex, so we assume that static blocks always\n * have side effects and reference the class (the reason to use them is to\n * perform additional initialization logic on the class anyway), so that we do\n * not have to check their contents.\n */\nexport function getPotentiallyBuggyFieldsIndexes(path: NodePath<t.Class>) {\n const buggyPublicStaticFieldsIndexes: number[] = [];\n\n let classReferenced = false;\n const className = path.node.id?.name;\n\n const hasReferenceState = {\n name: className,\n ref: () => (classReferenced = true),\n };\n\n if (className) {\n for (const el of path.get(\"body.body\")) {\n if ((el.node as ClassElementWithComputedKeySupport).computed) {\n // Since .traverse skips the top-level node, it doesn't detect\n // a reference happening immediately:\n // class Line truncated