import path from "node:path"; import type { AnalysisReport, ExportedMemberSummary, ExportedSymbolSummary, ReferenceKind, ReferenceRecord, SourceLocation, } from "./types"; export interface RenderOptions { summaryOnly?: boolean; unusedOnly?: boolean; noImports?: boolean; } const referenceKinds: ReferenceKind[] = [ "import", "re-export", "construct", "call", "write", "read", "type", "jsx", "unknown", ]; export function renderReport( report: AnalysisReport, options: RenderOptions = {}, cwd = process.cwd(), ): string { const symbols = filterSymbols(report.exportedSymbols, options); const lines: string[] = []; renderHeader(lines, { targetFile: report.targetFile, projectFile: report.projectFile, exportCount: symbols.length, externalCount: countReferences(symbols, false), internalCount: countReferences(symbols, true), diagnostics: report.diagnostics, }, cwd); lines.push(""); renderSummaryTable(lines, symbols); if (!options.summaryOnly) { for (const exportedSymbol of symbols) { lines.push(""); renderExport(lines, exportedSymbol, cwd, options); } } return `${lines.join("\n")}\n`; } export function filterSymbols( symbols: ExportedSymbolSummary[], options: RenderOptions = {}, ): ExportedSymbolSummary[] { if (!options.unusedOnly) { return symbols; } return symbols.filter((symbol) => countSymbolReferences(symbol, false) === 0); } function renderHeader( lines: string[], input: { targetFile: string; projectFile: string; exportCount: number; externalCount?: number; internalCount?: number; diagnostics: string[]; }, cwd: string, ): void { const summaryParts = [`${input.exportCount} exports`]; if (input.externalCount !== undefined) { summaryParts.push(`${input.externalCount} external refs`); } if (input.internalCount !== undefined) { summaryParts.push(`${input.internalCount} internal refs`); } lines.push(`tsref ${relativePath(input.targetFile, cwd)}`); lines.push(`project ${relativePath(input.projectFile, cwd)}`); lines.push(""); lines.push(summaryParts.join(" | ")); if (input.diagnostics.length > 0) { lines.push(`TypeScript diagnostics: ${input.diagnostics.length}`); } } function renderExport( lines: string[], exportedSymbol: ExportedSymbolSummary, cwd: string, options: RenderOptions, ): void { const label = exportedSymbol.localName && exportedSymbol.localName !== exportedSymbol.exportName ? `${exportedSymbol.exportName} ${exportedSymbol.kind} (${exportedSymbol.localName})` : `${exportedSymbol.exportName} ${exportedSymbol.kind}`; lines.push(label); if (exportedSymbol.declarationLocation) { lines.push(`declared ${formatLocation(exportedSymbol.declarationLocation, cwd)}`); } if (!options.summaryOnly) { renderFlatReferences(lines, exportedSymbol.references, cwd, false); renderFlatReferences(lines, exportedSymbol.references, cwd, true); } if (exportedSymbol.members.length === 0) { return; } lines.push(""); lines.push("members"); for (const member of exportedSymbol.members) { renderMember(lines, member, cwd, options); } } function renderMember( lines: string[], member: ExportedMemberSummary, cwd: string, options: RenderOptions, ): void { lines.push(` ${member.name} ${member.kind}`); if (!options.summaryOnly) { renderFlatReferences(lines, member.references, cwd, false, " "); renderFlatReferences(lines, member.references, cwd, true, " "); } } function renderFlatReferences( lines: string[], references: ReferenceRecord[], cwd: string, internal: boolean, indent = "", ): void { const filteredReferences = references.filter( (reference) => reference.isInternal === internal, ); lines.push(`${indent}${internal ? "internal" : "external"} (${filteredReferences.length})`); for (const kind of referenceKinds) { const group = filteredReferences.filter((reference) => reference.kind === kind); for (const reference of group) { lines.push(`${indent} ${kind.padEnd(9)} ${formatLocation(reference.location, cwd)}`); } } } function renderSummaryTable(lines: string[], symbols: ExportedSymbolSummary[]): void { if (symbols.length === 0) { lines.push("No exports to show."); return; } const rows = symbols.map((symbol) => ({ exportName: symbol.exportName, kind: symbol.kind, external: String(countSymbolReferences(symbol, false)), internal: String(countSymbolReferences(symbol, true)), status: countSymbolReferences(symbol, false) === 0 ? "unused" : "used", })); const widths = { exportName: Math.max("export".length, ...rows.map((row) => row.exportName.length)), kind: Math.max("kind".length, ...rows.map((row) => row.kind.length)), external: Math.max("external".length, ...rows.map((row) => row.external.length)), internal: Math.max("internal".length, ...rows.map((row) => row.internal.length)), status: Math.max("status".length, ...rows.map((row) => row.status.length)), }; lines.push( [ "export".padEnd(widths.exportName), "kind".padEnd(widths.kind), "external".padStart(widths.external), "internal".padStart(widths.internal), "status", ].join(" "), ); for (const row of rows) { lines.push( [ row.exportName.padEnd(widths.exportName), row.kind.padEnd(widths.kind), row.external.padStart(widths.external), row.internal.padStart(widths.internal), row.status, ].join(" "), ); } } function countReferences(symbols: ExportedSymbolSummary[], internal: boolean): number { return symbols.reduce((total, symbol) => total + countSymbolReferences(symbol, internal), 0); } function countSymbolReferences(symbol: ExportedSymbolSummary, internal: boolean): number { if (!internal && symbol.externalReferenceCount !== undefined) { return ( symbol.externalReferenceCount + symbol.members.reduce( (total, member) => total + (member.externalReferenceCount ?? 0), 0, ) ); } if (internal && symbol.internalReferenceCount !== undefined) { return ( symbol.internalReferenceCount + symbol.members.reduce( (total, member) => total + (member.internalReferenceCount ?? 0), 0, ) ); } const symbolCount = symbol.references.filter( (reference) => reference.isInternal === internal, ).length; const memberCount = symbol.members.reduce( (memberTotal, member) => memberTotal + member.references.filter((reference) => reference.isInternal === internal).length, 0, ); return symbolCount + memberCount; } function formatLocation(location: SourceLocation, cwd: string): string { return `${relativePath(location.filePath, cwd)}:${location.line}:${location.column}`; } function relativePath(filePath: string, cwd: string): string { return path.relative(cwd, filePath).replaceAll(path.sep, "/") || "."; }