{"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","inherits","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 * as t from \"@babel/types\";\nimport type { NodePath, Scope } from \"@babel/traverse\";\n\nexport interface Options {\n loose?: boolean;\n}\nexport default declare((api, opt: Options) => {\n api.assertVersion(\n process.env.BABEL_8_BREAKING && process.env.IS_PUBLISH\n ? PACKAGE_JSON.version\n : 7,\n );\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 = 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,\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,\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(\n weakSets: WeakMap,\n outerClass: NodePath,\n reference: NodePath,\n name = \"\",\n inject: (\n reference: NodePath,\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 inherits: USE_ESM\n ? undefined\n : IS_STANDALONE\n ? undefined\n : // eslint-disable-next-line no-restricted-globals\n require(\"@babel/plugin-syntax-private-property-in-object\").default,\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;\n\n return !!privateElement;\n }) as NodePath;\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(\n fieldsWeakSets,\n outerClass,\n privateElement as NodePath,\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"],"mappings":";;;;;;AAAA,IAAAA,kBAAA,GAAAC,OAAA;AACA,IAAAC,gCAAA,GAAAD,OAAA;AAMA,IAAAE,qBAAA,GAAAF,OAAA;AAA4D,IAAAG,QAAA,GAAAC,OAAA,CAAA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