Skip to content

Commit 3cb27eb

Browse files
committed
Refactor getImportsAndExports return structure
1 parent 39b4c29 commit 3cb27eb

File tree

5 files changed

+26
-49
lines changed

5 files changed

+26
-49
lines changed

packages/knip/src/ProjectPrincipal.ts

+2-14
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,7 @@ export class ProjectPrincipal {
246246

247247
const resolve = (specifier: string) => this.backend.resolveModuleNames([specifier], sourceFile.fileName)[0];
248248

249-
const { imports, exports, scripts, traceRefs } = _getImportsAndExports(sourceFile, resolve, typeChecker, {
250-
...options,
251-
skipExports,
252-
});
249+
const { imports, ...rest } = _getImportsAndExports(sourceFile, resolve, typeChecker, { ...options, skipExports });
253250

254251
const { internal, resolved, specifiers, unresolved, external } = imports;
255252

@@ -293,16 +290,7 @@ export class ProjectPrincipal {
293290
}
294291
}
295292

296-
return {
297-
imports: {
298-
internal,
299-
unresolved: unresolvedImports,
300-
external,
301-
},
302-
exports,
303-
scripts,
304-
traceRefs,
305-
};
293+
return { imports: { internal, unresolved: unresolvedImports, external }, ...rest };
306294
}
307295

