You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
17 KiB

3 weeks ago
{"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 th