1 line
8.8 KiB
Plaintext
1 line
8.8 KiB
Plaintext
{"version":3,"names":["findBareSupers","traverse","visitors","merge","Super","path","node","parentPath","isCallExpression","callee","push","environmentVisitor","referenceVisitor","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 } else {\n constructor.get(\"body\").unshiftContainer(\"body\", nodes);\n }\n}\n\nexport function extractComputedKeys(\n path: NodePath<t.Class>,\n computedPaths: NodePath<t.ClassProperty | t.ClassMethod>[],\n file: File,\n) {\n const declarations: t.ExpressionStatement[] = [];\n const state = {\n classBinding: path.node.id && path.scope.getBinding(path.node.id.name),\n file,\n };\n for (const computedPath of computedPaths) {\n const computedKey = computedPath.get(\"key\");\n if (computedKey.isReferencedIdentifier()) {\n handleClassTDZ(computedKey, state);\n } else {\n computedKey.traverse(classFieldDefinitionEvaluationTDZVisitor, state);\n }\n\n const computedNode = computedPath.node;\n // Make sure computed property names are only evaluated once (upon class definition)\n // and in the right order in combination with static properties\n if (!computedKey.isConstantExpression()) {\n const ident = path.scope.generateUidIdentifierBasedOnNode(\n computedNode.key,\n );\n // Declaring in the same block scope\n // Ref: https://github.com/babel/babel/pull/10029/files#diff-fbbdd83e7a9c998721c1484529c2ce92\n path.scope.push({\n id: ident,\n kind: \"let\",\n });\n declarations.push(\n t.expressionStatement(\n t.assignmentExpression(\"=\", t.cloneNode(ident), computedNode.key),\n ),\n );\n computedNode.key = t.cloneNode(ident);\n }\n }\n\n return declarations;\n}\n"],"mappings":";;;;;;;AAAA;AAGA;AAEA,MAAMA,cAAc,GAAGC,cAAQ,CAACC,QAAQ,CAACC,KAAK,CAA+B,CAC3E;EACEC,KAAK,CAACC,IAAI,EAAE;IACV,MAAM;MAAEC,IAAI;MAAEC;IAAW,CAAC,GAAGF,IAAI;IACjC,IAAIE,UAAU,CAACC,gBAAgB,CAAC;MAAEC,MAAM,EAAEH;IAAK,CAAC,CAAC,EAAE;MACjD,IAAI,CAACI,IAAI,CAACH,UAAU,CAAC;IACvB;EACF;AACF,CAAC,EACDI,iCAAkB,CACnB,CAAC;AAEF,MAAMC,gBAA2C,GAAG;EAClD,iCAAiC,CAC/BP,IAAqD,EACrD;IACAA,IAAI,CAACQ,IAAI,EAAE;EACb,CAAC;EAEDC,oBAAoB,CAACT,IAA4B,EAAE;IAAEU;EAAM,CAAC,EAAE;IAC5D,IAAIA,KAAK,CAACC,aAAa,CAACX,IAAI,CAACC,IAAI,CAACW,IAAI,CAAC,EAAE;MACvCF,KAAK,CAACG,MAAM,CAACb,IAAI,CAACC,IAAI,CAACW,IAAI,CAAC;MAC5BZ,IAAI,CAACQ,IAAI,EAAE;IACb;EACF;AACF,CAAC;AAOD,SAASM,cAAc,CACrBd,IAA4B,EAC5Be,KAA0B,EAC1B;EACA,IACEA,KAAK,CA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