Skip to content

Commit

Permalink
Add disclosure reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Oct 31, 2024
1 parent 3354e3a commit c5f0ee6
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
53 changes: 51 additions & 2 deletions packages/docs/src/content/docs/features/reporters.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ Knip provides the following built-in reporters:

- `codeowners`
- `compact`
- `json`
- `markdown`
- [`disclosure`](#disclosure)
- [`json`](#json)
- [`markdown`](#markdown)
- `symbol` (default)

Example usage:
Expand Down Expand Up @@ -103,6 +104,54 @@ Markdown tables separated by issue types as headings, for example:
| ./unresolved | src/index.ts:10:12 | error |
```

### Disclosure

This reporter is useful for sharing large reports. Groups of issues are rendered
in a closed state initially. The reporter renders this:

````text
$ knip --reporter disclosure
<details><summary>Unused files (2)</summary>
```
unused.ts
dangling.js
```
</details>
<details><summary>Unused dependencies (2)</summary>
```
unused-dep package.json
my-package package.json
```
</details>
````

The above can be copy-pasted where HTML and Markdown is supported, such as a
GitHub issue or pull request, and renders like so:

<details><summary>Unused files (2)</summary>

```
unused.ts
dangling.js
```

</details>

<details><summary>Unused dependencies (2)</summary>

```
unused-dep package.json
my-package package.json
```

</details>

## Custom Reporters

When the provided built-in reporters are not sufficient, a custom local reporter
Expand Down
56 changes: 56 additions & 0 deletions packages/knip/src/reporters/disclosure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import EasyTable from 'easy-table';
import type { Entries } from 'type-fest';
import type { Issue, ReporterOptions } from '../types/issues.js';
import { relative, toRelative } from '../util/path.js';
import { getTitle } from './util.js';

const printHeader = (size: number, title?: string) =>
console.log(`<details>${title ? `<summary>${title} (${size})</summary>` : ''}\n\n\`\`\``);

const printFooter = () => console.log('```\n\n</details>\n');

const logIssueRecord = (issues: Issue[]) => {
const table = new EasyTable();
for (const issue of issues) {
table.cell('symbol', issue.symbols ? issue.symbols.map(s => s.symbol).join(', ') : issue.symbol);
issue.parentSymbol && table.cell('parentSymbol', issue.parentSymbol);
issue.symbolType && table.cell('symbolType', issue.symbolType);
const pos = issue.line === undefined ? '' : `:${issue.line}${issue.col === undefined ? '' : `:${issue.col}`}`;
const cell = `${relative(issue.filePath)}${pos}`;
table.cell('filePath', cell);
table.newRow();
}
console.log(table.sort(['filePath', 'parentSymbol', 'symbol']).print().trim());
};

export default ({ report, issues }: ReporterOptions) => {
const reportMultipleGroups = Object.values(report).filter(Boolean).length > 1;
let totalIssues = 0;

for (const [reportType, isReportType] of Object.entries(report) as Entries<typeof report>) {
if (reportType === '_files') continue;

if (isReportType) {
const title = reportMultipleGroups ? getTitle(reportType) : undefined;

if (reportType === 'files') {
const issuesForType = Array.from(issues._files);
if (issuesForType.length > 0) {
printHeader(issuesForType.length, title);
const sortedIssues = issuesForType.sort((a, b) => a.filePath.localeCompare(b.filePath));
for (const issue of sortedIssues) console.log(toRelative(issue.filePath));
totalIssues = totalIssues + issuesForType.length;
printFooter();
}
} else {
const issuesForType = Object.values(issues[reportType]).flatMap(Object.values);
if (issuesForType.length > 0) {
printHeader(issuesForType.length, title);
logIssueRecord(issuesForType);
totalIssues = totalIssues + issuesForType.length;
printFooter();
}
}
}
}
};
2 changes: 2 additions & 0 deletions packages/knip/src/reporters/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import codeowners from './codeowners.js';
import compact from './compact.js';
import disclosure from './disclosure.js';
import json from './json.js';
import markdown from './markdown.js';
import symbols from './symbols.js';
Expand All @@ -8,6 +9,7 @@ export default {
symbols,
compact,
codeowners,
disclosure,
json,
markdown,
};

0 comments on commit c5f0ee6

Please sign in to comment.