Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const DEFAULT_EXPORT = "default"; function emptyShape() { return { classes: /* @__PURE__ */ new Map(), imports: /* @__PURE__ */ new Map(), namespaces: /* @__PURE__ */ new Map(), reexports: /* @__PURE__ */ new Map(), localExports: /* @__PURE__ */ new Map(), starExports: [] }; } function shapeFromAst(ast) { const shape = emptyShape(); for (const node of ast.body) { switch (node.type) { case "ClassDeclaration": addAstClass(shape, node); break; case "ImportDeclaration": addAstImports(shape, node); break; case "ExportNamedDeclaration": addAstNamedExport(shape, node); break; case "ExportDefaultDeclaration": { const decl = node.declaration; if (decl.type === "ClassDeclaration" || decl.type === "ClassExpression") { shape.defaultExportIsClass = true; shape.defaultExportSuper = superRefFromNode(decl.superClass); addAstClass(shape, decl); } else if (decl.type === "Identifier") { shape.defaultExportName = decl.name; } break; } case "ExportAllDeclaration": { const starNode = node; const source = starNode.exported ? void 0 : asString(starNode.source?.value); if (source) shape.starExports.push(source); break; } } } return shape; } function superRefFromNode(superClass) { if (!superClass) return void 0; if (superClass.type === "Identifier") { return { kind: "identifier", name: superClass.name }; } if (superClass.type === "MemberExpression") { const member = superClass; const object = member.object?.type === "Identifier" ? member.object.name : void 0; const property = member.property?.name; if (object && property) return { kind: "member", object, property }; } return void 0; } function addAstClass(shape, classNode) { if (classNode.id?.name) shape.classes.set(classNode.id.name, superRefFromNode(classNode.superClass)); } function addAstImports(shape, node) { const source = asString(node.source?.value); if (!source) return; for (const specifier of node.specifiers ?? []) { const local = specifier.local?.name; if (!local) continue; if (specifier.type === "ImportSpecifier" && specifier.imported?.name) { shape.imports.set(local, { source, imported: specifier.imported.name }); } else if (specifier.type === "ImportDefaultSpecifier") { shape.imports.set(local, { source, imported: DEFAULT_EXPORT }); } else if (specifier.type === "ImportNamespaceSpecifier") { shape.namespaces.set(local, source); } } } function addAstNamedExport(shape, node) { if (node.declaration?.type === "ClassDeclaration") { addAstClass(shape, node.declaration); return; } const source = asString(node.source?.value); for (const specifier of node.specifiers ?? []) { const exported = specifier.exported?.name; const local = specifier.local?.name; if (!exported || !local) continue; if (source) { shape.reexports.set(exported, { source, imported: local }); } else { shape.localExports.set(exported, local); } } } function shapeFromSource(rawCode) { const code = stripComments(rawCode); const shape = emptyShape(); const CLASS_RE = /\b(?:export\s+(?:default\s+)?)?(?:abstract\s+)?class\s+([A-Za-z_$][\w$]*)([^{]*)\{/g; for (const match of code.matchAll(CLASS_RE)) { const className = match[1]; const isDefault = /\bexport\s+default\b/.test(match[0]); const superRef = superRefFromHeader(match[2] ?? ""); shape.classes.set(className, superRef); if (isDefault) { shape.defaultExportIsClass = true; shape.defaultExportSuper = superRef; } } const defaultExprMatch = /\bexport\s+default\s+([A-Za-z_$][\w$]*)\s*;/.exec(code); if (defaultExprMatch && !shape.defaultExportIsClass) { shape.defaultExportName = defaultExprMatch[1]; } for (const match of code.matchAll(/\bexport\s*\*\s*from\s*['"]([^'"]+)['"]/g)) { if (match[1]) shape.starExports.push(match[1]); } for (const match of code.matchAll(/\bexport\s*\{([^}]*)\}\s*(?:from\s*['"]([^'"]+)['"])?/g)) { const source = match[2]; for (const { imported, local } of parseSpecifierList(match[1] ?? "")) { if (source) { shape.reexports.set(local, { source, imported }); } else { shape.localExports.set(local, imported); } } } for (const match of code.matchAll(/\bimport\s+([^'";]+?)\s+from\s*['"]([^'"]+)['"]/g)) { addSourceImports(shape, match[1] ?? "", match[2]); } return shape; } function superRefFromHeader(header) { const withoutParams = stripLeadingGenerics(header); const match = /\bextends\s+([A-Za-z_$][\w$]*)\s*(?:\.\s*([A-Za-z_$][\w$]*))?/.exec(withoutParams); if (!match) return void 0; return match[2] ? { kind: "member", object: match[1], property: match[2] } : { kind: "identifier", name: match[1] }; } function stripLeadingGenerics(header) { const start = header.indexOf("<"); if (start === -1 || header.slice(0, start).trim() !== "") return header; let depth = 0; for (let i = start; i < header.length; i++) { const char = header[i]; if (char === "<") depth++; else if (char === ">") { depth--; if (depth === 0) return header.slice(i + 1); } } return header; } function parseSpecifierList(clause) { const specifiers = []; for (const rawPart of clause.split(",")) { const part = rawPart.trim().replace(/^type\s+/, ""); if (!part) continue; const aliased = /^([A-Za-z_$][\w$]*)\s+as\s+([A-Za-z_$][\w$]*)$/.exec(part); if (aliased) { specifiers.push({ imported: aliased[1], local: aliased[2] }); } else if (/^[A-Za-z_$][\w$]*$/.test(part)) { specifiers.push({ imported: part, local: part }); } } return specifiers; } function addSourceImports(shape, clause, source) { if (/^type\s/.test(clause.trim())) return; const namespaceMatch = /\*\s*as\s+([A-Za-z_$][\w$]*)/.exec(clause); if (namespaceMatch) { shape.namespaces.set(namespaceMatch[1], source); } const bracesMatch = /\{([^}]*)\}/.exec(clause); if (bracesMatch) { for (const { imported, local } of parseSpecifierList(bracesMatch[1] ?? "")) { shape.imports.set(local, { source, imported }); } } const defaultMatch = /^\s*([A-Za-z_$][\w$]*)\s*(?:,|$)/.exec(clause); if (defaultMatch) { shape.imports.set(defaultMatch[1], { source, imported: DEFAULT_EXPORT }); } } function stripComments(code) { let result = ""; let index = 0; while (index < code.length) { const char = code[index]; const next = code[index + 1]; if (char === "/" && next === "/") { while (index < code.length && code[index] !== "\n") index++; continue; } if (char === "/" && next === "*") { index += 2; while (index < code.length && !(code[index] === "*" && code[index + 1] === "/")) index++; index += 2; result += " "; continue; } if (char === '"' || char === "'" || char === "`") { const quote = char; result += char; index++; while (index < code.length) { const inner = code[index]; result += inner; index++; if (inner === "\\") { if (index < code.length) { result += code[index]; index++; } continue; } if (inner === quote) break; } continue; } result += char; index++; } return result; } function asString(value) { return typeof value === "string" && value ? value : void 0; } exports.DEFAULT_EXPORT = DEFAULT_EXPORT; exports.shapeFromAst = shapeFromAst; exports.shapeFromSource = shapeFromSource; //# sourceMappingURL=moduleShape.js.map