-
Notifications
You must be signed in to change notification settings - Fork 2
/
unused-exports.ts
215 lines (173 loc) · 5.25 KB
/
unused-exports.ts
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { existsSync, readdirSync, readFileSync, statSync } from "fs";
import { dirname, join, resolve } from "path";
const log = (message: string): void => {
// tslint:disable-next-line: no-console
console.log(message);
};
const logEmptyLine = (): void => {
log("");
};
const findAllTypeScriptFiles = (directory: string): readonly string[] => {
const files = readdirSync(directory);
return files.reduce<readonly string[]>((allFiles, file) => {
const path = resolve(join(directory, file));
const stat = statSync(path);
if (stat.isFile()) {
if (!/\.tsx?$/.test(path)) {
return allFiles;
}
return [...allFiles, path];
}
if (stat.isDirectory()) {
return [...allFiles, ...findAllTypeScriptFiles(path)];
}
return allFiles;
}, []);
};
const matchAll = (text: string, pattern: RegExp): readonly string[][] => {
const matches: string[][] = [];
while (true) {
const match = pattern.exec(text);
if (match == null) {
break;
}
matches.push(match);
}
return matches;
};
const resolveModuleNameToPath = (name: string): string => {
const possibleNames = [
name,
`${name}.ts`,
`${name}.tsx`,
join(name, "index.ts"),
join(name, "index.tsx"),
];
for (const possibleName of possibleNames) {
if (existsSync(possibleName)) {
const stat = statSync(possibleName);
if (stat.isFile()) {
return possibleName;
}
}
}
throw new Error(`unable to resolve ${name}`);
};
interface IImport {
readonly path: string;
readonly symbols: readonly string[];
}
interface IFile {
readonly path: string;
readonly imports: readonly IImport[];
readonly exports: readonly string[];
}
type IProject = Map<string, IFile>;
const buildProject = (directory: string): IProject => {
const paths = findAllTypeScriptFiles(directory);
const project = new Map<string, IFile>();
for (const path of paths) {
const parentDirectory = dirname(path);
const text = readFileSync(path).toString();
const allImports = [
...matchAll(text, /import {([^}]+)} from "([^"]+)";/gm),
...matchAll(text, /import type {([^}]+)} from "([^"]+)";/gm),
...matchAll(text, /const {([^}]+)} = await import\("([^"]+)"\);/gm),
];
const importedSymbols = allImports
.map((match) => ({
module: match[2],
symbols: match[1].split(",").map((symbol) => symbol.trim()),
}))
.filter(({ module }) => module.startsWith("."))
.map<IImport>(({ module, symbols }) => ({
path: resolveModuleNameToPath(resolve(parentDirectory, module)),
symbols,
}));
const exportedSymbols = matchAll(
text,
/export (enum|const enum|const|class|function|type|interface) ([A-Za-z0-9]+)/gm,
).map((match) => match[2]);
project.set(path, {
exports: exportedSymbols,
imports: importedSymbols,
path,
});
}
return project;
};
interface IExport {
path: string;
exports: readonly string[];
}
const findUnusedExports = (directory: string): readonly IExport[] => {
const project = buildProject(directory);
for (const file of project.values()) {
for (const { path, symbols } of file.imports) {
const importedFile = project.get(path);
if (importedFile == null) {
// we imported a file not known in this project, e.g., a backend file in
// the frontend project
continue;
}
project.set(path, {
...importedFile,
exports: importedFile.exports.filter(
(exportedSymbol) => !symbols.includes(exportedSymbol),
),
});
}
}
return [...project.values()]
.filter(({ exports }) => exports.length > 0)
.map<IExport>(({ path, exports }) => ({ path, exports }));
};
const logUnusedExportsCount = (exports: readonly IExport[]): void => {
const fileCount = exports.length;
const totalCount = exports.reduce(
(sum, file) => sum + file.exports.length,
0,
);
log(
`found ${totalCount} potentially unused export(s) across ${fileCount} file(s)`,
);
};
const logUnusedExports = (exports: readonly IExport[]): void => {
for (const file of exports) {
log(file.path);
for (const symbol of file.exports) {
log(`- ${symbol}`);
}
logEmptyLine();
}
};
const unusedFrontendExports = findUnusedExports("./frontend/src");
const unusedBackendExports = findUnusedExports("./backend/src")
.map(({ path, exports }) => ({
exports: exports.filter((symbol) => {
const isService = symbol.endsWith("Service");
const isController = symbol.endsWith("Controller");
const isInterceptor = symbol.endsWith("Interceptor");
const isMiddleware = symbol.endsWith("Middleware");
const isError = symbol.endsWith("Error");
return (
!isService &&
!isController &&
!isInterceptor &&
!isMiddleware &&
!isError
);
}),
path,
}))
.filter(({ exports, path }) => {
const isEmpty = exports.length === 0;
const isExportedAPI =
path.endsWith("controllers/dto.ts") ||
path.endsWith("controllers/api.ts");
return !isEmpty && !isExportedAPI;
});
logUnusedExportsCount([...unusedFrontendExports, ...unusedBackendExports]);
logEmptyLine();
logUnusedExports(unusedFrontendExports);
logUnusedExports(unusedBackendExports);