{"version":3,"names":["_core","require","_globalTypes","transpileNamespace","path","allowNamespaces","node","declare","id","type","remove","get","buildCodeFrameError","name","value","handleNested","t","cloneNode","program","findParent","p","isProgram","registerGlobalType","scope","hasOwnBinding","replaceWith","registerDeclaration","replaceWithMultiple","getDeclaration","variableDeclaration","variableDeclarator","identifier","getMemberExpression","itemName","memberExpression","handleVariableDeclaration","hub","kind","file","declarations","every","declarator","isIdentifier","init","assignmentExpression","bindingIdentifiers","getBindingIdentifiers","assignments","idName","push","expressionStatement","sequenceExpression","buildNestedAmbientModuleError","buildError","Error","parentExport","names","Set","realName","assertIdentifier","generateUid","namespaceTopLevel","isTSModuleBlock","body","exportNamedDeclaration","isEmpty","i","length","subNode","transformed","moduleName","has","add","splice","isTypeScript","declaration","nodes","fallthroughValue","objectExpression","memberExpr","template","expression","ast","statement"],"sources":["../src/namespace.ts"],"sourcesContent":["import { template, types as t, type NodePath } from \"@babel/core\";\n\nimport { registerGlobalType } from \"./global-types.ts\";\n\nexport default function transpileNamespace(\n path: NodePath,\n allowNamespaces: boolean,\n) {\n if (path.node.declare || path.node.id.type === \"StringLiteral\") {\n path.remove();\n return;\n }\n\n if (!allowNamespaces) {\n throw path\n .get(\"id\")\n .buildCodeFrameError(\n \"Namespace not marked type-only declare.\" +\n \" Non-declarative namespaces are only supported experimentally in Babel.\" +\n \" To enable and review caveats see:\" +\n \" https://babeljs.io/docs/en/babel-plugin-transform-typescript\",\n );\n }\n\n const name = path.node.id.name;\n const value = handleNested(path, t.cloneNode(path.node, true));\n if (value === null) {\n // This means that `path` is a type-only namespace.\n // We call `registerGlobalType` here to allow it to be stripped.\n const program = path.findParent(p => p.isProgram());\n registerGlobalType(program.scope, name);\n\n path.remove();\n } else if (path.scope.hasOwnBinding(name)) {\n path.replaceWith(value);\n } else {\n path.scope.registerDeclaration(\n path.replaceWithMultiple([getDeclaration(name), value])[0],\n );\n }\n}\n\nfunction getDeclaration(name: string) {\n return t.variableDeclaration(\"let\", [\n t.variableDeclarator(t.identifier(name)),\n ]);\n}\n\nfunction getMemberExpression(name: string, itemName: string) {\n return t.memberExpression(t.identifier(name), t.identifier(itemName));\n}\n\n/**\n * Convert export const foo = 1 to Namespace.foo = 1;\n *\n * @param {t.VariableDeclaration} node given variable declaration, e.g. `const foo = 1`\n * @param {string} name the generated unique namespace member name\n * @param {*} hub An instance implements HubInterface defined in `@babel/traverse` that can throw a code frame error\n */\nfunction handleVariableDeclaration(\n node: t.VariableDeclaration,\n name: string,\n hub: any,\n): t.Statement[] {\n if (node.kind !== \"const\") {\n throw hub.file.buildCodeFrameError(\n node,\n \"Namespaces exporting non-const are not supported by Babel.\" +\n \" Change to const or see:\" +\n \" https://babeljs.io/docs/en/babel-plugin-transform-typescript\",\n );\n }\n const { declarations } = node;\n if (\n declarations.every(\n (declarator): declarator is t.VariableDeclarator & { id: t.Identifier } =>\n t.isIdentifier(declarator.id),\n )\n ) {\n // `export const a = 1` transforms to `const a = N.a = 1`, the output\n // is smaller than `const a = 1; N.a = a`;\n for (const declarator of declarations) {\n declarator.init = t.assignmentExpression(\n \"=\",\n getMemberExpression(name, declarator.id.name),\n declarator.init,\n );\n }\n return [node];\n }\n // Now we have pattern in declarators\n // `export const [a] = 1` transforms to `const [a] = 1; N.a = a`\n const bindingIdentifiers = t.getBindingIdentifiers(node);\n const assignments = [];\n // getBindingIdentifiers returns an object without prototype.\n // eslint-disable-next-line guard-for-in\n for (const idName in bindingIdentifiers) {\n assignments.push(\n t.assignmentExpression(\n \"=\",\n getMemberExpression(name, idName),\n t.cloneNode(bindingIdentifiers[idName]),\n ),\n );\n }\n return [node, t.expressionStatement(t.sequenceExpression(assignments))];\n}\n\nfunction buildNestedAmbientModuleError(path: NodePath, node: t.Node) {\n return path.hub.buildError(\n node,\n \"Ambient modules cannot be nested in other modules or namespaces.\",\n Error,\n );\n}\n\nfunction handleNested(\n path: NodePath,\n node: t.TSModuleDeclaration,\n parentExport?: t.Expression,\n): t.Statement | null {\n const names = new Set();\n const realName = node.id;\n t.assertIdentifier(realName);\n\n const name = path.scope.generateUid(realName.name);\n\n const namespaceTopLevel: t.Statement[] = t.isTSModuleBlock(node.body)\n ? node.body.body\n : // We handle `namespace X.Y {}` as if it was\n // namespace X {\n // export namespace Y {}\n // }\n [t.exportNamedDeclaration(node.body)];\n\n let isEmpty = true;\n\n for (let i = 0; i < namespaceTopLevel.length; i++) {\n const subNode = namespaceTopLevel[i];\n\n // The first switch is mainly to detect name usage. Only export\n // declarations require further transformation.\n switch (subNode.type) {\n case \"TSModuleDeclaration\": {\n if (!t.isIdentifier(subNode.id)) {\n throw buildNestedAmbientModuleError(path, subNode);\n }\n\n const transformed = handleNested(path, subNode);\n if (transformed !== null) {\n isEmpty = false;\n const moduleName = subNode.id.name;\n if (names.has(moduleName)) {\n namespaceTopLevel[i] = transformed;\n } else {\n names.add(moduleName);\n namespaceTopLevel.splice(\n i++,\n 1,\n getDeclaration(moduleName),\n transformed,\n );\n }\n }\n continue;\n }\n case \"TSEnumDeclaration\":\n case \"FunctionDeclaration\":\n case \"ClassDeclaration\":\n isEmpty = false;\n names.add(subNode.id.name);\n continue;\n case \"VariableDeclaration\": {\n isEmpty = false;\n // getBindingIdentifiers returns an object without prototype.\n // eslint-disable-next-line guard-for-in\n for (const name in t.getBindingIdentifiers(subNode)) {\n names.add(name);\n }\n continue;\n }\n default:\n isEmpty &&= t.isTypeScript(subNode);\n // Neither named declaration nor export, continue to next item.\n continue;\n case \"ExportNamedDeclaration\":\n // Export declarations get parsed using the next switch.\n }\n\n if (\"declare\" in subNode.declaration && subNode.declaration.declare) {\n continue;\n }\n\n // Transform the export declarations that occur inside of a namespace.\n switch (subNode.declaration.type) {\n case \"TSEnumDeclaration\":\n case \"FunctionDeclaration\":\n case \"ClassDeclaration\": {\n isEmpty = false;\n const itemName = subNode.declaration.id.name;\n names.add(itemName);\n namespaceTopLevel.splice(\n i++,\n 1,\n subNode.declaration,\n t.expressionStatement(\n t.assignmentExpression(\n \"=\",\n getMemberExpression(name, itemName),\n t.identifier(itemName),\n ),\n ),\n );\n break;\n }\n case \"VariableDeclaration\": {\n isEmpty = false;\n const nodes = handleVariableDeclaration(\n subNode.declaration,\n name,\n path.hub,\n );\n namespaceTopLevel.splice(i, nodes.length, ...nodes);\n i += nodes.length - 1;\n break;\n }\n case \"TSModuleDeclaration\": {\n if (!t.isIdentifier(subNode.declaration.id)) {\n throw buildNestedAmbientModuleError(path, subNode.declaration);\n }\n\n const transformed = handleNested(\n path,\n subNode.declaration,\n t.identifier(name),\n );\n if (transformed !== null) {\n isEmpty = false;\n const moduleName = subNode.declaration.id.name;\n if (names.has(moduleName)) {\n namespaceTopLevel[i] = transformed;\n } else {\n names.add(moduleName);\n namespaceTopLevel.splice(\n i++,\n 1,\n getDeclaration(moduleName),\n transformed,\n );\n }\n } else {\n namespaceTopLevel.splice(i, 1);\n i--;\n }\n }\n }\n }\n\n if (isEmpty) return null;\n\n // {}\n let fallthroughValue: t.Expression = t.objectExpression([]);\n\n if (parentExport) {\n const memberExpr = t.memberExpression(parentExport, realName);\n fallthroughValue = template.expression.ast`\n ${t.cloneNode(memberExpr)} ||\n (${t.cloneNode(memberExpr)} = ${fallthroughValue})\n `;\n }\n\n return template.statement.ast`\n (function (${t.identifier(name)}) {\n ${namespaceTopLevel}\n })(${realName} || (${t.cloneNode(realName)} = ${fallthroughValue}));\n `;\n}\n"],"mappings":";;;;;;AAAA,IAAAA,KAAA,GAAAC,OAAA;AAEA,IAAAC,YAAA,GAAAD,OAAA;AAEe,SAASE,kBAAkBA,CACxCC,IAAqC,EACrCC,eAAwB,EACxB;EACA,IAAID,IAAI,CAACE,IAAI,CAACC,OAAO,IAAIH,IAAI,CAACE,IAAI,CAACE,EAAE,CAACC,IAAI,KAAK,eAAe,EAAE;IAC9DL,IAAI,CAACM,MAAM,CAAC,CAAC;IACb;EACF;EAEA,IAAI,CAACL,eAAe,EAAE;IACpB,MAAMD,IAAI,CACPO,GAAG,CAAC,IAAI,CAAC,CACTC,mBAAmB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