Reporters & Preprocessors
Built-in Reporters
Knip provides the following built-in reporters:
codeowners
compact
disclosure
json
markdown
symbol
(default)
Example usage:
JSON
The built-in json
reporter output is meant to be consumed by other tools. It
reports in JSON format with unused files
and issues
as an array with one
object per file structured like this:
The keys match the reported issue types. Example usage:
Markdown
The built-in markdown
reporter output is meant to be saved to a Markdown file.
This allows following the changes in issues over time. It reports issues in
Markdown tables separated by issue types as headings, for example:
Disclosure
This reporter is useful for sharing large reports. Groups of issues are rendered in a closed state initially. The reporter renders this:
The above can be copy-pasted where HTML and Markdown is supported, such as a GitHub issue or pull request, and renders like so:
Unused files (2)
Unused dependencies (2)
Custom Reporters
When the provided built-in reporters are not sufficient, a custom local reporter
can be implemented or an external reporter can be used. Multiple reporters can
be used at once by repeating the --reporter
argument.
The results are passed to the function from its default export and can be used
to write issues to stdout
, a JSON or CSV file, or sent to a service. It
supports a local JavaScript or TypeScript file or an external dependency.
Local
The default export of the reporter should be a function with this interface:
The data can then be used to write issues to stdout
, a JSON or CSV file, or
sent to a service.
Here’s a most minimal reporter example:
Example usage:
External
Pass --reporter [pkg-name]
to use an external reporter. The default exported
function of the main
script (default: index.js
) will be invoked with the
ReporterOptions
, just like a local reporter.
Preprocessors
A preprocessor is a function that runs after the analysis is finished. It receives the results from the analysis and should return data in the same shape/structure (unless you pass it to only your own reporter).
The data goes through the preprocessors before the final data is passed to the
reporters. There are no built-in preprocessors. Just like reporters, use e.g.
--preprocessor ./my-preprocessor
from the command line (can be repeated).
The default export of the preprocessor should be a function with this interface:
Like reporters, you can use local JavaScript or TypeScript files and external npm packages as preprocessors.
Example preprocessor:
Example usage:
ISC License © 2024 Lars Kappert