@knip/mcp 0.0.1 → 0.0.2
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/docs/blog/brief-history.md +1 -1
- package/docs/blog/knip-v3.mdx +1 -1
- package/docs/blog/knip-v4.mdx +1 -1
- package/docs/blog/knip-v5.mdx +1 -1
- package/docs/blog/release-notes-v2.md +1 -1
- package/docs/blog/slim-down-to-speed-up.md +1 -1
- package/docs/blog/state-of-knip.md +1 -1
- package/docs/blog/two-years.mdx +1 -1
- package/docs/index.mdx +4 -0
- package/package.json +2 -2
- package/docs/docs/blog/brief-history.md +0 -30
- package/docs/docs/blog/knip-v3.mdx +0 -88
- package/docs/docs/blog/knip-v4.mdx +0 -149
- package/docs/docs/blog/knip-v5.mdx +0 -190
- package/docs/docs/blog/migration-to-v1.md +0 -65
- package/docs/docs/blog/release-notes-v2.md +0 -46
- package/docs/docs/blog/slim-down-to-speed-up.md +0 -269
- package/docs/docs/blog/state-of-knip.md +0 -191
- package/docs/docs/blog/two-years.mdx +0 -107
- package/docs/docs/explanations/comparison-and-migration.md +0 -129
- package/docs/docs/explanations/entry-files.md +0 -70
- package/docs/docs/explanations/plugins.md +0 -318
- package/docs/docs/explanations/why-use-knip.md +0 -128
- package/docs/docs/features/auto-fix.mdx +0 -333
- package/docs/docs/features/compilers.md +0 -172
- package/docs/docs/features/integrated-monorepos.md +0 -52
- package/docs/docs/features/monorepos-and-workspaces.md +0 -134
- package/docs/docs/features/production-mode.md +0 -95
- package/docs/docs/features/reporters.md +0 -302
- package/docs/docs/features/rules-and-filters.md +0 -102
- package/docs/docs/features/script-parser.md +0 -156
- package/docs/docs/features/source-mapping.md +0 -100
- package/docs/docs/guides/configuring-project-files.md +0 -205
- package/docs/docs/guides/contributing.md +0 -24
- package/docs/docs/guides/handling-issues.mdx +0 -646
- package/docs/docs/guides/issue-reproduction.md +0 -94
- package/docs/docs/guides/namespace-imports.md +0 -125
- package/docs/docs/guides/performance.md +0 -97
- package/docs/docs/guides/troubleshooting.md +0 -127
- package/docs/docs/guides/using-knip-in-ci.md +0 -54
- package/docs/docs/guides/working-with-commonjs.md +0 -72
- package/docs/docs/index.mdx +0 -160
- package/docs/docs/overview/configuration.md +0 -104
- package/docs/docs/overview/features.md +0 -66
- package/docs/docs/overview/getting-started.mdx +0 -195
- package/docs/docs/overview/screenshots-videos.md +0 -42
- package/docs/docs/playground.mdx +0 -38
- package/docs/docs/reference/cli.md +0 -481
- package/docs/docs/reference/configuration.md +0 -413
- package/docs/docs/reference/dynamic-configuration.mdx +0 -72
- package/docs/docs/reference/faq.md +0 -441
- package/docs/docs/reference/issue-types.md +0 -43
- package/docs/docs/reference/jsdoc-tsdoc-tags.md +0 -122
- package/docs/docs/reference/known-issues.md +0 -64
- package/docs/docs/reference/plugins/.gitkeep +0 -0
- package/docs/docs/reference/plugins.md +0 -238
- package/docs/docs/reference/related-tooling.md +0 -46
- package/docs/docs/sponsors.mdx +0 -65
- package/docs/docs/typescript/unused-dependencies.md +0 -86
- package/docs/docs/typescript/unused-exports.md +0 -87
- package/docs/docs/writing-a-plugin/argument-parsing.md +0 -202
- package/docs/docs/writing-a-plugin/index.md +0 -376
- package/docs/docs/writing-a-plugin/inputs.md +0 -162
- /package/docs/{docs/blog → blog}/for-editors-and-agents.md +0 -0
|
@@ -1,95 +0,0 @@
|
|
|
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
|
|
@@ -1,302 +0,0 @@
|
|
|
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
|
|
@@ -1,102 +0,0 @@
|
|
|
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
|
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: Script Parser
|
|
3
|
-
---
|
|
4
|
-
|
|
5
|
-
Knip parses shell commands and scripts to find additional dependencies, entry
|
|
6
|
-
files and configuration files in various places:
|
|
7
|
-
|
|
8
|
-
- In [`package.json`][1]
|
|
9
|
-
- In [CLI arguments][2]
|
|
10
|
-
- In [scripts][3]
|
|
11
|
-
- In [source code][4]
|
|
12
|
-
|
|
13
|
-
Shell scripts can be read and statically analyzed, but they're not executed.
|
|
14
|
-
|
|
15
|
-
## package.json
|
|
16
|
-
|
|
17
|
-
The `main`, `bin`, `exports` and `scripts` fields may contain entry files. Let's
|
|
18
|
-
take a look at this example:
|
|
19
|
-
|
|
20
|
-
```json title="package.json"
|
|
21
|
-
{
|
|
22
|
-
"name": "my-package",
|
|
23
|
-
"main": "index.js",
|
|
24
|
-
"exports": {
|
|
25
|
-
"./lib": {
|
|
26
|
-
"import": "./dist/index.mjs",
|
|
27
|
-
"require": "./dist/index.cjs"
|
|
28
|
-
}
|
|
29
|
-
},
|
|
30
|
-
"bin": {
|
|
31
|
-
"program": "bin/cli.js"
|
|
32
|
-
},
|
|
33
|
-
"scripts": {
|
|
34
|
-
"build": "rollup src/entry.ts",
|
|
35
|
-
"start": "node --loader tsx server.ts"
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
From this example, Knip automatically adds the following files as entry files:
|
|
41
|
-
|
|
42
|
-
- `index.js`
|
|
43
|
-
- `./dist/index.mjs`
|
|
44
|
-
- `./dist/index.cjs`
|
|
45
|
-
- `bin/cli.js`
|
|
46
|
-
- `src/entry.ts`
|
|
47
|
-
- `server.ts`
|
|
48
|
-
|
|
49
|
-
### Excluded files
|
|
50
|
-
|
|
51
|
-
Knip would not add the `exports` if the `dist` folder is matching a pattern in a
|
|
52
|
-
relevant `.gitignore` file or `ignore` option.
|
|
53
|
-
|
|
54
|
-
Knip does not add scripts without a standard extension. For instance, the
|
|
55
|
-
`bin/tool` file might be a valid executable for Node.js, but wouldn't be added
|
|
56
|
-
or parsed by Knip.
|
|
57
|
-
|
|
58
|
-
### CLI Arguments
|
|
59
|
-
|
|
60
|
-
When parsing the `scripts` of `package.json` and other files, Knip detects
|
|
61
|
-
various types of inputs. Some examples:
|
|
62
|
-
|
|
63
|
-
- The first positional argument is usually an entry file
|
|
64
|
-
- Configuration files are often in the `-c` or `--config` argument
|
|
65
|
-
- The `--require`, `--loader` or `--import` arguments are often dependencies
|
|
66
|
-
|
|
67
|
-
```json
|
|
68
|
-
{
|
|
69
|
-
"name": "my-lib",
|
|
70
|
-
"scripts": {
|
|
71
|
-
"start": "node --import tsx/esm run.ts",
|
|
72
|
-
"bundle": "tsup -c tsup.lib.config.ts",
|
|
73
|
-
"type-check": "tsc -p tsconfig.app.json"
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
The `"start"` script will have `tsx` marked as a referenced dependency, and adds
|
|
79
|
-
`run.ts` as an entry file.
|
|
80
|
-
|
|
81
|
-
Additionally, the following files are detected as configuration files:
|
|
82
|
-
|
|
83
|
-
- `tsup.lib.config.ts` - to be handled by the tsup plugin
|
|
84
|
-
- `tsconfig.app.json` - to be handled by the TypeScript plugin
|
|
85
|
-
|
|
86
|
-
Such executables and their arguments are all defined in plugins separately for
|
|
87
|
-
fine-grained results.
|
|
88
|
-
|
|
89
|
-
## Scripts
|
|
90
|
-
|
|
91
|
-
Plugins may also use the script parser to extract entry files and dependencies
|
|
92
|
-
from commands. A few examples:
|
|
93
|
-
|
|
94
|
-
- GitHub Actions: workflow files may contain `run` commands (e.g.
|
|
95
|
-
`.github/workflows/ci.yml`)
|
|
96
|
-
- Husky & Lefthook: Git hooks such as `.git/hooks/pre-push` contain scripts;
|
|
97
|
-
also `lefthook.yml` has `run` commands
|
|
98
|
-
- Lint Staged: configuration values are all commands
|
|
99
|
-
- Nx: task executors and `nx:run-commands` executors in `project.json` contains
|
|
100
|
-
scripts
|
|
101
|
-
- Release It: `hooks` contain commands
|
|
102
|
-
|
|
103
|
-
Plugins can also return configuration files. Some examples:
|
|
104
|
-
|
|
105
|
-
- The Angular plugin detects `options.tsConfig` as a TypeScript config file
|
|
106
|
-
- The GitHub Actions plugin parses `run` commands which may contain
|
|
107
|
-
configuration file paths
|
|
108
|
-
|
|
109
|
-
## Source Code
|
|
110
|
-
|
|
111
|
-
When Knip is walking the abstract syntax trees (ASTs) of JavaScript and
|
|
112
|
-
TypeScript source code files, it looks for imports and exports. But there's a
|
|
113
|
-
few more (rather obscure) things that Knip detects in the process. Below are
|
|
114
|
-
examples of additional scripts Knip parses to find entry files and dependencies.
|
|
115
|
-
|
|
116
|
-
### bun
|
|
117
|
-
|
|
118
|
-
If the `bun` dependency is imported in source code, Knip considers the contents
|
|
119
|
-
of `$` template tags to be scripts:
|
|
120
|
-
|
|
121
|
-
```ts
|
|
122
|
-
import { $ } from 'bun';
|
|
123
|
-
await $`bun boxen I ❤ unicorns`;
|
|
124
|
-
await $`boxen I ❤ unicorns`;
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
Parsing the script results in the `boxen` binary (the `boxen-cli` dependency) as
|
|
128
|
-
referenced (twice).
|
|
129
|
-
|
|
130
|
-
### execa
|
|
131
|
-
|
|
132
|
-
If the `execa` dependency is imported in source code, Knip considers the
|
|
133
|
-
contents of `$` template tags to be scripts:
|
|
134
|
-
|
|
135
|
-
```ts
|
|
136
|
-
await $({ stdio: 'inherit' })`c8 node hydrate.js`;
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
Parsing the script results in `hydrate.js` added as an entry file and the `c8`
|
|
140
|
-
binary/dependency as referenced.
|
|
141
|
-
|
|
142
|
-
### zx
|
|
143
|
-
|
|
144
|
-
If the `zx` dependency is imported in source code, Knip considers the contents
|
|
145
|
-
of `$` template tags to be scripts:
|
|
146
|
-
|
|
147
|
-
```ts
|
|
148
|
-
await $`node scripts/parse.js`;
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
This will add `scripts/parse.js` as an entry file.
|
|
152
|
-
|
|
153
|
-
[1]: #packagejson
|
|
154
|
-
[2]: #cli-arguments
|
|
155
|
-
[3]: #scripts
|
|
156
|
-
[4]: #source-code
|