Skip to content

Commit 171c86a

Browse files
committedMar 11, 2025·
Make _files issue type consistent with the other types
1 parent d6a8c3b commit 171c86a

File tree

7 files changed

+26
-50
lines changed

7 files changed

+26
-50
lines changed
 

‎packages/knip/src/IssueCollector.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ export class IssueCollector {
6262
if (this.isMatch(filePath)) continue;
6363

6464
this.issues.files.add(filePath);
65+
const symbol = relative(filePath);
6566
// @ts-expect-error TODO Fix up in next major
66-
this.issues._files.add({ type: 'files', filePath, symbol: relative(filePath), severity: this.rules.files });
67+
this.issues._files[symbol] = [{ type: 'files', filePath, symbol, severity: this.rules.files }];
6768

6869
this.counters.files++;
6970
this.counters.processed++;

‎packages/knip/src/IssueFixer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class IssueFixer {
6868
private async removeUnusedFiles(issues: Issues) {
6969
if (!this.isFixFiles) return;
7070

71-
for (const issue of issues._files) {
71+
for (const issue of Object.values(issues._files).flatMap(Object.values)) {
7272
await rm(issue.filePath);
7373
issue.isFixed = true;
7474
}

‎packages/knip/src/reporters/compact.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default ({ report, issues, isShowProgress }: ReporterOptions) => {
2121
? Object.values(issues[reportType]).flatMap(Object.values)
2222
: Object.values(issues[reportType] as IssueRecords).map(issues => {
2323
const items = Object.values(issues);
24-
return { ...items[0], symbols: items.map(issue => issue.symbol) };
24+
return { ...items[0], symbols: items };
2525
});
2626

2727
if (issuesForType.length > 0) {

‎packages/knip/src/reporters/disclosure.ts

+11-22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import EasyTable from 'easy-table';
22
import type { Entries } from 'type-fest';
33
import type { Issue, ReporterOptions } from '../types/issues.js';
4-
import { relative, toRelative } from '../util/path.js';
4+
import { relative } from '../util/path.js';
55
import { getTitle } from './util.js';
66

77
const printHeader = (size: number, title?: string) =>
@@ -16,7 +16,8 @@ const logIssueRecord = (issues: Issue[]) => {
1616
issue.parentSymbol && table.cell('parentSymbol', issue.parentSymbol);
1717
issue.symbolType && table.cell('symbolType', issue.symbolType);
1818
const pos = issue.line === undefined ? '' : `:${issue.line}${issue.col === undefined ? '' : `:${issue.col}`}`;
19-
const cell = `${relative(issue.filePath)}${pos}`;
19+
// @ts-expect-error TODO Fix up in next major
20+
const cell = issue.type === 'files' ? '' : `${relative(issue.filePath)}${pos}`;
2021
table.cell('filePath', cell);
2122
table.newRow();
2223
}
@@ -27,29 +28,17 @@ export default ({ report, issues }: ReporterOptions) => {
2728
const reportMultipleGroups = Object.values(report).filter(Boolean).length > 1;
2829
let totalIssues = 0;
2930

30-
for (const [reportType, isReportType] of Object.entries(report) as Entries<typeof report>) {
31-
if (reportType === '_files') continue;
31+
for (let [reportType, isReportType] of Object.entries(report) as Entries<typeof report>) {
32+
if (reportType === 'files') reportType = '_files';
3233

3334
if (isReportType) {
3435
const title = reportMultipleGroups ? getTitle(reportType) : undefined;
35-
36-
if (reportType === 'files') {
37-
const issuesForType = Array.from(issues._files);
38-
if (issuesForType.length > 0) {
39-
printHeader(issuesForType.length, title);
40-
const sortedIssues = issuesForType.sort((a, b) => a.filePath.localeCompare(b.filePath));
41-
for (const issue of sortedIssues) console.log(toRelative(issue.filePath));
42-
totalIssues = totalIssues + issuesForType.length;
43-
printFooter();
44-
}
45-
} else {
46-
const issuesForType = Object.values(issues[reportType]).flatMap(Object.values);
47-
if (issuesForType.length > 0) {
48-
printHeader(issuesForType.length, title);
49-
logIssueRecord(issuesForType);
50-
totalIssues = totalIssues + issuesForType.length;
51-
printFooter();
52-
}
36+
const issuesForType = Object.values(issues[reportType]).flatMap(Object.values);
37+
if (issuesForType.length > 0) {
38+
printHeader(issuesForType.length, title);
39+
logIssueRecord(issuesForType);
40+
totalIssues = totalIssues + issuesForType.length;
41+
printFooter();
5342
}
5443
}
5544
}

‎packages/knip/src/reporters/symbols.ts

+8-22
Original file line numberDiff line numberDiff line change
@@ -52,33 +52,19 @@ export default ({ report, issues, tagHints, configurationHints, noConfigHints, i
5252
const reportMultipleGroups = Object.values(report).filter(Boolean).length > 1;
5353
let totalIssues = 0;
5454

55-
for (const [reportType, isReportType] of Object.entries(report) as Entries<typeof report>) {
56-
if (reportType === '_files') continue;
55+
for (let [reportType, isReportType] of Object.entries(report) as Entries<typeof report>) {
56+
if (reportType === 'files') reportType = '_files';
5757

5858
if (isReportType) {
5959
const title = reportMultipleGroups && getTitle(reportType);
6060

61-
if (reportType === 'files') {
62-
const issuesForType = Array.from(issues._files);
63-
if (issuesForType.length > 0) {
64-
title && logTitle(title, issuesForType.length);
65-
const sortedIssues = issuesForType.sort((a, b) => a.filePath.localeCompare(b.filePath));
66-
for (const issue of sortedIssues) {
67-
const relPath = toRelative(issue.filePath);
68-
if (issue.isFixed) console.log(dim(`${relPath} (removed)`));
69-
else if (issue.severity === 'warn') console.log(dim(relPath));
70-
else console.log(relPath);
71-
}
72-
totalIssues = totalIssues + issuesForType.length;
73-
}
74-
} else {
75-
const issuesForType = Object.values(issues[reportType]).flatMap(Object.values);
76-
if (issuesForType.length > 0) {
77-
title && logTitle(title, issuesForType.length);
78-
logIssueRecord(issuesForType);
79-
totalIssues = totalIssues + issuesForType.length;
80-
}
61+
const issuesForType = Object.values(issues[reportType]).flatMap(Object.values);
62+
if (issuesForType.length > 0) {
63+
title && logTitle(title, issuesForType.length);
64+
logIssueRecord(issuesForType);
65+
totalIssues = totalIssues + issuesForType.length;
8166
}
67+
// }
8268
}
8369
}
8470

‎packages/knip/src/types/issues.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export type IssueRecords = Record<string, Record<string, Issue>>;
3232

3333
export type Issues = {
3434
files: IssueSet;
35-
_files: Set<Issue>;
35+
_files: IssueRecords;
3636
dependencies: IssueRecords;
3737
devDependencies: IssueRecords;
3838
optionalPeerDependencies: IssueRecords;
@@ -50,7 +50,7 @@ export type Issues = {
5050

5151
export type IssueType = keyof Issues;
5252

53-
export type SymbolIssueType = Exclude<IssueType, 'files' | '_files'>;
53+
export type SymbolIssueType = Exclude<IssueType, 'files'>;
5454

5555
export type Report = {
5656
[key in keyof Issues]: boolean;

‎packages/knip/src/util/issue-initializers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Counters, Issue, IssueType, Issues, Rules } from '../types/issues.
44
export const initIssues = (): Issues => ({
55
...(Object.fromEntries(ISSUE_TYPES.map(issueType => [issueType, {}])) as Record<IssueType, never>),
66
files: new Set<string>(),
7-
_files: new Set<Issue>(),
7+
_files: {} as Record<string, Record<string, Issue>>,
88
});
99

1010
export const initCounters = (): Counters => ({

0 commit comments

Comments
 (0)
Please sign in to comment.