Skip to content

Commit 0b02923

Browse files
authoredFeb 8, 2024
feat: improve markdown reporter (#502)
- make the output valid for prettier - add line+col to the location of issue - add severity as it's own column
1 parent d99f7d8 commit 0b02923

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed
 

‎packages/docs/src/content/docs/features/reporters.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,16 @@ markdown tables separated by issue types as headings, for example:
9191

9292
## Unlisted dependencies (2)
9393

94-
| Name | Location |
95-
| :-------------- | :----------- |
96-
| unresolved | src/index.ts |
97-
| @org/unresolved | src/index.ts |
94+
| Name | Location | Severity |
95+
| :-------------- | :---------------- | :------- |
96+
| unresolved | src/index.ts:8:23 | error |
97+
| @org/unresolved | src/index.ts:9:23 | error |
9898

9999
## Unresolved imports (1)
100100

101-
| Name | Location |
102-
| :----------- | :----------- |
103-
| ./unresolved | src/index.ts |
101+
| Name | Location | Severity |
102+
| :----------- | :----------------- | :------- |
103+
| ./unresolved | src/index.ts:10:12 | error |
104104
```
105105

106106
## Custom Reporters
@@ -170,7 +170,7 @@ line (can be repeated).
170170
The default export of the preprocessor should be a function with this interface:
171171

172172
```ts
173-
type Preprocessor = async (options: ReporterOptions) => ReporterOptions;
173+
type Preprocessor = async (options: ReporterOptions) => ReporterOptions;
174174
```
175175

176176
Like reporters, you can use local JavaScript or TypeScript files and external

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

+16-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ import type { Entries } from 'type-fest';
66
export default ({ report, issues }: ReporterOptions) => {
77
console.log('# Knip report\n');
88

9+
const getFilePath = (issue: Issue) => {
10+
if (!issue.line || !issue.col) return relative(issue.filePath);
11+
return `${relative(issue.filePath)}:${issue.line}:${issue.col}`;
12+
};
13+
const sortLongestSymbol = (a: Issue, b: Issue) => b.symbol.length - a.symbol.length;
14+
const sortLongestFilePath = (a: Issue, b: Issue) => getFilePath(b).length - getFilePath(a).length;
15+
916
for (const [reportType, isReportType] of Object.entries(report) as Entries<typeof report>) {
1017
if (isReportType) {
1118
const title = getTitle(reportType);
@@ -21,16 +28,17 @@ export default ({ report, issues }: ReporterOptions) => {
2128
console.log(`* ${toRelative(issue)}`);
2229
});
2330
} else {
24-
const longestSymbol = issuesForType.sort((a, b) => b.symbol.length - a.symbol.length)[0].symbol.length;
25-
const longestFilePath = relative(
26-
issuesForType.sort((a, b) => relative(b.filePath).length - relative(a.filePath).length)[0].filePath
27-
).length;
28-
const sortedByFilePath = issuesForType.sort((a, b) => (a.filePath > b.filePath ? 1 : -1));
29-
console.log(`| ${`Name`.padEnd(longestSymbol)} | ${`Location`.padEnd(longestFilePath)} |`);
30-
console.log(`|:${'-'.repeat(longestSymbol + 1)}|:${'-'.repeat(longestFilePath + 1)}|`);
31+
const longestSymbol = issuesForType.sort(sortLongestSymbol)[0].symbol.length;
32+
const sortedByFilePath = issuesForType.sort(sortLongestFilePath);
33+
const longestFilePath = getFilePath(sortedByFilePath[0]).length;
34+
35+
console.log(`| ${`Name`.padEnd(longestSymbol)} | ${`Location`.padEnd(longestFilePath)} | Severity |`);
36+
console.log(`| :${'-'.repeat(longestSymbol - 1)} | :${'-'.repeat(longestFilePath - 1)} | :------- |`);
3137
sortedByFilePath.forEach((issue: Issue) => {
3238
console.log(
33-
`| ${issue.symbol.padEnd(longestSymbol)} | ${relative(issue.filePath).padEnd(longestFilePath)} |`
39+
`| ${issue.symbol.padEnd(longestSymbol)} | ${getFilePath(issue).padEnd(longestFilePath)} | ${(
40+
issue.severity ?? ''
41+
).padEnd(8)} |`
3442
);
3543
});
3644
}

‎packages/knip/test/cli-reporter-markdown.test.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ test('knip --reporter markdown', () => {
1616
1717
## Unlisted dependencies (2)
1818
19-
| Name | Location |
20-
|:----------------|:-------------|
21-
| unresolved | src/index.ts |
22-
| @org/unresolved | src/index.ts |
19+
| Name | Location | Severity |
20+
| :-------------- | :----------- | :------- |
21+
| @org/unresolved | src/index.ts | error |
22+
| unresolved | src/index.ts | error |
2323
2424
## Unresolved imports (1)
2525
26-
| Name | Location |
27-
|:-------------|:-------------|
28-
| ./unresolved | src/index.ts |
26+
| Name | Location | Severity |
27+
| :----------- | :---------------- | :------- |
28+
| ./unresolved | src/index.ts:8:23 | error |
2929
3030
`;
3131
const out = exec('knip --reporter markdown').stdout;

0 commit comments

Comments
 (0)
Please sign in to comment.