@knip/mcp 0.0.20 → 0.0.22
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/package.json +2 -2
- package/src/docs/docs/blog/brief-history.md +29 -0
- package/src/docs/docs/blog/for-editors-and-agents.md +130 -0
- package/src/docs/docs/blog/knip-v3.mdx +88 -0
- package/src/docs/docs/blog/knip-v4.mdx +149 -0
- package/src/docs/docs/blog/knip-v5.mdx +190 -0
- package/src/docs/docs/blog/migration-to-v1.md +65 -0
- package/src/docs/docs/blog/release-notes-v2.md +46 -0
- package/src/docs/docs/blog/slim-down-to-speed-up.md +269 -0
- package/src/docs/docs/blog/state-of-knip.md +191 -0
- package/src/docs/docs/blog/two-years.mdx +107 -0
- package/src/docs/docs/explanations/comparison-and-migration.md +133 -0
- package/src/docs/docs/explanations/entry-files.md +70 -0
- package/src/docs/docs/explanations/plugins.md +347 -0
- package/src/docs/docs/explanations/why-use-knip.md +128 -0
- package/src/docs/docs/features/auto-fix.mdx +348 -0
- package/src/docs/docs/features/compilers.md +172 -0
- package/src/docs/docs/features/integrated-monorepos.md +61 -0
- package/src/docs/docs/features/monorepos-and-workspaces.md +149 -0
- package/src/docs/docs/features/production-mode.md +95 -0
- package/src/docs/docs/features/reporters.md +304 -0
- package/src/docs/docs/features/rules-and-filters.md +102 -0
- package/src/docs/docs/features/script-parser.md +156 -0
- package/src/docs/docs/features/source-mapping.md +100 -0
- package/src/docs/docs/guides/configuring-project-files.md +205 -0
- package/src/docs/docs/guides/contributing.md +24 -0
- package/src/docs/docs/guides/handling-issues.mdx +708 -0
- package/src/docs/docs/guides/issue-reproduction.md +99 -0
- package/src/docs/docs/guides/namespace-imports.md +125 -0
- package/src/docs/docs/guides/performance.md +108 -0
- package/src/docs/docs/guides/troubleshooting.md +133 -0
- package/src/docs/docs/guides/using-knip-in-ci.md +50 -0
- package/src/docs/docs/guides/working-with-commonjs.md +72 -0
- package/src/docs/docs/index.mdx +160 -0
- package/src/docs/docs/overview/configuration.md +104 -0
- package/src/docs/docs/overview/features.md +66 -0
- package/src/docs/docs/overview/getting-started.mdx +205 -0
- package/src/docs/docs/overview/screenshots-videos.md +42 -0
- package/src/docs/docs/playground.mdx +38 -0
- package/src/docs/docs/reference/cli.md +511 -0
- package/src/docs/docs/reference/configuration-hints.md +146 -0
- package/src/docs/docs/reference/configuration.md +430 -0
- package/src/docs/docs/reference/dynamic-configuration.mdx +72 -0
- package/src/docs/docs/reference/faq.md +493 -0
- package/src/docs/docs/reference/integrations.md +104 -0
- package/src/docs/docs/reference/issue-types.md +45 -0
- package/src/docs/docs/reference/jsdoc-tsdoc-tags.md +133 -0
- package/src/docs/docs/reference/known-issues.md +86 -0
- package/src/docs/docs/reference/plugins/.gitkeep +0 -0
- package/src/docs/docs/reference/plugins/angular.md +34 -0
- package/src/docs/docs/reference/plugins/astro-db.md +34 -0
- package/src/docs/docs/reference/plugins/astro-og-canvas.md +15 -0
- package/src/docs/docs/reference/plugins/astro.md +44 -0
- package/src/docs/docs/reference/plugins/ava.md +46 -0
- package/src/docs/docs/reference/plugins/babel.md +41 -0
- package/src/docs/docs/reference/plugins/biome.md +34 -0
- package/src/docs/docs/reference/plugins/bumpp.md +41 -0
- package/src/docs/docs/reference/plugins/bun.md +34 -0
- package/src/docs/docs/reference/plugins/c8.md +29 -0
- package/src/docs/docs/reference/plugins/capacitor.md +36 -0
- package/src/docs/docs/reference/plugins/changelogen.md +41 -0
- package/src/docs/docs/reference/plugins/changelogithub.md +41 -0
- package/src/docs/docs/reference/plugins/changesets.md +34 -0
- package/src/docs/docs/reference/plugins/commitizen.md +34 -0
- package/src/docs/docs/reference/plugins/commitlint.md +42 -0
- package/src/docs/docs/reference/plugins/convex.md +34 -0
- package/src/docs/docs/reference/plugins/create-typescript-app.md +34 -0
- package/src/docs/docs/reference/plugins/cspell.md +39 -0
- package/src/docs/docs/reference/plugins/cucumber.md +35 -0
- package/src/docs/docs/reference/plugins/cypress.md +41 -0
- package/src/docs/docs/reference/plugins/danger.md +34 -0
- package/src/docs/docs/reference/plugins/dependency-cruiser.md +46 -0
- package/src/docs/docs/reference/plugins/docusaurus.md +40 -0
- package/src/docs/docs/reference/plugins/dotenv.md +32 -0
- package/src/docs/docs/reference/plugins/drizzle.md +34 -0
- package/src/docs/docs/reference/plugins/eleventy.md +35 -0
- package/src/docs/docs/reference/plugins/eslint.md +86 -0
- package/src/docs/docs/reference/plugins/execa.md +15 -0
- package/src/docs/docs/reference/plugins/expo.md +35 -0
- package/src/docs/docs/reference/plugins/expressive-code.md +40 -0
- package/src/docs/docs/reference/plugins/gatsby.md +49 -0
- package/src/docs/docs/reference/plugins/github-action.md +34 -0
- package/src/docs/docs/reference/plugins/github-actions.md +34 -0
- package/src/docs/docs/reference/plugins/glob.md +29 -0
- package/src/docs/docs/reference/plugins/graphql-codegen.md +46 -0
- package/src/docs/docs/reference/plugins/hardhat.md +34 -0
- package/src/docs/docs/reference/plugins/husky.md +40 -0
- package/src/docs/docs/reference/plugins/i18next-parser.md +46 -0
- package/src/docs/docs/reference/plugins/jest.md +51 -0
- package/src/docs/docs/reference/plugins/karma.md +39 -0
- package/src/docs/docs/reference/plugins/knex.md +34 -0
- package/src/docs/docs/reference/plugins/ladle.md +39 -0
- package/src/docs/docs/reference/plugins/lefthook.md +46 -0
- package/src/docs/docs/reference/plugins/lint-staged.md +48 -0
- package/src/docs/docs/reference/plugins/linthtml.md +41 -0
- package/src/docs/docs/reference/plugins/lockfile-lint.md +41 -0
- package/src/docs/docs/reference/plugins/lost-pixel.md +34 -0
- package/src/docs/docs/reference/plugins/markdownlint.md +34 -0
- package/src/docs/docs/reference/plugins/mdx.md +38 -0
- package/src/docs/docs/reference/plugins/mdxlint.md +38 -0
- package/src/docs/docs/reference/plugins/metro.md +46 -0
- package/src/docs/docs/reference/plugins/mocha.md +47 -0
- package/src/docs/docs/reference/plugins/moonrepo.md +34 -0
- package/src/docs/docs/reference/plugins/msw.md +35 -0
- package/src/docs/docs/reference/plugins/nano-staged.md +39 -0
- package/src/docs/docs/reference/plugins/nest.md +36 -0
- package/src/docs/docs/reference/plugins/netlify.md +41 -0
- package/src/docs/docs/reference/plugins/next-intl.md +34 -0
- package/src/docs/docs/reference/plugins/next-mdx.md +35 -0
- package/src/docs/docs/reference/plugins/next.md +52 -0
- package/src/docs/docs/reference/plugins/nitro.md +52 -0
- package/src/docs/docs/reference/plugins/node-modules-inspector.md +49 -0
- package/src/docs/docs/reference/plugins/node.md +50 -0
- package/src/docs/docs/reference/plugins/nodemon.md +30 -0
- package/src/docs/docs/reference/plugins/npm-package-json-lint.md +41 -0
- package/src/docs/docs/reference/plugins/nuxt.md +55 -0
- package/src/docs/docs/reference/plugins/nx.md +62 -0
- package/src/docs/docs/reference/plugins/nyc.md +39 -0
- package/src/docs/docs/reference/plugins/oclif.md +34 -0
- package/src/docs/docs/reference/plugins/openapi-ts.md +41 -0
- package/src/docs/docs/reference/plugins/oxfmt.md +46 -0
- package/src/docs/docs/reference/plugins/oxlint.md +46 -0
- package/src/docs/docs/reference/plugins/parcel.md +38 -0
- package/src/docs/docs/reference/plugins/payload.md +40 -0
- package/src/docs/docs/reference/plugins/playwright-ct.md +40 -0
- package/src/docs/docs/reference/plugins/playwright-test.md +29 -0
- package/src/docs/docs/reference/plugins/playwright.md +49 -0
- package/src/docs/docs/reference/plugins/plop.md +34 -0
- package/src/docs/docs/reference/plugins/pm2.md +37 -0
- package/src/docs/docs/reference/plugins/pnpm.md +34 -0
- package/src/docs/docs/reference/plugins/postcss.md +46 -0
- package/src/docs/docs/reference/plugins/preconstruct.md +34 -0
- package/src/docs/docs/reference/plugins/prettier.md +39 -0
- package/src/docs/docs/reference/plugins/prisma.md +62 -0
- package/src/docs/docs/reference/plugins/qwik.md +40 -0
- package/src/docs/docs/reference/plugins/raycast.md +34 -0
- package/src/docs/docs/reference/plugins/react-cosmos.md +40 -0
- package/src/docs/docs/reference/plugins/react-native.md +34 -0
- package/src/docs/docs/reference/plugins/react-router.md +37 -0
- package/src/docs/docs/reference/plugins/relay.md +53 -0
- package/src/docs/docs/reference/plugins/release-it.md +34 -0
- package/src/docs/docs/reference/plugins/remark.md +40 -0
- package/src/docs/docs/reference/plugins/remix.md +43 -0
- package/src/docs/docs/reference/plugins/rollup.md +49 -0
- package/src/docs/docs/reference/plugins/rsbuild.md +34 -0
- package/src/docs/docs/reference/plugins/rslib.md +34 -0
- package/src/docs/docs/reference/plugins/rspack.md +34 -0
- package/src/docs/docs/reference/plugins/rstest.md +34 -0
- package/src/docs/docs/reference/plugins/sanity.md +38 -0
- package/src/docs/docs/reference/plugins/semantic-release.md +41 -0
- package/src/docs/docs/reference/plugins/sentry.md +36 -0
- package/src/docs/docs/reference/plugins/simple-git-hooks.md +38 -0
- package/src/docs/docs/reference/plugins/size-limit.md +38 -0
- package/src/docs/docs/reference/plugins/sst.md +34 -0
- package/src/docs/docs/reference/plugins/starlight.md +34 -0
- package/src/docs/docs/reference/plugins/storybook.md +43 -0
- package/src/docs/docs/reference/plugins/stryker.md +34 -0
- package/src/docs/docs/reference/plugins/stylelint.md +41 -0
- package/src/docs/docs/reference/plugins/svelte.md +34 -0
- package/src/docs/docs/reference/plugins/sveltekit.md +34 -0
- package/src/docs/docs/reference/plugins/svgo.md +38 -0
- package/src/docs/docs/reference/plugins/svgr.md +43 -0
- package/src/docs/docs/reference/plugins/swc.md +34 -0
- package/src/docs/docs/reference/plugins/syncpack.md +41 -0
- package/src/docs/docs/reference/plugins/tailwind.md +34 -0
- package/src/docs/docs/reference/plugins/tanstack-router.md +47 -0
- package/src/docs/docs/reference/plugins/taskfile.md +43 -0
- package/src/docs/docs/reference/plugins/travis.md +34 -0
- package/src/docs/docs/reference/plugins/ts-node.md +30 -0
- package/src/docs/docs/reference/plugins/tsdown.md +46 -0
- package/src/docs/docs/reference/plugins/tsup.md +46 -0
- package/src/docs/docs/reference/plugins/tsx.md +48 -0
- package/src/docs/docs/reference/plugins/typedoc.md +53 -0
- package/src/docs/docs/reference/plugins/typescript.md +57 -0
- package/src/docs/docs/reference/plugins/unbuild.md +34 -0
- package/src/docs/docs/reference/plugins/unocss.md +39 -0
- package/src/docs/docs/reference/plugins/vercel-og.md +41 -0
- package/src/docs/docs/reference/plugins/vike.md +37 -0
- package/src/docs/docs/reference/plugins/vite.md +50 -0
- package/src/docs/docs/reference/plugins/vitepress.md +37 -0
- package/src/docs/docs/reference/plugins/vitest.md +72 -0
- package/src/docs/docs/reference/plugins/vue.md +34 -0
- package/src/docs/docs/reference/plugins/webdriver-io.md +34 -0
- package/src/docs/docs/reference/plugins/webpack.md +55 -0
- package/src/docs/docs/reference/plugins/wireit.md +34 -0
- package/src/docs/docs/reference/plugins/wrangler.md +34 -0
- package/src/docs/docs/reference/plugins/xo.md +40 -0
- package/src/docs/docs/reference/plugins/yarn.md +35 -0
- package/src/docs/docs/reference/plugins/yorkie.md +34 -0
- package/src/docs/docs/reference/plugins/zx.md +15 -0
- package/src/docs/docs/reference/plugins.md +146 -0
- package/src/docs/docs/reference/related-tooling.md +46 -0
- package/src/docs/docs/sponsors.mdx +64 -0
- package/src/docs/docs/typescript/unused-dependencies.md +86 -0
- package/src/docs/docs/typescript/unused-exports.md +87 -0
- package/src/docs/docs/writing-a-plugin/argument-parsing.md +201 -0
- package/src/docs/docs/writing-a-plugin/index.md +391 -0
- package/src/docs/docs/writing-a-plugin/inputs.md +162 -0
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Auto-fix
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
import { Tabs, TabItem } from '@astrojs/starlight/components';
|
|
6
|
+
import { Badge } from '@astrojs/starlight/components';
|
|
7
|
+
|
|
8
|
+
Run Knip as you normally would, and if the report looks good then run it again
|
|
9
|
+
with the `--fix` flag to let Knip automatically apply fixes. It fixes the
|
|
10
|
+
following [issue types][1]:
|
|
11
|
+
|
|
12
|
+
- Remove `export` keyword for unused exports, re-exports, and exported types
|
|
13
|
+
- Remove `export default` keywords for unused default exports
|
|
14
|
+
- Remove unused enum members
|
|
15
|
+
- Remove unused class members (disabled by default)
|
|
16
|
+
- Remove unused `dependencies` and `devDependencies` from `package.json`
|
|
17
|
+
- Remove unused files
|
|
18
|
+
- Remove unused catalog entries
|
|
19
|
+
|
|
20
|
+
:::caution
|
|
21
|
+
|
|
22
|
+
Use a VCS (version control system) like Git to review and undo changes as
|
|
23
|
+
necessary.
|
|
24
|
+
|
|
25
|
+
:::
|
|
26
|
+
|
|
27
|
+
## Flags
|
|
28
|
+
|
|
29
|
+
### Fix
|
|
30
|
+
|
|
31
|
+
Add the `--fix` flag to remove unused exports and dependencies:
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
knip --fix
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Add `--allow-remove-files` to allow Knip to remove unused files:
|
|
38
|
+
|
|
39
|
+
```sh
|
|
40
|
+
knip --fix --allow-remove-files
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Use `--fix-type` to fix only specific issue types:
|
|
44
|
+
|
|
45
|
+
- `dependencies`
|
|
46
|
+
- `exports`
|
|
47
|
+
- `types`
|
|
48
|
+
- `files`
|
|
49
|
+
- `catalog`
|
|
50
|
+
|
|
51
|
+
Example:
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
knip --fix-type exports,types
|
|
55
|
+
knip --fix-type exports --fix-type types # same as above
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Format
|
|
59
|
+
|
|
60
|
+
Add `--format` to format the modified files using the formatter and
|
|
61
|
+
configuration in your project. Supports Biome, deno fmt, dprint and Prettier
|
|
62
|
+
(using [Formatly][2]):
|
|
63
|
+
|
|
64
|
+
```sh
|
|
65
|
+
knip --fix --format
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Demo
|
|
69
|
+
|
|
70
|
+
<video controls width="500">
|
|
71
|
+
<source src="/screenshots/fix.mp4" type="video/mp4" />
|
|
72
|
+
|
|
73
|
+
<source src="/screenshots/fix.webm" type="video/webm" />
|
|
74
|
+
</video>
|
|
75
|
+
|
|
76
|
+
## Post-fix
|
|
77
|
+
|
|
78
|
+
After Knip has fixed issues, there are four things to consider:
|
|
79
|
+
|
|
80
|
+
### 1. Use a formatter
|
|
81
|
+
|
|
82
|
+
Use a tool like Prettier or Biome if the code needs formatting. Knip removes the
|
|
83
|
+
minimum amount of code while leaving it in a working state.
|
|
84
|
+
|
|
85
|
+
:::tip
|
|
86
|
+
|
|
87
|
+
Add the `--format` flag to format the modified files using the formatter and
|
|
88
|
+
configuration in your project.
|
|
89
|
+
|
|
90
|
+
:::
|
|
91
|
+
|
|
92
|
+
### 2. Unused variables
|
|
93
|
+
|
|
94
|
+
Use a tool like ESLint or Biome to find and remove unused imports and variables
|
|
95
|
+
inside files. Even better, try [remove-unused-vars][3] to remove unused
|
|
96
|
+
variables within files.
|
|
97
|
+
|
|
98
|
+
This may result in more deleted code, and Knip may then find more unused code.
|
|
99
|
+
Rinse and repeat!
|
|
100
|
+
|
|
101
|
+
### 3. Unused dependencies
|
|
102
|
+
|
|
103
|
+
Verify changes in `package.json` and update dependencies using your package
|
|
104
|
+
manager.
|
|
105
|
+
|
|
106
|
+
<Tabs syncKey="pm">
|
|
107
|
+
<TabItem label="npm">
|
|
108
|
+
```shell
|
|
109
|
+
npm install
|
|
110
|
+
```
|
|
111
|
+
</TabItem>
|
|
112
|
+
|
|
113
|
+
<TabItem label="pnpm">
|
|
114
|
+
```shell
|
|
115
|
+
pnpm install
|
|
116
|
+
```
|
|
117
|
+
</TabItem>
|
|
118
|
+
|
|
119
|
+
<TabItem label="bun">
|
|
120
|
+
```shell
|
|
121
|
+
bun install
|
|
122
|
+
```
|
|
123
|
+
</TabItem>
|
|
124
|
+
|
|
125
|
+
<TabItem label="yarn">
|
|
126
|
+
```shell
|
|
127
|
+
yarn
|
|
128
|
+
```
|
|
129
|
+
</TabItem>
|
|
130
|
+
</Tabs>
|
|
131
|
+
|
|
132
|
+
### 4. Install unlisted dependencies
|
|
133
|
+
|
|
134
|
+
If Knip reports unlisted dependencies or binaries, they should be installed
|
|
135
|
+
using the package manager in the project, for example:
|
|
136
|
+
|
|
137
|
+
<Tabs syncKey="pm">
|
|
138
|
+
<TabItem label="npm">
|
|
139
|
+
```shell
|
|
140
|
+
npm install unlisted-package
|
|
141
|
+
```
|
|
142
|
+
</TabItem>
|
|
143
|
+
|
|
144
|
+
<TabItem label="pnpm">
|
|
145
|
+
```shell
|
|
146
|
+
pnpm add unlisted-package
|
|
147
|
+
```
|
|
148
|
+
</TabItem>
|
|
149
|
+
|
|
150
|
+
<TabItem label="bun">
|
|
151
|
+
```shell
|
|
152
|
+
bun add unlisted-package
|
|
153
|
+
```
|
|
154
|
+
</TabItem>
|
|
155
|
+
|
|
156
|
+
<TabItem label="yarn">
|
|
157
|
+
```shell
|
|
158
|
+
yarn add unlisted-package
|
|
159
|
+
```
|
|
160
|
+
</TabItem>
|
|
161
|
+
</Tabs>
|
|
162
|
+
|
|
163
|
+
## Example results
|
|
164
|
+
|
|
165
|
+
### Exports
|
|
166
|
+
|
|
167
|
+
The `export` keyword for unused exports is removed:
|
|
168
|
+
|
|
169
|
+
```diff title="module.ts"
|
|
170
|
+
-export const unused = 1;
|
|
171
|
+
-export default class MyClass {}
|
|
172
|
+
+const unused = 1;
|
|
173
|
+
+class MyClass {}
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
The `default` keyword was also removed here.
|
|
177
|
+
|
|
178
|
+
Knip removes the whole or part of export declarations:
|
|
179
|
+
|
|
180
|
+
```diff title="module.ts"
|
|
181
|
+
type Snake = 'python' | 'anaconda';
|
|
182
|
+
const Owl = 'Hedwig';
|
|
183
|
+
const Hawk = 'Tony';
|
|
184
|
+
-export type { Snake };
|
|
185
|
+
-export { Owl, Hawk };
|
|
186
|
+
+;
|
|
187
|
+
+;
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### Re-exports
|
|
191
|
+
|
|
192
|
+
Knip removes the whole or part of re-exports:
|
|
193
|
+
|
|
194
|
+
```diff title="file.js"
|
|
195
|
+
-export { Cat, Dog } from './pets';
|
|
196
|
+
-export { Lion, Elephant } from './jungle';
|
|
197
|
+
+export { Elephant } from './jungle'
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
Also across any chain of re-exports:
|
|
201
|
+
|
|
202
|
+
<Tabs>
|
|
203
|
+
<TabItem label="module.ts">
|
|
204
|
+
```diff
|
|
205
|
+
export const Hawk = 'Tony';
|
|
206
|
+
-export const Owl = 'Hedwig';
|
|
207
|
+
+const Owl = 'Hedwig';
|
|
208
|
+
```
|
|
209
|
+
</TabItem>
|
|
210
|
+
|
|
211
|
+
<TabItem label="barrel.ts">
|
|
212
|
+
```diff
|
|
213
|
+
export * from './module.js';
|
|
214
|
+
```
|
|
215
|
+
</TabItem>
|
|
216
|
+
|
|
217
|
+
<TabItem label="index.ts">
|
|
218
|
+
```diff
|
|
219
|
+
-export { Hawk, Owl } from './barrel.js';
|
|
220
|
+
+export { Hawk } from './barrel.js'
|
|
221
|
+
```
|
|
222
|
+
</TabItem>
|
|
223
|
+
</Tabs>
|
|
224
|
+
|
|
225
|
+
### Export assignments
|
|
226
|
+
|
|
227
|
+
Knip removes individual exported items in "export assignments", but does not
|
|
228
|
+
remove the entire export declaration if it's empty:
|
|
229
|
+
|
|
230
|
+
```diff title="file.js"
|
|
231
|
+
-export const { a, b } = fn();
|
|
232
|
+
+export const { } = fn();
|
|
233
|
+
|
|
234
|
+
-export const [c, d] = [c, d];
|
|
235
|
+
+export const [, ] = [c, d];
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
Reason: the right-hand side of the assignment might have side-effects. It's not
|
|
239
|
+
safe to always remove the whole declaration. This could be improved in the
|
|
240
|
+
future (feel free to open an issue/RFC).
|
|
241
|
+
|
|
242
|
+
### Enum members
|
|
243
|
+
|
|
244
|
+
Unused members of enums are removed:
|
|
245
|
+
|
|
246
|
+
```diff title="file.ts"
|
|
247
|
+
export enum Directions {
|
|
248
|
+
North = 1,
|
|
249
|
+
East = 2,
|
|
250
|
+
- South = 3,
|
|
251
|
+
West = 4,
|
|
252
|
+
}
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### CommonJS
|
|
256
|
+
|
|
257
|
+
Knip supports CommonJS and removes unused exports:
|
|
258
|
+
|
|
259
|
+
```diff title="common.js"
|
|
260
|
+
-module.exports = { identifier, unused };
|
|
261
|
+
+module.exports = { identifier, };
|
|
262
|
+
|
|
263
|
+
-module.exports.UNUSED = 1;
|
|
264
|
+
-module.exports['ACCESS'] = 1;
|
|
265
|
+
+
|
|
266
|
+
+
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
Warning: the right-hand side of such an assignment might have side-effects. Knip
|
|
270
|
+
currently removes the whole declaration (feel free to open an issue/RFC).
|
|
271
|
+
|
|
272
|
+
### Dependencies
|
|
273
|
+
|
|
274
|
+
Unused dependencies are removed from `package.json`:
|
|
275
|
+
|
|
276
|
+
```diff title="package.json"
|
|
277
|
+
{
|
|
278
|
+
"name": "my-package",
|
|
279
|
+
"dependencies": {
|
|
280
|
+
- "rimraf": "*",
|
|
281
|
+
- "unused-dependency": "*"
|
|
282
|
+
+ "rimraf": "*"
|
|
283
|
+
},
|
|
284
|
+
- "devDependencies": {
|
|
285
|
+
- "unreferenced-package": "5.3.3"
|
|
286
|
+
- }
|
|
287
|
+
+ "devDependencies": {}
|
|
288
|
+
}
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
### Class members <Badge text="experimental" variant="caution" />
|
|
292
|
+
|
|
293
|
+
Unused members of classes can be removed:
|
|
294
|
+
|
|
295
|
+
```diff title="file.ts"
|
|
296
|
+
export class Rectangle {
|
|
297
|
+
constructor(public width: number, public height: number) {}
|
|
298
|
+
|
|
299
|
+
- static Key = 1;
|
|
300
|
+
+
|
|
301
|
+
|
|
302
|
+
area() {
|
|
303
|
+
return this.width * this.height;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
- public get unusedGetter(): string {
|
|
307
|
+
- return 'unusedGetter';
|
|
308
|
+
- }
|
|
309
|
+
}
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
Currently Knip might be too eager removing class members when they're not
|
|
313
|
+
referenced internally but meant to be called by an external library. For
|
|
314
|
+
instance, Knip might think `componentDidMount` and `render` in React class
|
|
315
|
+
component are unused and will remove those.
|
|
316
|
+
|
|
317
|
+
Note that [`classMembers` aren't included by default][4].
|
|
318
|
+
|
|
319
|
+
### Catalog entries
|
|
320
|
+
|
|
321
|
+
Unused [catalog][5] entries are removed from `pnpm-workspace.yaml`:
|
|
322
|
+
|
|
323
|
+
```diff title="pnpm-workspace.yaml"
|
|
324
|
+
packages:
|
|
325
|
+
- 'packages/*'
|
|
326
|
+
catalog:
|
|
327
|
+
react: ^18.0.0
|
|
328
|
+
- unused-package: ^1.0.0
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
Catalogs in `package.json` are supported as well.
|
|
332
|
+
|
|
333
|
+
## What's not included
|
|
334
|
+
|
|
335
|
+
Operations that auto-fix does not (yet) perform and why:
|
|
336
|
+
|
|
337
|
+
- Add unlisted (dev) dependencies to `package.json` (should it go into
|
|
338
|
+
`dependencies` or `devDependencies`? For monorepos in current workspace or
|
|
339
|
+
root?)
|
|
340
|
+
- Add unlisted binaries (which package and package version contains the used
|
|
341
|
+
binary?)
|
|
342
|
+
- Fix duplicate exports (which one should be removed?)
|
|
343
|
+
|
|
344
|
+
[1]: ../reference/issue-types.md
|
|
345
|
+
[2]: https://github.com/JoshuaKGoldberg/formatly
|
|
346
|
+
[3]: https://github.com/webpro-nl/remove-unused-vars
|
|
347
|
+
[4]: ../guides/handling-issues.mdx#class-members
|
|
348
|
+
[5]: https://pnpm.io/catalogs
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Compilers
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Projects may have source files that are not JavaScript or TypeScript, and thus
|
|
6
|
+
require compilation (or transpilation, or pre-processing, you name it). Files
|
|
7
|
+
like `.mdx`, `.astro`, `.vue` and `.svelte` may also import other source files
|
|
8
|
+
and external dependencies. So ideally, these files are included when linting the
|
|
9
|
+
project. That's why Knip supports compilers.
|
|
10
|
+
|
|
11
|
+
## Built-in compilers
|
|
12
|
+
|
|
13
|
+
Knip has built-in "compilers" for the following file extensions:
|
|
14
|
+
|
|
15
|
+
- `.astro`
|
|
16
|
+
- `.css` (only enabled by `tailwindcss`)
|
|
17
|
+
- `.mdx`
|
|
18
|
+
- `.prisma`
|
|
19
|
+
- `.sass` + `.scss`
|
|
20
|
+
- `.svelte`
|
|
21
|
+
- `.vue`
|
|
22
|
+
|
|
23
|
+
Knip does not include real compilers for those files, but regular expressions to
|
|
24
|
+
collect `import` statements. This is fast, requires no dependencies, and enough
|
|
25
|
+
for Knip to build the module graph.
|
|
26
|
+
|
|
27
|
+
On the other hand, real compilers may expose their own challenges in the context
|
|
28
|
+
of Knip. For instance, the Svelte compiler keeps `exports` intact, while they
|
|
29
|
+
might represent component properties. This results in those exports being
|
|
30
|
+
reported as unused by Knip.
|
|
31
|
+
|
|
32
|
+
The built-in functions seem to do a decent job, but override them however you
|
|
33
|
+
like.
|
|
34
|
+
|
|
35
|
+
Compilers are enabled only if certain dependencies are found. If that's not
|
|
36
|
+
working for your project, set `true` and enable any compiler manually:
|
|
37
|
+
|
|
38
|
+
```ts title="knip.ts"
|
|
39
|
+
export default {
|
|
40
|
+
compilers: {
|
|
41
|
+
mdx: true,
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Custom compilers
|
|
47
|
+
|
|
48
|
+
Built-in compilers can be overridden, and additional compilers can be added.
|
|
49
|
+
Since compilers are functions, the Knip configuration file must be a dynamic
|
|
50
|
+
`.js` or `.ts` file.
|
|
51
|
+
|
|
52
|
+
### Interface
|
|
53
|
+
|
|
54
|
+
The compiler function interface is straightforward. Text in, text out:
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
(source: string, filename: string) => string;
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
This may also be an `async` function.
|
|
61
|
+
|
|
62
|
+
:::tip[Note]
|
|
63
|
+
|
|
64
|
+
Compilers will automatically have their extension added as a default extension
|
|
65
|
+
to Knip. This means you don't need to add something like `**/*.{ts,vue}` to the
|
|
66
|
+
`entry` or `project` file patterns manually.
|
|
67
|
+
|
|
68
|
+
:::
|
|
69
|
+
|
|
70
|
+
### Examples
|
|
71
|
+
|
|
72
|
+
- [CSS][1]
|
|
73
|
+
- [MDX][2]
|
|
74
|
+
- [Svelte][3]
|
|
75
|
+
- [Vue][4]
|
|
76
|
+
|
|
77
|
+
#### CSS
|
|
78
|
+
|
|
79
|
+
Here's an example, minimal compiler for CSS files:
|
|
80
|
+
|
|
81
|
+
```ts title="knip.ts"
|
|
82
|
+
export default {
|
|
83
|
+
compilers: {
|
|
84
|
+
css: (text: string) => [...text.matchAll(/(?<=@)import[^;]+/g)].join('\n'),
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
You may wonder why the CSS compiler is not included by default. It's currently
|
|
90
|
+
not clear if it should be included. And if so, what would be the best way to
|
|
91
|
+
determine it should be enabled, and what syntax(es) it should support. Note that
|
|
92
|
+
Tailwind CSS and SASS/SCSS compilers are included.
|
|
93
|
+
|
|
94
|
+
#### MDX
|
|
95
|
+
|
|
96
|
+
Another example, in case the built-in MDX compiler is not enough:
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
import { compile } from '@mdx-js/mdx';
|
|
100
|
+
|
|
101
|
+
export default {
|
|
102
|
+
compilers: {
|
|
103
|
+
mdx: async text => (await compile(text)).toString(),
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
#### Svelte
|
|
109
|
+
|
|
110
|
+
In a Svelte project, the compiler is automatically enabled. Override and use
|
|
111
|
+
Svelte's compiler for better results if the built-in "compiler" is not enough:
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
import type { KnipConfig } from 'knip';
|
|
115
|
+
import { compile } from 'svelte/compiler';
|
|
116
|
+
|
|
117
|
+
export default {
|
|
118
|
+
compilers: {
|
|
119
|
+
svelte: (source: string) => compile(source, {}).js.code,
|
|
120
|
+
},
|
|
121
|
+
} satisfies KnipConfig;
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
#### Vue
|
|
125
|
+
|
|
126
|
+
In a Vue project, the compiler is automatically enabled. Override and use Vue's
|
|
127
|
+
parser for better results if the built-in "compiler" is not enough:
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
import type { KnipConfig } from 'knip';
|
|
131
|
+
import {
|
|
132
|
+
parse,
|
|
133
|
+
type SFCScriptBlock,
|
|
134
|
+
type SFCStyleBlock,
|
|
135
|
+
} from 'vue/compiler-sfc';
|
|
136
|
+
|
|
137
|
+
function getScriptBlockContent(block: SFCScriptBlock | null): string[] {
|
|
138
|
+
if (!block) return [];
|
|
139
|
+
if (block.src) return [`import '${block.src}'`];
|
|
140
|
+
return [block.content];
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function getStyleBlockContent(block: SFCStyleBlock | null): string[] {
|
|
144
|
+
if (!block) return [];
|
|
145
|
+
if (block.src) return [`@import '${block.src}';`];
|
|
146
|
+
return [block.content];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function getStyleImports(content: string): string {
|
|
150
|
+
return [...content.matchAll(/(?<=@)import[^;]+/g)].join('\n');
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const config = {
|
|
154
|
+
compilers: {
|
|
155
|
+
vue: (text: string, filename: string) => {
|
|
156
|
+
const { descriptor } = parse(text, { filename, sourceMap: false });
|
|
157
|
+
return [
|
|
158
|
+
...getScriptBlockContent(descriptor.script),
|
|
159
|
+
...getScriptBlockContent(descriptor.scriptSetup),
|
|
160
|
+
...descriptor.styles.flatMap(getStyleBlockContent).map(getStyleImports),
|
|
161
|
+
].join('\n');
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
} satisfies KnipConfig;
|
|
165
|
+
|
|
166
|
+
export default config;
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
[1]: #css
|
|
170
|
+
[2]: #mdx
|
|
171
|
+
[3]: #svelte
|
|
172
|
+
[4]: #vue
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Integrated Monorepos
|
|
3
|
+
sidebar:
|
|
4
|
+
order: 3
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
Some repositories have a single `package.json`, but consist of multiple projects
|
|
8
|
+
with configuration files across the repository. A good example is the [Nx
|
|
9
|
+
integrated monorepo style][1].
|
|
10
|
+
|
|
11
|
+
:::tip
|
|
12
|
+
|
|
13
|
+
An integrated monorepo is a single workspace.
|
|
14
|
+
|
|
15
|
+
:::
|
|
16
|
+
|
|
17
|
+
## Entry Files
|
|
18
|
+
|
|
19
|
+
The default entrypoints files might not be enough. Here's an idea that might fit
|
|
20
|
+
this type of monorepo:
|
|
21
|
+
|
|
22
|
+
```json title="knip.json"
|
|
23
|
+
{
|
|
24
|
+
"entry": ["{apps,libs}/**/src/index.{ts,tsx}"],
|
|
25
|
+
"project": ["{apps,libs}/**/src/**/*.{ts,tsx}"]
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Plugins
|
|
30
|
+
|
|
31
|
+
Let's assume some of these projects are applications ("apps") which have their
|
|
32
|
+
own ESLint configuration files and Cypress configuration and test files. This
|
|
33
|
+
may result in those files getting reported as unused, and consequently also the
|
|
34
|
+
dependencies they import and refer to.
|
|
35
|
+
|
|
36
|
+
In that case, we could configure the ESLint and Cypress plugins like this:
|
|
37
|
+
|
|
38
|
+
```json title="knip.json"
|
|
39
|
+
{
|
|
40
|
+
"eslint": {
|
|
41
|
+
"config": ["{apps,libs}/**/.eslintrc.json"]
|
|
42
|
+
},
|
|
43
|
+
"cypress": {
|
|
44
|
+
"entry": ["apps/**/cypress.config.ts", "apps/**/cypress/e2e/*.spec.ts"]
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Adapt the file patterns to your project, and the relevant `config` and `entry`
|
|
50
|
+
files and dependencies should no longer be reported as unused.
|
|
51
|
+
|
|
52
|
+
## Internal Workspace Dependencies
|
|
53
|
+
|
|
54
|
+
A note about repositories with multiple `package.json` files and **internal**
|
|
55
|
+
workspace packages: it is recommended to list all dependencies in each consuming
|
|
56
|
+
`package.json`, allowing Knip to do fine-grained reporting of both unused and
|
|
57
|
+
unlisted dependencies.
|
|
58
|
+
|
|
59
|
+
An alternative is to `ignoreDependencies: ["@internal/*"]`.
|
|
60
|
+
|
|
61
|
+
[1]: https://nx.dev/getting-started/tutorials/integrated-repo-tutorial
|