node-ejs-renderer/node_modules/@babel/helper-create-class-features-plugin/lib/misc.js.map
2024-06-09 13:55:01 -04:00

1 line
12 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","lastReturnsThis","length","isDerived","superClass","newConstructor","classMethod","identifier","blockStatement","params","restElement","body","template","statement","ast","get","unshiftContainer","bareSupers","isFirst","bareSuper","map","n","cloneNode","isExpressionStatement","allNodes","toExpression","thisExpression","insertAfter","memoiseComputedKey","keyNode","hint","isUidReference","isIdentifier","hasUid","isMemoiseAssignment","isAssignmentExpression","operator","left","ident","id","kind","assignmentExpression","extractComputedKeys","computedPaths","declarations","computedPath","computedKey","isReferencedIdentifier","computedNode","isConstantExpression","assignment","generateUidBasedOnNode","expressionStatement","key"],"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.ExpressionStatement[],\n renamer?: (visitor: Visitor<RenamerState>, state: RenamerState) => void,\n lastReturnsThis?: boolean,\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 isFirst = false;\n } else {\n nodes = nodes.map(n => t.cloneNode(n));\n }\n if (!bareSuper.parentPath.isExpressionStatement()) {\n const allNodes: t.Expression[] = [\n bareSuper.node,\n ...nodes.map(n => t.toExpression(n)),\n ];\n if (!lastReturnsThis) allNodes.push(t.thisExpression());\n bareSuper.replaceWith(t.sequenceExpression(allNodes));\n } else {\n bareSuper.insertAfter(nodes);\n }\n }\n } else {\n constructor.get(\"body\").unshiftContainer(\"body\", nodes);\n }\n}\n\ntype ComputedKeyAssignmentExpression = t.AssignmentExpression & {\n left: t.Identifier;\n};\n\n/**\n * Try to memoise a computed key.\n * It returns undefined when the computed key is an uid reference, otherwise\n * an assignment expression `memoiserId = computed key`\n * @export\n * @param {t.Expression} keyNode Computed key\n * @param {Scope} scope The scope where memoiser id should be registered\n * @param {string} hint The memoiser id hint\n * @returns {(ComputedKeyAssignmentExpression | undefined)}\n */\nexport function memoiseComputedKey(\n keyNode: t.Expression,\n scope: Scope,\n hint: string,\n): ComputedKeyAssignmentExpression | undefined {\n const isUidReference = t.isIdentifier(keyNode) && scope.hasUid(keyNode.name);\n if (isUidReference) {\n return;\n }\n const isMemoiseAssignment =\n t.isAssignmentExpression(keyNode, { operator: \"=\" }) &&\n t.isIdentifier(keyNode.left) &&\n scope.hasUid(keyNode.left.name);\n if (isMemoiseAssignment) {\n return t.cloneNode(keyNode as ComputedKeyAssignmentExpression);\n } else {\n const ident = t.identifier(hint);\n // Declaring in the same block scope\n // Ref: https://github.com/babel/babel/pull/10029/files#diff-fbbdd83e7a9c998721c1484529c2ce92\n scope.push({\n id: ident,\n kind: \"let\",\n });\n return t.assignmentExpression(\n \"=\",\n t.cloneNode(ident),\n keyNode,\n ) as ComputedKeyAssignmentExpression;\n }\n}\n\nexport function extractComputedKeys(\n path: NodePath<t.Class>,\n computedPaths: NodePath<t.ClassProperty | t.ClassMethod>[],\n file: File,\n) {\n const { scope } = path;\n const declarations: t.ExpressionStatement[] = [];\n const state = {\n classBinding: path.node.id && 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 assignment = memoiseComputedKey(\n computedKey.node,\n scope,\n scope.generateUidBasedOnNode(computedKey.node),\n );\n if (assignment) {\n declarations.push(t.expressionStatement(assignment));\n computedNode.key = t.cloneNode(assignment.left);\n }\n }\n }\n\n return declarations;\n}\n"],"mappings":";;;;;;;;AAAA,IAAAA,KAAA,GAAAC,OAAA;AAGA,IAAAC,yBAAA,GAAAD,OAAA;AAEA,MAAME,cAAc,GAAGC,cAAQ,CAACC,QAAQ,CAACC,KAAK,CAA+B,CAC3E;EACEC,KAAKA,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,iCAAiCC,CAC/BR,IAAqD,EACrD;IACAA,IAAI,CAACS,IAAI,CAAC,CAAC;EACb,CAAC;EAEDC,oBAAoBA,CAACV,IAA4B,EAAE;IAAEW;EAAM,CAAC,EAAE;IAC5D,IAAIA,KAAK,CAACC,aAAa,CAACZ,IAAI,CAACC,IAAI,CAACY,IAAI,CAAC,EAAE;MACvCF,KAAK,CAACG,MAAM,CAACd,IAAI,CAACC,IAAI,CAACY,IAAI,CAAC;MAC5Bb,IAAI,CAACS,IAAI,CAAC,CAAC;IACb;EACF;AACF,CAAC;AAOD,SAASM,cAAcA,CACrBf,IAA4B,EAC5BgB,KAA0B,EAC1B;EACA,IACEA,KAAK,CAACC,YAAY,IAClBD,KAAK,CAACC,YAAY,KAAKjB,IAAI,CAACW,KAAK,CAACO,UAAU,CAAClB,IAAI,CAACC,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