-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathexports.ts
More file actions
152 lines (134 loc) · 4.58 KB
/
Copy pathexports.ts
File metadata and controls
152 lines (134 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { exportsData, kindIcon } from '../../domain/queries.js';
import { outputResult } from '../../infrastructure/result-formatter.js';
interface ExportConsumer {
name: string;
file: string;
line: number;
}
interface ExportSymbol {
kind: string;
name: string;
line: number;
role?: string;
signature?: { params?: string };
consumers: ExportConsumer[];
}
interface ReexportedSymbol extends ExportSymbol {
originFile: string;
}
interface ExportsDataResult {
file: string;
totalExported: number;
totalInternal: number;
totalUnused: number;
totalReexported?: number;
totalReexportedUnused?: number;
results: ExportSymbol[];
reexportedSymbols: ReexportedSymbol[];
reexports: { file: string }[];
}
interface ExportsOpts {
json?: boolean;
ndjson?: boolean;
csv?: boolean;
table?: boolean;
noTests?: boolean;
unused?: boolean;
[key: string]: unknown;
}
function printExportHeader(data: ExportsDataResult, opts: ExportsOpts): void {
if (opts.unused) {
console.log(
`\n# ${data.file} — ${data.totalUnused} unused export${data.totalUnused !== 1 ? 's' : ''} (of ${data.totalExported} exported)\n`,
);
} else {
const unusedNote = data.totalUnused > 0 ? ` (${data.totalUnused} unused)` : '';
console.log(
`\n# ${data.file} — ${data.totalExported} exported${unusedNote}, ${data.totalInternal} internal\n`,
);
}
}
function printExportSymbols(results: ExportSymbol[]): void {
for (const sym of results) {
const icon = kindIcon(sym.kind);
const sig = sym.signature?.params ? `(${sym.signature.params})` : '';
const role = sym.role ? ` [${sym.role}]` : '';
console.log(` ${icon} ${sym.name}${sig}${role} :${sym.line}`);
if (sym.consumers.length === 0) {
console.log(' (no consumers)');
} else {
for (const c of sym.consumers) {
console.log(` <- ${c.name} (${c.file}:${c.line})`);
}
}
}
}
function groupByOriginFile(reexportedSymbols: ReexportedSymbol[]): Map<string, ReexportedSymbol[]> {
const byOrigin = new Map<string, ReexportedSymbol[]>();
for (const sym of reexportedSymbols) {
if (!byOrigin.has(sym.originFile)) byOrigin.set(sym.originFile, []);
byOrigin.get(sym.originFile)!.push(sym);
}
return byOrigin;
}
function printReexportSymbol(sym: ExportSymbol, indent: string): void {
const icon = kindIcon(sym.kind);
const sig = sym.signature?.params ? `(${sym.signature.params})` : '';
const role = sym.role ? ` [${sym.role}]` : '';
console.log(`${indent}${icon} ${sym.name}${sig}${role} :${sym.line}`);
if (sym.consumers.length === 0) {
console.log(`${indent} (no consumers)`);
} else {
for (const c of sym.consumers) {
console.log(`${indent} <- ${c.name} (${c.file}:${c.line})`);
}
}
}
function printReexportedSymbols(reexportedSymbols: ReexportedSymbol[]): void {
const byOrigin = groupByOriginFile(reexportedSymbols);
for (const [originFile, syms] of byOrigin) {
console.log(`\n from ${originFile}:`);
for (const sym of syms) {
printReexportSymbol(sym, ' ');
}
}
}
function printReexportedSection(data: ExportsDataResult, opts: ExportsOpts): void {
const totalReexported = opts.unused
? (data.totalReexportedUnused ?? data.reexportedSymbols.length)
: (data.totalReexported ?? data.reexportedSymbols.length);
const plural = totalReexported !== 1 ? 's' : '';
if (data.results.length === 0) {
const label = opts.unused ? 'unused re-exported' : 're-exported';
console.log(
`\n# ${data.file} — barrel file (${totalReexported} ${label} symbol${plural} from sub-modules)\n`,
);
} else {
console.log(`\n Re-exported symbols (${totalReexported} from sub-modules):`);
}
printReexportedSymbols(data.reexportedSymbols);
}
export function fileExports(file: string, customDbPath: string, opts: ExportsOpts = {}): void {
const data = exportsData(file, customDbPath, opts) as ExportsDataResult;
if (outputResult(data, 'results', opts)) return;
const hasReexported = data.reexportedSymbols && data.reexportedSymbols.length > 0;
if (data.results.length === 0 && !hasReexported) {
if (opts.unused) {
console.log(`No unused exports found for "${file}".`);
} else {
console.log(`No exported symbols found for "${file}". Run "codegraph build" first.`);
}
return;
}
if (data.results.length > 0) {
printExportHeader(data, opts);
printExportSymbols(data.results);
}
if (hasReexported) {
printReexportedSection(data, opts);
}
if (data.reexports.length > 0) {
console.log(`\n Re-exported by: ${data.reexports.map((r) => r.file).join(', ')}`);
}
console.log();
}