Skip to content

Commit aaa1bd0

Browse files
committed
Refactor post-processing of irregular imports/resolveds (#1156)
1 parent cc55504 commit aaa1bd0

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

packages/knip/src/ProjectPrincipal.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -260,25 +260,28 @@ export class ProjectPrincipal {
260260

261261
const { imports, ...rest } = _getImportsAndExports(sourceFile, resolve, typeChecker, { ...options, skipExports });
262262

263-
const { internal, resolved, specifiers, unresolved, external } = imports;
263+
const { internal, resolved: _resolved, specifiers: _specifiers, unresolved: _unresolved, external } = imports;
264264

265-
const unresolvedImports = new Set<UnresolvedImport>();
265+
// Post-processing (1)
266+
const specifiers = new Set<string>();
267+
const resolved = new Set<string>();
268+
const unresolved = new Set<UnresolvedImport>();
266269

267-
for (const [specifier, specifierFilePath] of specifiers) {
270+
for (const [specifier, specifierFilePath] of _specifiers) {
268271
const packageName = getPackageNameFromModuleSpecifier(specifier);
269272
if (packageName && isInternalWorkspace(packageName)) {
270273
external.add(packageName);
271274
const principal = getPrincipalByFilePath(specifierFilePath);
272-
if (principal && !isGitIgnored(specifierFilePath)) principal.addNonEntryPath(specifierFilePath);
275+
if (principal && !isGitIgnored(specifierFilePath)) specifiers.add(specifierFilePath);
273276
}
274277
}
275278

276-
for (const filePath of resolved) {
279+
for (const filePath of _resolved) {
277280
const isIgnored = isGitIgnored(filePath);
278-
if (!isIgnored) this.addEntryPath(filePath, { skipExportsAnalysis: true });
281+
if (!isIgnored) resolved.add(filePath);
279282
}
280283

281-
for (const unresolvedImport of unresolved) {
284+
for (const unresolvedImport of _unresolved) {
282285
const { specifier } = unresolvedImport;
283286

284287
// Ignore Deno style http import specifiers
@@ -295,14 +298,12 @@ export class ProjectPrincipal {
295298
if (!isIgnored) {
296299
const ext = extname(sanitizedSpecifier);
297300
const hasIgnoredExtension = FOREIGN_FILE_EXTENSIONS.has(ext);
298-
if (!ext || (ext !== '.json' && !hasIgnoredExtension)) {
299-
unresolvedImports.add(unresolvedImport);
300-
}
301+
if (!ext || (ext !== '.json' && !hasIgnoredExtension)) unresolved.add(unresolvedImport);
301302
}
302303
}
303304
}
304305

305-
return { imports: { internal, unresolved: unresolvedImports, external }, ...rest };
306+
return { imports: { internal, external, specifiers, resolved, unresolved }, ...rest };
306307
}
307308

308309
invalidateFile(filePath: string) {

packages/knip/src/graph/build.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,10 @@ export async function build({
374374

375375
graph.set(filePath, node);
376376

377-
// A bit out of place, but good spot to add source files referenced from scripts recursively
377+
// Post-processing (2)
378+
for (const filePath of imports.resolved) principal.addEntryPath(filePath, { skipExportsAnalysis: true });
379+
for (const filePath of imports.specifiers) principal.addNonEntryPath(filePath);
380+
378381
if (scripts && scripts.size > 0) {
379382
const dependencies = deputy.getDependencies(workspace.name);
380383
const manifestScriptNames = new Set(Object.keys(chief.getManifestForWorkspace(workspace.name)?.scripts ?? {}));
@@ -385,7 +388,7 @@ export async function build({
385388
input.containingFilePath ??= filePath;
386389
input.dir ??= dir;
387390
const specifierFilePath = getReferencedInternalFilePath(input, workspace);
388-
if (specifierFilePath) analyzeSourceFile(specifierFilePath, principal);
391+
if (specifierFilePath) principal.addEntryPath(specifierFilePath, { skipExportsAnalysis: true });
389392
}
390393
}
391394
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ export type FileNode = {
6161
internal: ImportMap;
6262
external: Set<string>;
6363
unresolved: Set<UnresolvedImport>;
64+
resolved: Set<FilePath>;
65+
specifiers: Set<FilePath>;
6466
};
6567
exports: ExportMap;
6668
duplicates: Iterable<Array<IssueSymbol>>;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const createFileNode = (): FileNode => ({
3838
internal: new Map(),
3939
external: new Set(),
4040
unresolved: new Set(),
41+
resolved: new Set(),
42+
specifiers: new Set(),
4143
},
4244
exports: new Map(),
4345
duplicates: new Set(),

0 commit comments

Comments
 (0)