Something went wrong. Try again.
tsref is a Bun CLI for finding references to exported TypeScript APIs.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261import 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, "/") || ".";}