@knip/mcp 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +53 -0
- package/docs/blog/brief-history.md +30 -0
- package/docs/blog/knip-v3.mdx +88 -0
- package/docs/blog/knip-v4.mdx +149 -0
- package/docs/blog/knip-v5.mdx +190 -0
- package/docs/blog/migration-to-v1.md +65 -0
- package/docs/blog/release-notes-v2.md +46 -0
- package/docs/blog/slim-down-to-speed-up.md +269 -0
- package/docs/blog/state-of-knip.md +191 -0
- package/docs/blog/two-years.mdx +107 -0
- package/docs/docs/blog/brief-history.md +30 -0
- package/docs/docs/blog/for-editors-and-agents.md +109 -0
- package/docs/docs/blog/knip-v3.mdx +88 -0
- package/docs/docs/blog/knip-v4.mdx +149 -0
- package/docs/docs/blog/knip-v5.mdx +190 -0
- package/docs/docs/blog/migration-to-v1.md +65 -0
- package/docs/docs/blog/release-notes-v2.md +46 -0
- package/docs/docs/blog/slim-down-to-speed-up.md +269 -0
- package/docs/docs/blog/state-of-knip.md +191 -0
- package/docs/docs/blog/two-years.mdx +107 -0
- package/docs/docs/explanations/comparison-and-migration.md +129 -0
- package/docs/docs/explanations/entry-files.md +70 -0
- package/docs/docs/explanations/plugins.md +318 -0
- package/docs/docs/explanations/why-use-knip.md +128 -0
- package/docs/docs/features/auto-fix.mdx +333 -0
- package/docs/docs/features/compilers.md +172 -0
- package/docs/docs/features/integrated-monorepos.md +52 -0
- package/docs/docs/features/monorepos-and-workspaces.md +134 -0
- package/docs/docs/features/production-mode.md +95 -0
- package/docs/docs/features/reporters.md +302 -0
- package/docs/docs/features/rules-and-filters.md +102 -0
- package/docs/docs/features/script-parser.md +156 -0
- package/docs/docs/features/source-mapping.md +100 -0
- package/docs/docs/guides/configuring-project-files.md +205 -0
- package/docs/docs/guides/contributing.md +24 -0
- package/docs/docs/guides/handling-issues.mdx +646 -0
- package/docs/docs/guides/issue-reproduction.md +94 -0
- package/docs/docs/guides/namespace-imports.md +125 -0
- package/docs/docs/guides/performance.md +97 -0
- package/docs/docs/guides/troubleshooting.md +127 -0
- package/docs/docs/guides/using-knip-in-ci.md +54 -0
- package/docs/docs/guides/working-with-commonjs.md +72 -0
- package/docs/docs/index.mdx +160 -0
- package/docs/docs/overview/configuration.md +104 -0
- package/docs/docs/overview/features.md +66 -0
- package/docs/docs/overview/getting-started.mdx +195 -0
- package/docs/docs/overview/screenshots-videos.md +42 -0
- package/docs/docs/playground.mdx +38 -0
- package/docs/docs/reference/cli.md +481 -0
- package/docs/docs/reference/configuration.md +413 -0
- package/docs/docs/reference/dynamic-configuration.mdx +72 -0
- package/docs/docs/reference/faq.md +441 -0
- package/docs/docs/reference/issue-types.md +43 -0
- package/docs/docs/reference/jsdoc-tsdoc-tags.md +122 -0
- package/docs/docs/reference/known-issues.md +64 -0
- package/docs/docs/reference/plugins/.gitkeep +0 -0
- package/docs/docs/reference/plugins.md +238 -0
- package/docs/docs/reference/related-tooling.md +46 -0
- package/docs/docs/sponsors.mdx +65 -0
- package/docs/docs/typescript/unused-dependencies.md +86 -0
- package/docs/docs/typescript/unused-exports.md +87 -0
- package/docs/docs/writing-a-plugin/argument-parsing.md +202 -0
- package/docs/docs/writing-a-plugin/index.md +376 -0
- package/docs/docs/writing-a-plugin/inputs.md +162 -0
- package/docs/explanations/comparison-and-migration.md +129 -0
- package/docs/explanations/entry-files.md +70 -0
- package/docs/explanations/plugins.md +318 -0
- package/docs/explanations/why-use-knip.md +128 -0
- package/docs/features/auto-fix.mdx +333 -0
- package/docs/features/compilers.md +172 -0
- package/docs/features/integrated-monorepos.md +52 -0
- package/docs/features/monorepos-and-workspaces.md +134 -0
- package/docs/features/production-mode.md +95 -0
- package/docs/features/reporters.md +302 -0
- package/docs/features/rules-and-filters.md +102 -0
- package/docs/features/script-parser.md +156 -0
- package/docs/features/source-mapping.md +100 -0
- package/docs/guides/configuring-project-files.md +205 -0
- package/docs/guides/contributing.md +24 -0
- package/docs/guides/handling-issues.mdx +646 -0
- package/docs/guides/issue-reproduction.md +94 -0
- package/docs/guides/namespace-imports.md +125 -0
- package/docs/guides/performance.md +97 -0
- package/docs/guides/troubleshooting.md +127 -0
- package/docs/guides/using-knip-in-ci.md +54 -0
- package/docs/guides/working-with-commonjs.md +72 -0
- package/docs/index.mdx +156 -0
- package/docs/overview/configuration.md +104 -0
- package/docs/overview/features.md +66 -0
- package/docs/overview/getting-started.mdx +195 -0
- package/docs/overview/screenshots-videos.md +42 -0
- package/docs/playground.mdx +38 -0
- package/docs/reference/cli.md +481 -0
- package/docs/reference/configuration.md +413 -0
- package/docs/reference/dynamic-configuration.mdx +72 -0
- package/docs/reference/faq.md +441 -0
- package/docs/reference/issue-types.md +43 -0
- package/docs/reference/jsdoc-tsdoc-tags.md +122 -0
- package/docs/reference/known-issues.md +64 -0
- package/docs/reference/plugins/.gitkeep +0 -0
- package/docs/reference/plugins.md +238 -0
- package/docs/reference/related-tooling.md +46 -0
- package/docs/sponsors.mdx +65 -0
- package/docs/typescript/unused-dependencies.md +86 -0
- package/docs/typescript/unused-exports.md +87 -0
- package/docs/writing-a-plugin/argument-parsing.md +202 -0
- package/docs/writing-a-plugin/index.md +376 -0
- package/docs/writing-a-plugin/inputs.md +162 -0
- package/license +15 -0
- package/package.json +38 -0
- package/src/cli.js +13 -0
- package/src/curated-resources.js +62 -0
- package/src/server.js +129 -0
- package/src/texts.js +76 -0
- package/src/tools.js +68 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Monorepos & Workspaces
|
|
3
|
+
sidebar:
|
|
4
|
+
order: 2
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Workspaces are handled out-of-the-box by Knip.
|
|
8
|
+
|
|
9
|
+
Workspaces are sometimes also referred to as package-based monorepos, or as
|
|
10
|
+
packages in a monorepo. Knip uses the term workspace exclusively to indicate a
|
|
11
|
+
directory that has a `package.json`.
|
|
12
|
+
|
|
13
|
+
## Configuration
|
|
14
|
+
|
|
15
|
+
Here's example configuration with custom `entry` and `project` patterns:
|
|
16
|
+
|
|
17
|
+
```json title="knip.json"
|
|
18
|
+
{
|
|
19
|
+
"workspaces": {
|
|
20
|
+
".": {
|
|
21
|
+
"entry": "scripts/*.js",
|
|
22
|
+
"project": "scripts/**/*.js"
|
|
23
|
+
},
|
|
24
|
+
"packages/*": {
|
|
25
|
+
"entry": "{index,cli}.ts",
|
|
26
|
+
"project": "**/*.ts"
|
|
27
|
+
},
|
|
28
|
+
"packages/cli": {
|
|
29
|
+
"entry": "bin/cli.js"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
:::tip
|
|
36
|
+
|
|
37
|
+
Run Knip without any configuration to see if and where custom `entry` and/or
|
|
38
|
+
`project` files are necessary per workspace.
|
|
39
|
+
|
|
40
|
+
:::
|
|
41
|
+
|
|
42
|
+
Each workspace has the same [default configuration][1].
|
|
43
|
+
|
|
44
|
+
The root workspace is named `"."` under `workspaces` (like in the example
|
|
45
|
+
above).
|
|
46
|
+
|
|
47
|
+
:::caution
|
|
48
|
+
|
|
49
|
+
In a project with workspaces, the `entry` and `project` options at the root
|
|
50
|
+
level are ignored. Use the workspace named `"."` for those (like in the example
|
|
51
|
+
above).
|
|
52
|
+
|
|
53
|
+
:::
|
|
54
|
+
|
|
55
|
+
## Workspaces
|
|
56
|
+
|
|
57
|
+
Knip reads workspaces from four possible locations:
|
|
58
|
+
|
|
59
|
+
1. The `workspaces` array in `package.json` (npm, Bun, Yarn, Lerna)
|
|
60
|
+
2. The `packages` array in `pnpm-workspace.yaml` (pnpm)
|
|
61
|
+
3. The `workspaces.packages` array in `package.json` (legacy)
|
|
62
|
+
4. The `workspaces` object in Knip configuration
|
|
63
|
+
|
|
64
|
+
The `workspaces` in Knip configuration (4) not already defined in the root
|
|
65
|
+
`package.json` or `pnpm-workspace.yaml` (1, 2, 3) are added to the analysis.
|
|
66
|
+
|
|
67
|
+
:::caution
|
|
68
|
+
|
|
69
|
+
A workspace must have a `package.json` file.
|
|
70
|
+
|
|
71
|
+
:::
|
|
72
|
+
|
|
73
|
+
For projects with only a root `package.json`, please see [integrated
|
|
74
|
+
monorepos][2].
|
|
75
|
+
|
|
76
|
+
## Additional workspaces
|
|
77
|
+
|
|
78
|
+
If a workspaces is not configured as such in `package.json#workspaces` (or
|
|
79
|
+
`pnpm-workspace.yaml`) it can be added to the Knip configuration manually. Add
|
|
80
|
+
their path to the `workspaces` configuration object the same way as
|
|
81
|
+
`"packages/cli": {}` in the example above.
|
|
82
|
+
|
|
83
|
+
## Source mapping
|
|
84
|
+
|
|
85
|
+
See [Source Mapping][3].
|
|
86
|
+
|
|
87
|
+
## Additional options
|
|
88
|
+
|
|
89
|
+
The following options are available inside workspace configurations:
|
|
90
|
+
|
|
91
|
+
- [ignore][4]
|
|
92
|
+
- [ignoreBinaries][5]
|
|
93
|
+
- [ignoreDependencies][6]
|
|
94
|
+
- [ignoreMembers][7]
|
|
95
|
+
- [ignoreUnresolved][8]
|
|
96
|
+
- [includeEntryExports][9]
|
|
97
|
+
|
|
98
|
+
[Plugins][10] can be configured separately per workspace.
|
|
99
|
+
|
|
100
|
+
Use `--debug` for verbose output and see the workspaces Knip includes, their
|
|
101
|
+
configurations, enabled plugins, glob options and resolved files.
|
|
102
|
+
|
|
103
|
+
## Lint a single workspace
|
|
104
|
+
|
|
105
|
+
Use the `--workspace` (or `-W`) argument to focus on a single workspace (and let
|
|
106
|
+
Knip run faster). Example:
|
|
107
|
+
|
|
108
|
+
```sh
|
|
109
|
+
knip --workspace packages/my-lib
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
This will include the target workspace, but also ancestor and dependent
|
|
113
|
+
workspaces. For two reasons:
|
|
114
|
+
|
|
115
|
+
- Ancestor workspaces may list dependencies in `package.json` the linted
|
|
116
|
+
workspace uses.
|
|
117
|
+
- Dependent workspaces may reference exports from the linted workspace.
|
|
118
|
+
|
|
119
|
+
To lint the workspace in isolation, there are two options:
|
|
120
|
+
|
|
121
|
+
- Combine the `workspace` argument with [strict production mode][11].
|
|
122
|
+
- Run Knip from inside the workspace directory.
|
|
123
|
+
|
|
124
|
+
[1]: ../overview/configuration.md#defaults
|
|
125
|
+
[2]: ./integrated-monorepos.md
|
|
126
|
+
[3]: ./source-mapping.md
|
|
127
|
+
[4]: ../reference/configuration.md#ignore
|
|
128
|
+
[5]: ../reference/configuration.md#ignorebinaries
|
|
129
|
+
[6]: ../reference/configuration.md#ignoredependencies
|
|
130
|
+
[7]: ../reference/configuration.md#ignoremembers
|
|
131
|
+
[8]: ../reference/configuration.md#ignoreunresolved
|
|
132
|
+
[9]: ../reference/configuration.md#includeentryexports
|
|
133
|
+
[10]: ../reference/configuration.md#plugins
|
|
134
|
+
[11]: ./production-mode.md#strict-mode
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Production Mode
|
|
3
|
+
sidebar:
|
|
4
|
+
order: 1
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
The default mode for Knip is comprehensive and targets all project code,
|
|
8
|
+
including configuration files, test files, Storybook stories, and so on. Test
|
|
9
|
+
files usually import production files. This prevents production files or their
|
|
10
|
+
exports from being reported as unused, while sometimes both of them can be
|
|
11
|
+
deleted. Knip features a "production mode" to focus only on the code that you
|
|
12
|
+
ship.
|
|
13
|
+
|
|
14
|
+
## Configuration
|
|
15
|
+
|
|
16
|
+
To tell Knip what is production code, add an exclamation mark behind each
|
|
17
|
+
`pattern!` that represents production code:
|
|
18
|
+
|
|
19
|
+
```json title="knip.json"
|
|
20
|
+
{
|
|
21
|
+
"entry": ["src/index.ts!", "build/script.js"],
|
|
22
|
+
"project": ["src/**/*.ts!", "build/*.js"]
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Depending on file structure and enabled plugins, you might not need to modify
|
|
27
|
+
your configuration at all.
|
|
28
|
+
|
|
29
|
+
Run Knip with the `--production` flag:
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
knip --production
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Here's what's included in production mode:
|
|
36
|
+
|
|
37
|
+
- Only `entry` and `project` patterns suffixed with `!`
|
|
38
|
+
- Only production `entry` file patterns exported by plugins (such as Next.js and
|
|
39
|
+
Remix)
|
|
40
|
+
- Only the `start` and `postinstall` scripts
|
|
41
|
+
- Ignore exports with the [`@internal` tag][1]
|
|
42
|
+
|
|
43
|
+
:::note
|
|
44
|
+
|
|
45
|
+
The production run does not replace the default run. Depending on your needs you
|
|
46
|
+
can run either of them or both separately. Usually both modes can share the same
|
|
47
|
+
configuration.
|
|
48
|
+
|
|
49
|
+
:::
|
|
50
|
+
|
|
51
|
+
To see the difference between default and production mode in great detail, use
|
|
52
|
+
the `--debug` flag and inspect what entry and project files are used, and the
|
|
53
|
+
plugins that are enabled. For instance, in production mode this shows that files
|
|
54
|
+
such as tests and Storybook files (stories) are excluded from the analysis.
|
|
55
|
+
|
|
56
|
+
In case files like mocks and test helpers are reported as unused files, use
|
|
57
|
+
negated patterns to exclude those files in production mode:
|
|
58
|
+
|
|
59
|
+
```json title="knip.json"
|
|
60
|
+
{
|
|
61
|
+
"entry": ["src/index.ts!"],
|
|
62
|
+
"project": ["src/**/*.ts!", "!src/test-helpers/**!"]
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Also see [configuring project files][2] to align `entry` and `project` with
|
|
67
|
+
production mode.
|
|
68
|
+
|
|
69
|
+
## Strict Mode
|
|
70
|
+
|
|
71
|
+
In production mode, only `dependencies` (not `devDependencies`) are considered
|
|
72
|
+
when finding unused or unlisted dependencies.
|
|
73
|
+
|
|
74
|
+
Additionally, the `--strict` flag can be added to:
|
|
75
|
+
|
|
76
|
+
- Verify isolation: workspaces should use strictly their own `dependencies`
|
|
77
|
+
- Include `peerDependencies` when finding unused or unlisted dependencies
|
|
78
|
+
- Report type-only imports listed in `dependencies`
|
|
79
|
+
|
|
80
|
+
```sh
|
|
81
|
+
knip --production --strict
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Using `--strict` implies `--production`, so the latter can be omitted.
|
|
85
|
+
|
|
86
|
+
## Types
|
|
87
|
+
|
|
88
|
+
Add `--exclude types` if you don't want to include types in the report:
|
|
89
|
+
|
|
90
|
+
```sh
|
|
91
|
+
knip --production --exclude types
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
[1]: ../reference/jsdoc-tsdoc-tags.md#internal
|
|
95
|
+
[2]: ../guides/configuring-project-files.md
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Reporters & Preprocessors
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
## Built-in Reporters
|
|
6
|
+
|
|
7
|
+
Knip provides the following built-in reporters:
|
|
8
|
+
|
|
9
|
+
- `codeowners`
|
|
10
|
+
- `compact`
|
|
11
|
+
- [`disclosure`][1]
|
|
12
|
+
- [`github-actions`][2]
|
|
13
|
+
- [`json`][3]
|
|
14
|
+
- [`markdown`][4]
|
|
15
|
+
- [`codeclimate`][5]
|
|
16
|
+
- `symbols` (default)
|
|
17
|
+
|
|
18
|
+
Example usage:
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
knip --reporter compact
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### JSON
|
|
25
|
+
|
|
26
|
+
The built-in `json` reporter output is meant to be consumed by other tools. It
|
|
27
|
+
reports in JSON format with unused `files` and `issues` as an array with one
|
|
28
|
+
object per file structured like this:
|
|
29
|
+
|
|
30
|
+
```json
|
|
31
|
+
{
|
|
32
|
+
"files": ["src/unused.ts"],
|
|
33
|
+
"issues": [
|
|
34
|
+
{
|
|
35
|
+
"file": "package.json",
|
|
36
|
+
"owners": ["@org/admin"],
|
|
37
|
+
"dependencies": [{ "name": "jquery", "line": 5, "col": 6, "pos": 71 }],
|
|
38
|
+
"devDependencies": [{ "name": "lodash", "line": 9, "col": 6, "pos": 99 }],
|
|
39
|
+
"unlisted": [{ "name": "react" }, { "name": "@org/unresolved" }],
|
|
40
|
+
"exports": [],
|
|
41
|
+
"types": [],
|
|
42
|
+
"duplicates": []
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
"file": "src/Registration.tsx",
|
|
46
|
+
"owners": ["@org/owner"],
|
|
47
|
+
"dependencies": [],
|
|
48
|
+
"devDependencies": [],
|
|
49
|
+
"binaries": [],
|
|
50
|
+
"unresolved": [
|
|
51
|
+
{ "name": "./unresolved", "line": 8, "col": 23, "pos": 407 }
|
|
52
|
+
],
|
|
53
|
+
"exports": [{ "name": "unusedExport", "line": 1, "col": 14, "pos": 13 }],
|
|
54
|
+
"types": [
|
|
55
|
+
{ "name": "unusedEnum", "line": 3, "col": 13, "pos": 71 },
|
|
56
|
+
{ "name": "unusedType", "line": 8, "col": 14, "pos": 145 }
|
|
57
|
+
],
|
|
58
|
+
"enumMembers": {
|
|
59
|
+
"MyEnum": [
|
|
60
|
+
{ "name": "unusedMember", "line": 13, "col": 3, "pos": 167 },
|
|
61
|
+
{ "name": "unusedKey", "line": 15, "col": 3, "pos": 205 }
|
|
62
|
+
]
|
|
63
|
+
},
|
|
64
|
+
"classMembers": {
|
|
65
|
+
"MyClass": [
|
|
66
|
+
{ "name": "unusedMember", "line": 40, "col": 3, "pos": 687 },
|
|
67
|
+
{ "name": "unusedSetter", "line": 61, "col": 14, "pos": 1071 }
|
|
68
|
+
]
|
|
69
|
+
},
|
|
70
|
+
"duplicates": ["Registration", "default"]
|
|
71
|
+
}
|
|
72
|
+
]
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
The keys match the [reported issue types][6]. Example usage:
|
|
77
|
+
|
|
78
|
+
```sh
|
|
79
|
+
knip --reporter json
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Github Actions
|
|
83
|
+
|
|
84
|
+
TODO
|
|
85
|
+
|
|
86
|
+
Example usage:
|
|
87
|
+
|
|
88
|
+
```sh
|
|
89
|
+
knip --reporter github-actions
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Markdown
|
|
93
|
+
|
|
94
|
+
The built-in `markdown` reporter output is meant to be saved to a Markdown file.
|
|
95
|
+
This allows following the changes in issues over time. It reports issues in
|
|
96
|
+
Markdown tables separated by issue types as headings, for example:
|
|
97
|
+
|
|
98
|
+
```md
|
|
99
|
+
# Knip report
|
|
100
|
+
|
|
101
|
+
## Unused files (1)
|
|
102
|
+
|
|
103
|
+
- src/unused.ts
|
|
104
|
+
|
|
105
|
+
## Unlisted dependencies (2)
|
|
106
|
+
|
|
107
|
+
| Name | Location | Severity |
|
|
108
|
+
| :-------------- | :---------------- | :------- |
|
|
109
|
+
| unresolved | src/index.ts:8:23 | error |
|
|
110
|
+
| @org/unresolved | src/index.ts:9:23 | error |
|
|
111
|
+
|
|
112
|
+
## Unresolved imports (1)
|
|
113
|
+
|
|
114
|
+
| Name | Location | Severity |
|
|
115
|
+
| :----------- | :----------------- | :------- |
|
|
116
|
+
| ./unresolved | src/index.ts:10:12 | error |
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Disclosure
|
|
120
|
+
|
|
121
|
+
This reporter is useful for sharing large reports. Groups of issues are rendered
|
|
122
|
+
in a closed state initially. The reporter renders this:
|
|
123
|
+
|
|
124
|
+
````text
|
|
125
|
+
$ knip --reporter disclosure
|
|
126
|
+
|
|
127
|
+
<details>
|
|
128
|
+
<summary>Unused files (2)</summary>
|
|
129
|
+
|
|
130
|
+
```
|
|
131
|
+
unused.ts
|
|
132
|
+
dangling.js
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
</details>
|
|
136
|
+
|
|
137
|
+
<details>
|
|
138
|
+
<summary>Unused dependencies (2)</summary>
|
|
139
|
+
|
|
140
|
+
```
|
|
141
|
+
unused-dep package.json
|
|
142
|
+
my-package package.json
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
</details>
|
|
146
|
+
````
|
|
147
|
+
|
|
148
|
+
The above can be copy-pasted where HTML and Markdown is supported, such as a
|
|
149
|
+
GitHub issue or pull request, and renders like so:
|
|
150
|
+
|
|
151
|
+
<details>
|
|
152
|
+
<summary>Unused files (2)</summary>
|
|
153
|
+
|
|
154
|
+
```
|
|
155
|
+
unused.ts
|
|
156
|
+
dangling.js
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
</details>
|
|
160
|
+
|
|
161
|
+
<details>
|
|
162
|
+
<summary>Unused dependencies (2)</summary>
|
|
163
|
+
|
|
164
|
+
```
|
|
165
|
+
unused-dep package.json
|
|
166
|
+
my-package package.json
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
</details>
|
|
170
|
+
|
|
171
|
+
### CodeClimate
|
|
172
|
+
|
|
173
|
+
The built-in `codeclimate` reporter generates output in the Code Climate Report
|
|
174
|
+
JSON format. Example usage:
|
|
175
|
+
|
|
176
|
+
```text
|
|
177
|
+
$ knip --reporter codeclimate
|
|
178
|
+
|
|
179
|
+
[
|
|
180
|
+
{
|
|
181
|
+
"type": "issue",
|
|
182
|
+
"check_name": "Unused exports",
|
|
183
|
+
"description": "isUnused",
|
|
184
|
+
"categories": ["Bug Risk"],
|
|
185
|
+
"location": {
|
|
186
|
+
"path": "path/to/file.ts",
|
|
187
|
+
"positions": {
|
|
188
|
+
"begin": {
|
|
189
|
+
"line": 6,
|
|
190
|
+
"column": 1
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
"severity": "major",
|
|
195
|
+
"fingerprint": "e9789995c1fe9f7d75eed6a0c0f89e84",
|
|
196
|
+
}
|
|
197
|
+
]
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
## Custom Reporters
|
|
201
|
+
|
|
202
|
+
When the provided built-in reporters are not sufficient, a custom local reporter
|
|
203
|
+
can be implemented or an external reporter can be used. Multiple reporters can
|
|
204
|
+
be used at once by repeating the `--reporter` argument.
|
|
205
|
+
|
|
206
|
+
The results are passed to the function from its default export and can be used
|
|
207
|
+
to write issues to `stdout`, a JSON or CSV file, or sent to a service. It
|
|
208
|
+
supports a local JavaScript or TypeScript file or an external dependency.
|
|
209
|
+
|
|
210
|
+
### Local
|
|
211
|
+
|
|
212
|
+
The default export of the reporter should be a function with this interface:
|
|
213
|
+
|
|
214
|
+
```ts
|
|
215
|
+
type Reporter = async (options: ReporterOptions): void;
|
|
216
|
+
|
|
217
|
+
type ReporterOptions = {
|
|
218
|
+
report: Report;
|
|
219
|
+
issues: Issues;
|
|
220
|
+
counters: Counters;
|
|
221
|
+
configurationHints: ConfigurationHints;
|
|
222
|
+
isDisableConfigHints: boolean;
|
|
223
|
+
isTreatConfigHintsAsErrors: boolean;
|
|
224
|
+
cwd: string;
|
|
225
|
+
isProduction: boolean;
|
|
226
|
+
isShowProgress: boolean;
|
|
227
|
+
options: string;
|
|
228
|
+
};
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
The data can then be used to write issues to `stdout`, a JSON or CSV file, or
|
|
232
|
+
sent to a service.
|
|
233
|
+
|
|
234
|
+
Here's a most minimal reporter example:
|
|
235
|
+
|
|
236
|
+
```ts title="./my-reporter.ts"
|
|
237
|
+
import type { Reporter } from 'knip';
|
|
238
|
+
|
|
239
|
+
const reporter: Reporter = function (options) {
|
|
240
|
+
console.log(options.issues);
|
|
241
|
+
console.log(options.counters);
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
export default reporter;
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
Example usage:
|
|
248
|
+
|
|
249
|
+
```sh
|
|
250
|
+
knip --reporter ./my-reporter.ts
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### External
|
|
254
|
+
|
|
255
|
+
Pass `--reporter [pkg-name]` to use an external reporter. The default exported
|
|
256
|
+
function of the `main` script (default: `index.js`) will be invoked with the
|
|
257
|
+
`ReporterOptions`, just like a local reporter.
|
|
258
|
+
|
|
259
|
+
## Preprocessors
|
|
260
|
+
|
|
261
|
+
A preprocessor is a function that runs after the analysis is finished. It
|
|
262
|
+
receives the results from the analysis and should return data in the same
|
|
263
|
+
shape/structure (unless you pass it to only your own reporter).
|
|
264
|
+
|
|
265
|
+
The data goes through the preprocessors before the final data is passed to the
|
|
266
|
+
reporters. There are no built-in preprocessors. Just like reporters, use e.g.
|
|
267
|
+
`--preprocessor ./my-preprocessor` from the command line (can be repeated).
|
|
268
|
+
|
|
269
|
+
The default export of the preprocessor should be a function with this interface:
|
|
270
|
+
|
|
271
|
+
```ts
|
|
272
|
+
type Preprocessor = async (options: ReporterOptions) => ReporterOptions;
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
Like reporters, you can use local JavaScript or TypeScript files and external
|
|
276
|
+
npm packages as preprocessors.
|
|
277
|
+
|
|
278
|
+
Example preprocessor:
|
|
279
|
+
|
|
280
|
+
```ts title="./preprocess.ts"
|
|
281
|
+
import type { Preprocessor } from 'knip';
|
|
282
|
+
|
|
283
|
+
const preprocess: Preprocessor = function (options) {
|
|
284
|
+
// modify options.issues and options.counters
|
|
285
|
+
return options;
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
export default preprocess;
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
Example usage:
|
|
292
|
+
|
|
293
|
+
```sh
|
|
294
|
+
knip --preprocessor ./preprocess.ts
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
[1]: #disclosure
|
|
298
|
+
[2]: #github-actions
|
|
299
|
+
[3]: #json
|
|
300
|
+
[4]: #markdown
|
|
301
|
+
[5]: #codeclimate
|
|
302
|
+
[6]: ../reference/issue-types.md
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Rules & Filters
|
|
3
|
+
sidebar:
|
|
4
|
+
order: 5
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Use rules or filters to customize Knip's output. This has various use cases, a
|
|
8
|
+
few examples:
|
|
9
|
+
|
|
10
|
+
- Temporarily focus on a specific issue type.
|
|
11
|
+
- You don't want to see unused `type`, `interface` and `enum` exports reported.
|
|
12
|
+
- Specific issue types should be printed, but not counted against the total
|
|
13
|
+
error count.
|
|
14
|
+
|
|
15
|
+
If you're looking to handle one-off exceptions, also see [JSDoc tags][1].
|
|
16
|
+
|
|
17
|
+
## Filters
|
|
18
|
+
|
|
19
|
+
You can `--include` or `--exclude` any of the reported issue types to slice &
|
|
20
|
+
dice the report to your needs. Alternatively, they can be added to the
|
|
21
|
+
configuration (e.g. `"exclude": ["dependencies"]`).
|
|
22
|
+
|
|
23
|
+
Use `--include` to report only specific issue types. The following example
|
|
24
|
+
commands do the same:
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
knip --include files --include dependencies
|
|
28
|
+
knip --include files,dependencies
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Or the other way around, use `--exclude` to ignore the types you're not
|
|
32
|
+
interested in:
|
|
33
|
+
|
|
34
|
+
```sh
|
|
35
|
+
knip --include files --exclude enumMembers,duplicates
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Also see the [list of issue types][2].
|
|
39
|
+
|
|
40
|
+
### Shorthands
|
|
41
|
+
|
|
42
|
+
Knip has shortcuts to include only specific issue types.
|
|
43
|
+
|
|
44
|
+
1. The `--dependencies` flag includes:
|
|
45
|
+
- `dependencies` (and `devDependencies` + `optionalPeerDependencies`)
|
|
46
|
+
- `unlisted`
|
|
47
|
+
- `binaries`
|
|
48
|
+
- `unresolved`
|
|
49
|
+
- `catalog`
|
|
50
|
+
|
|
51
|
+
2. The `--exports` flag includes:
|
|
52
|
+
- `exports`
|
|
53
|
+
- `types`
|
|
54
|
+
- `enumMembers`
|
|
55
|
+
- `duplicates`
|
|
56
|
+
|
|
57
|
+
3. The `--files` flag is a shortcut for `--include files`
|
|
58
|
+
|
|
59
|
+
## Rules
|
|
60
|
+
|
|
61
|
+
Use `rules` in the configuration to customize the issue types that count towards
|
|
62
|
+
the total error count, or to exclude them altogether.
|
|
63
|
+
|
|
64
|
+
| Value | Default | Printed | Counted | Description |
|
|
65
|
+
| :-------- | :-----: | :-----: | :-----: | :-------------------------------- |
|
|
66
|
+
| `"error"` | ✓ | ✓ | ✓ | Similar to the `--include` filter |
|
|
67
|
+
| `"warn"` | - | ✓ | - | Printed in faded/gray color |
|
|
68
|
+
| `"off"` | - | - | - | Similar to the `--exclude` filter |
|
|
69
|
+
|
|
70
|
+
Example:
|
|
71
|
+
|
|
72
|
+
```json title="knip.json"
|
|
73
|
+
{
|
|
74
|
+
"rules": {
|
|
75
|
+
"files": "warn",
|
|
76
|
+
"classMembers": "off",
|
|
77
|
+
"duplicates": "off"
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Also see the [issue types overview][2].
|
|
83
|
+
|
|
84
|
+
NOTE: If the `dependencies` issue type is included, the `devDependencies` and
|
|
85
|
+
`optionalPeerDependencies` types can still be set to `"warn"` separately.
|
|
86
|
+
|
|
87
|
+
The rules are modeled after the ESLint `rules` configuration, and could be
|
|
88
|
+
extended in the future.
|
|
89
|
+
|
|
90
|
+
## Rules or filters?
|
|
91
|
+
|
|
92
|
+
Filters are meant to be used as command-line flags, rules allow for more
|
|
93
|
+
fine-grained configuration.
|
|
94
|
+
|
|
95
|
+
- Rules are more fine-grained since they also have "warn".
|
|
96
|
+
- Rules could be extended in the future.
|
|
97
|
+
- Filters can be set in configuration and from CLI (rules only in
|
|
98
|
+
configuration).
|
|
99
|
+
- Filters have shorthands (rules don't have this).
|
|
100
|
+
|
|
101
|
+
[1]: ../reference/jsdoc-tsdoc-tags.md
|
|
102
|
+
[2]: ../reference/issue-types.md
|