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
12 KiB

{"version":3,"names":["_helperPluginUtils","require","_helperCreateClassFeaturesPlugin","_helperAnnotateAsPure","_default","exports","default","declare","api","opt","assertVersion","types","t","template","loose","classWeakSets","WeakMap","fieldsWeakSets","unshadow","name","targetScope","scope","hasOwnBinding","rename","parent","injectToFieldInit","fieldPath","expr","before","node","value","get","insertBefore","insertAfter","set","unaryExpression","injectInitialization","classPath","init","firstFieldPath","constructorPath","el","isClassProperty","isClassPrivateProperty","static","isClassMethod","kind","injectConstructorInit","expressionStatement","getWeakSetId","weakSets","outerClass","reference","inject","id","generateUidIdentifier","expression","ast","cloneNode","newExpr","newExpression","identifier","annotateAsPure","manipulateOptions","_","parser","plugins","push","pre","enableFeature","file","FEATURES","privateIn","visitor","BinaryExpression","path","state","operator","isPrivateName","left","privateElement","findParent","isClass","find","isPrivate","key","parentPath","isPattern","replaceWith","type","buildCheckInRHS","right","_outerClass$node$id"],"sources":["../src/index.ts"],"sourcesContent":["import { declare } from \"@babel/helper-plugin-utils\";\nimport {\n enableFeature,\n FEATURES,\n injectInitialization as injectConstructorInit,\n buildCheckInRHS,\n} from \"@babel/helper-create-class-features-plugin\";\nimport annotateAsPure from \"@babel/helper-annotate-as-pure\";\nimport type { NodePath, Scope, types as t } from \"@babel/core\";\n\nexport interface Options {\n loose?: boolean;\n}\nexport default declare((api, opt: Options) => {\n api.assertVersion(REQUIRED_VERSION(7));\n const { types: t, template } = api;\n const { loose } = opt;\n\n // NOTE: When using the class fields or private methods plugins,\n // they will also take care of '#priv in obj' checks when visiting\n // the ClassExpression or ClassDeclaration nodes.\n // The visitor of this plugin is only effective when not compiling\n // private fields and methods.\n\n const classWeakSets: WeakMap<t.Class, t.Identifier> = new WeakMap();\n const fieldsWeakSets: WeakMap<\n t.ClassPrivateProperty | t.ClassPrivateMethod,\n t.Identifier\n > = new WeakMap();\n\n function unshadow(name: string, targetScope: Scope, scope: Scope) {\n while (scope !== targetScope) {\n if (scope.hasOwnBinding(name)) scope.rename(name);\n scope = scope.parent;\n }\n }\n\n function injectToFieldInit(\n fieldPath: NodePath<t.ClassPrivateProperty | t.ClassProperty>,\n expr: t.Expression,\n before = false,\n ) {\n if (fieldPath.node.value) {\n const value = fieldPath.get(\"value\");\n if (before) {\n value.insertBefore(expr);\n } else {\n value.insertAfter(expr);\n }\n } else {\n fieldPath.set(\"value\", t.unaryExpression(\"void\", expr));\n }\n }\n\n function injectInitialization(\n classPath: NodePath<t.Class>,\n init: t.Expression,\n ) {\n let firstFieldPath;\n let constructorPath;\n\n for (const el of classPath.get(\"body.body\")) {\n if (\n (el.isClassProperty() || el.isClassPrivateProperty()) &&\n !el.node.static\n ) {\n firstFieldPath = el;\n break;\n }\n if (!constructorPath && el.isClassMethod({ kind: \"constructor\" })) {\n constructorPath = el;\n }\n }\n\n if (firstFieldPath) {\n injectToFieldInit(firstFieldPath, init, true);\n } else {\n injectConstructorInit(classPath, constructorPath, [\n t.expressionStatement(init),\n ]);\n }\n }\n\n function getWeakSetId<Ref extends t.Node>(\n weakSets: WeakMap<t.Node, t.Identifier>,\n outerClass: NodePath<t.Class>,\n reference: NodePath<Ref>,\n name = \"\",\n inject: (\n reference: NodePath<Ref>,\n expression: t.Expression,\n before?: boolean,\n ) => void,\n ) {\n let id = weakSets.get(reference.node);\n\n if (!id) {\n id = outerClass.scope.generateUidIdentifier(`${name || \"\"} brandCheck`);\n weakSets.set(reference.node, id);\n\n inject(reference, template.expression.ast`${t.cloneNode(id)}.add(this)`);\n\n const newExpr = t.newExpression(t.identifier(\"WeakSet\"), []);\n annotateAsPure(newExpr);\n\n outerClass.insertBefore(template.ast`var ${id} = ${newExpr}`);\n }\n\n return t.cloneNode(id);\n }\n\n return {\n name: \"transform-private-property-in-object\",\n manipulateOptions: process.env.BABEL_8_BREAKING\n ? undefined\n : (_, parser) => parser.plugins.push(\"privateIn\"),\n pre() {\n // Enable this in @babel/helper-create-class-features-plugin, so that it\n // can be handled by the private fields and methods transform.\n enableFeature(this.file, FEATURES.privateIn, loose);\n },\n visitor: {\n BinaryExpression(path, state) {\n const { node } = path;\n const { file } = state;\n if (node.operator !== \"in\") return;\n if (!t.isPrivateName(node.left)) return;\n\n const { name } = node.left.id;\n\n let privateElement: NodePath<\n t.ClassPrivateMethod | t.ClassPrivateProperty\n >;\n const outerClass = path.findParent(path => {\n if (!path.isClass()) return false;\n\n privateElement = path.get(\"body.body\").find(\n ({ node }) =>\n // fixme: Support class accessor property\n t.isPrivate(node) && node.key.id.name === name,\n ) as NodePath<t.ClassPrivateMethod | t.ClassPrivateProperty>;\n\n return !!privateElement;\n }) as NodePath<t.Class>;\n\n if (outerClass.parentPath.scope.path.isPattern()) {\n outerClass.replaceWith(\n template.ast`(() => ${outerClass.node})()` as t.Statement,\n );\n // The injected class will be queued and eventually transformed when visited\n return;\n }\n\n if (privateElement.node.type === \"ClassPrivateMethod\") {\n if (privateElement.node.static) {\n if (outerClass.node.id) {\n unshadow(outerClass.node.id.name, outerClass.scope, path.scope);\n } else {\n outerClass.set(\"id\", path.scope.generateUidIdentifier(\"class\"));\n }\n path.replaceWith(\n template.expression.ast`\n ${t.cloneNode(outerClass.node.id)} === ${buildCheckInRHS(\n node.right,\n file,\n )}\n `,\n );\n } else {\n const id = getWeakSetId(\n classWeakSets,\n outerClass,\n outerClass,\n outerClass.node.id?.name,\n injectInitialization,\n );\n\n path.replaceWith(\n template.expression.ast`${id}.has(${buildCheckInRHS(\n node.right,\n file,\n )})`,\n );\n }\n } else {\n // Private fields might not all be initialized: see the 'halfConstructed'\n // example at https://v8.dev/features/private-brand-checks.\n\n const id = getWeakSetId<t.ClassPrivateProperty>(\n fieldsWeakSets,\n outerClass,\n privateElement as NodePath<t.ClassPrivateProperty>,\n privateElement.node.key.id.name,\n injectToFieldInit,\n );\n\n path.replaceWith(\n template.expression.ast`${id}.has(${buildCheckInRHS(\n node.right,\n file,\n )})`,\n );\n }\n },\n },\n };\n});\n"],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