Skip to content

Commit

Permalink
Keep track of analyzed files per resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Apr 16, 2024
1 parent 9ad7a3c commit 890375f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/knip/fixtures/plugins/metro/src/About.android.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => 1;
1 change: 1 addition & 0 deletions packages/knip/fixtures/plugins/metro/src/About.ios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => 1;
1 change: 1 addition & 0 deletions packages/knip/fixtures/plugins/metro/src/About.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => 1;
12 changes: 9 additions & 3 deletions packages/knip/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => {

debugLog('*', `Created ${principals.length} programs for ${workspaces.length} workspaces`);

const analyzedFiles = new Set<string>();
const analyzed = new Map<string, Set<string>>();
const exportedSymbols: SerializableExportMap = {};
const importedSymbols: SerializableImportMap = {};
const unreferencedFiles = new Set<string>();
Expand All @@ -303,6 +303,11 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => {
for (const principal of principals) {
principal.init();

const [moduleResolverName] = principal.moduleResolver;

if (!analyzed.has(moduleResolverName)) analyzed.set(moduleResolverName, new Set<string>());
const analyzedFiles = analyzed.get(moduleResolverName)!;

principal.referencedDependencies.forEach(([containingFilePath, specifier, workspaceName]) => {
const workspace = chief.findWorkspaceByName(workspaceName);
if (workspace) handleReferencedDependency({ specifier, containingFilePath, principal, workspace });
Expand Down Expand Up @@ -386,8 +391,6 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => {

await principal.runAsyncCompilers();

const [moduleResolverName] = principal.moduleResolver;

streamer.cast(`Connecting the dots...${moduleResolverName === 'default' ? '' : ` (${moduleResolverName})`}`);

let size = principal.entryPaths.size;
Expand Down Expand Up @@ -618,6 +621,9 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => {
}
}

const analyzedFiles = new Set<string>();
for (const files of analyzed.values()) for (const filePath of files.values()) analyzedFiles.add(filePath);

const unusedFiles = [...unreferencedFiles].filter(filePath => !analyzedFiles.has(filePath));

collector.addFilesIssues(unusedFiles);
Expand Down

0 comments on commit 890375f

Please sign in to comment.