mirror of https://github.com/jkjoy/sunpeiwen.git
1 line
9.0 KiB
Plaintext
1 line
9.0 KiB
Plaintext
|
{"version":3,"names":["_core","require","_helperEnvironmentVisitor","findBareSupers","traverse","visitors","merge","Super","path","node","parentPath","isCallExpression","callee","push","environmentVisitor","referenceVisitor","TSTypeAnnotation|TypeAnnotation","skip","ReferencedIdentifier","scope","hasOwnBinding","name","rename","handleClassTDZ","state","classBinding","getBinding","classNameTDZError","file","addHelper","throwNode","t","callExpression","stringLiteral","replaceWith","sequenceExpression","classFieldDefinitionEvaluationTDZVisitor","injectInitialization","constructor","nodes","renamer","length","isDerived","superClass","newConstructor","classMethod","identifier","blockStatement","params","restElement","body","template","statement","ast","get","unshiftContainer","bareSupers","isFirst","bareSuper","insertAfter","map","n","cloneNode","extractComputedKeys","computedPaths","declarations","id","computedPath","computedKey","isReferencedIdentifier","computedNode","isConstantExpression","ident","generateUidIdentifierBasedOnNode","key","kind","expressionStatement","assignmentExpression"],"sources":["../src/misc.ts"],"sourcesContent":["import { template, traverse, types as t } from \"@babel/core\";\nimport type { File } from \"@babel/core\";\nimport type { NodePath, Scope, Visitor, Binding } from \"@babel/traverse\";\nimport environmentVisitor from \"@babel/helper-environment-visitor\";\n\nconst findBareSupers = traverse.visitors.merge<NodePath<t.CallExpression>[]>([\n {\n Super(path) {\n const { node, parentPath } = path;\n if (parentPath.isCallExpression({ callee: node })) {\n this.push(parentPath);\n }\n },\n },\n environmentVisitor,\n]);\n\nconst referenceVisitor: Visitor<{ scope: Scope }> = {\n \"TSTypeAnnotation|TypeAnnotation\"(\n path: NodePath<t.TSTypeAnnotation | t.TypeAnnotation>,\n ) {\n path.skip();\n },\n\n ReferencedIdentifier(path: NodePath<t.Identifier>, { scope }) {\n if (scope.hasOwnBinding(path.node.name)) {\n scope.rename(path.node.name);\n path.skip();\n }\n },\n};\n\ntype HandleClassTDZState = {\n classBinding: Binding;\n file: File;\n};\n\nfunction handleClassTDZ(\n path: NodePath<t.Identifier>,\n state: HandleClassTDZState,\n) {\n if (\n state.classBinding &&\n state.classBinding === path.scope.getBinding(path.node.name)\n ) {\n const classNameTDZError = state.file.addHelper(\"classNameTDZError\");\n const throwNode = t.callExpression(classNameTDZError, [\n t.stringLiteral(path.node.name),\n ]);\n\n path.replaceWith(t.sequenceExpression([throwNode, path.node]));\n path.skip();\n }\n}\n\nconst classFieldDefinitionEvaluationTDZVisitor: Visitor<HandleClassTDZState> = {\n ReferencedIdentifier: handleClassTDZ,\n};\n\ninterface RenamerState {\n scope: Scope;\n}\n\nexport function injectInitialization(\n path: NodePath<t.Class>,\n constructor: NodePath<t.ClassMethod> | undefined,\n nodes: t.Statement[],\n renamer?: (visitor: Visitor<RenamerState>, state: RenamerState) => void,\n) {\n if (!nodes.length) return;\n\n const isDerived = !!path.node.superClass;\n\n if (!constructor) {\n const newConstructor = t.classMethod(\n \"constructor\",\n t.identifier(\"constructor\"),\n [],\n t.blockStatement([]),\n );\n\n if (isDerived) {\n newConstructor.params = [t.restElement(t.identifier(\"args\"))];\n newConstructor.body.body.push(template.statement.ast`super(...args)`);\n }\n\n [constructor] = path\n .get(\"body\")\n .unshiftContainer(\"body\", newConstructor) as NodePath<t.ClassMethod>[];\n }\n\n if (renamer) {\n renamer(referenceVisitor, { scope: constructor.scope });\n }\n\n if (isDerived) {\n const bareSupers: NodePath<t.CallExpression>[] = [];\n constructor.traverse(findBareSupers, bareSupers);\n let isFirst = true;\n for (const bareSuper of bareSupers) {\n if (isFirst) {\n bareSuper.insertAfter(nodes);\n isFirst = false;\n } else {\n bareSuper.insertAfter(nodes.map(n => t.cloneNode(n)));\n }\n }\n
|