308296
invalidateFile(filePath: string) {

packages/knip/src/index.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => {
307307

308308
const workspace = chief.findWorkspaceByFilePath(filePath);
309309
if (workspace) {
310-
const { imports, exports, scripts, traceRefs } = principal.analyzeSourceFile(
310+
const { imports, exports, duplicates, scripts, traceRefs } = principal.analyzeSourceFile(
311311
filePath,
312312
{
313313
skipTypeOnly: isStrict,
@@ -326,6 +326,7 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => {
326326

327327
file.imports = imports;
328328
file.exports = exports;
329+
file.duplicates = duplicates;
329330
file.scripts = scripts;
330331
file.traceRefs = traceRefs;
331332

@@ -402,7 +403,7 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => {
402403
streamer.cast('Connecting the dots...');
403404

404405
for (const [filePath, file] of graph.entries()) {
405-
const exportItems = file.exports?.exported;
406+
const exportItems = file.exports;
406407

407408
if (!exportItems || exportItems.size === 0) continue;
408409

@@ -454,7 +455,7 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => {
454455
continue;
455456
}
456457
// Skip exports if re-exported from entry file and tagged
457-
const reExportedItem = graph.get(reExportingEntryFile)?.exports.exported.get(identifier);
458+
const reExportedItem = graph.get(reExportingEntryFile)?.exports.get(identifier);
458459
if (reExportedItem && shouldIgnore(reExportedItem.jsDocTags)) continue;
459460
}
460461

@@ -571,8 +572,8 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => {
571572
const ws = chief.findWorkspaceByFilePath(filePath);
572573

573574
if (ws) {
574-
if (file.exports?.duplicate) {
575-
for (const symbols of file.exports.duplicate) {
575+
if (file.duplicates) {
576+
for (const symbols of file.duplicates) {
576577
if (symbols.length > 1) {
577578
const symbol = symbols.map(s => s.symbol).join('|');
578579
collector.addIssue({ type: 'duplicates', filePath, workspace: ws.name, symbol, symbols });

packages/knip/src/types/dependency-graph.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,8 @@ export type FileNode = {
6262
external: Set<string>;
6363
unresolved: Set<UnresolvedImport>;
6464
};
65-
exports: {
66-
exported: ExportMap;
67-
duplicate: Iterable<Array<IssueSymbol>>;
68-
};
65+
exports: ExportMap;
66+
duplicates: Iterable<Array<IssueSymbol>>;
6967
scripts: Set<string>;
7068
imported?: ImportDetails;
7169
internalImportCache?: ImportMap;

packages/knip/src/typescript/get-imports-and-exports.ts

+14-22
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ const getImportsAndExports = (
6969
options: GetImportsAndExportsOptions
7070
) => {
7171
const { skipTypeOnly, tags, ignoreExportsUsedInFile } = options;
72-
const internalImports: ImportMap = new Map();
73-
const externalImports = new Set<string>();
74-
const unresolvedImports = new Set<UnresolvedImport>();
72+
const internal: ImportMap = new Map();
73+
const external = new Set<string>();
74+
const unresolved = new Set<UnresolvedImport>();
7575
const resolved = new Set<string>();
7676
const specifiers = new Set<[string, string]>();
7777
const exports: ExportMap = new Map();
@@ -122,11 +122,11 @@ const getImportsAndExports = (
122122

123123
specifiers.add([specifier, filePath]);
124124

125-
const file = internalImports.get(filePath);
125+
const file = internal.get(filePath);
126126

127127
const imports = file ?? createImports();
128128

129-
if (!file) internalImports.set(filePath, imports);
129+
if (!file) internal.set(filePath, imports);
130130

131131
const nsOrAlias = symbol ? String(symbol.escapedName) : alias;
132132

@@ -191,7 +191,7 @@ const getImportsAndExports = (
191191

192192
// Module resolver may return DTS references or unaliased npm package names,
193193
// but in the rest of the program we want the package name based on the original specifier.
194-
externalImports.add(sanitizedSpecifier);
194+
external.add(sanitizedSpecifier);
195195
}
196196
}
197197
} else {
@@ -201,9 +201,9 @@ const getImportsAndExports = (
201201

202202
if (typeof pos === 'number') {
203203
const { line, character } = sourceFile.getLineAndCharacterOfPosition(pos);
204-
unresolvedImports.add({ specifier, pos, line: line + 1, col: character + 1 });
204+
unresolved.add({ specifier, pos, line: line + 1, col: character + 1 });
205205
} else {
206-
unresolvedImports.add({ specifier });
206+
unresolved.add({ specifier });
207207
}
208208
}
209209
};
@@ -215,7 +215,7 @@ const getImportsAndExports = (
215215
const importedSymbolFilePath = importedInternalSymbols.get(symbol);
216216
if (importedSymbolFilePath) {
217217
const importId = String(symbol.escapedName);
218-
const internalImport = internalImports.get(importedSymbolFilePath);
218+
const internalImport = internal.get(importedSymbolFilePath);
219219
if (internalImport) {
220220
if (importId !== identifier) {
221221
// Pattern: import { id as alias } from 'specifier'; export = id; export default id;
@@ -316,7 +316,7 @@ const getImportsAndExports = (
316316
if (symbol) {
317317
if (filePath) {
318318
if (!isImportSpecifier(node)) {
319-
const imports = internalImports.get(filePath);
319+
const imports = internal.get(filePath);
320320
if (imports) {
321321
traceRefs.add(id);
322322
if (isAccessExpression(node.parent)) {
@@ -378,7 +378,7 @@ const getImportsAndExports = (
378378
const namespace = left.text;
379379
const { filePath } = getImport(namespace, node);
380380
if (filePath) {
381-
const internalImport = internalImports.get(filePath);
381+
const internalImport = internal.get(filePath);
382382
if (internalImport) addNsMemberRefs(internalImport, namespace, right.text);
383383
}
384384
}
@@ -421,17 +421,9 @@ const getImportsAndExports = (
421421
}
422422

423423
return {
424-
imports: {
425-
internal: internalImports,
426-
external: externalImports,
427-
resolved,
428-
specifiers,
429-
unresolved: unresolvedImports,
430-
},
431-
exports: {
432-
exported: exports,
433-
duplicate: [...aliasedExports.values()],
434-
},
424+
imports: { internal, external, resolved, specifiers, unresolved },
425+
exports,
426+
duplicates: [...aliasedExports.values()],
435427
scripts,
436428
traceRefs,
437429
};

packages/knip/src/util/dependency-graph.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,8 @@ const createFileNode = (): FileNode => ({
4040
external: new Set(),
4141
unresolved: new Set(),
4242
},
43-
exports: {
44-
exported: new Map(),
45-
duplicate: new Set(),
46-
},
43+
exports: new Map(),
44+
duplicates: new Set(),
4745
scripts: new Set(),
4846
traceRefs: new Set(),
4947
});

0 commit comments

Comments
 (0)