@knip/mcp 0.0.4 → 0.0.6

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.
Files changed (55) hide show
  1. package/docs/docs/blog/brief-history.md +30 -0
  2. package/docs/docs/blog/for-editors-and-agents.md +127 -0
  3. package/docs/docs/blog/knip-v3.mdx +88 -0
  4. package/docs/docs/blog/knip-v4.mdx +149 -0
  5. package/docs/docs/blog/knip-v5.mdx +190 -0
  6. package/docs/docs/blog/migration-to-v1.md +65 -0
  7. package/docs/docs/blog/release-notes-v2.md +46 -0
  8. package/docs/docs/blog/slim-down-to-speed-up.md +269 -0
  9. package/docs/docs/blog/state-of-knip.md +191 -0
  10. package/docs/docs/blog/two-years.mdx +107 -0
  11. package/docs/docs/explanations/comparison-and-migration.md +129 -0
  12. package/docs/docs/explanations/entry-files.md +70 -0
  13. package/docs/docs/explanations/plugins.md +319 -0
  14. package/docs/docs/explanations/why-use-knip.md +128 -0
  15. package/docs/docs/features/auto-fix.mdx +333 -0
  16. package/docs/docs/features/compilers.md +172 -0
  17. package/docs/docs/features/integrated-monorepos.md +61 -0
  18. package/docs/docs/features/monorepos-and-workspaces.md +134 -0
  19. package/docs/docs/features/production-mode.md +95 -0
  20. package/docs/docs/features/reporters.md +302 -0
  21. package/docs/docs/features/rules-and-filters.md +102 -0
  22. package/docs/docs/features/script-parser.md +156 -0
  23. package/docs/docs/features/source-mapping.md +100 -0
  24. package/docs/docs/guides/configuring-project-files.md +205 -0
  25. package/docs/docs/guides/contributing.md +24 -0
  26. package/docs/docs/guides/handling-issues.mdx +646 -0
  27. package/docs/docs/guides/issue-reproduction.md +94 -0
  28. package/docs/docs/guides/namespace-imports.md +125 -0
  29. package/docs/docs/guides/performance.md +97 -0
  30. package/docs/docs/guides/troubleshooting.md +136 -0
  31. package/docs/docs/guides/using-knip-in-ci.md +54 -0
  32. package/docs/docs/guides/working-with-commonjs.md +72 -0
  33. package/docs/docs/index.mdx +160 -0
  34. package/docs/docs/overview/configuration.md +104 -0
  35. package/docs/docs/overview/features.md +66 -0
  36. package/docs/docs/overview/getting-started.mdx +195 -0
  37. package/docs/docs/overview/screenshots-videos.md +42 -0
  38. package/docs/docs/playground.mdx +38 -0
  39. package/docs/docs/reference/cli.md +485 -0
  40. package/docs/docs/reference/configuration.md +413 -0
  41. package/docs/docs/reference/dynamic-configuration.mdx +72 -0
  42. package/docs/docs/reference/faq.md +441 -0
  43. package/docs/docs/reference/issue-types.md +43 -0
  44. package/docs/docs/reference/jsdoc-tsdoc-tags.md +122 -0
  45. package/docs/docs/reference/known-issues.md +64 -0
  46. package/docs/docs/reference/plugins/.gitkeep +0 -0
  47. package/docs/docs/reference/plugins.md +238 -0
  48. package/docs/docs/reference/related-tooling.md +46 -0
  49. package/docs/docs/sponsors.mdx +65 -0
  50. package/docs/docs/typescript/unused-dependencies.md +86 -0
  51. package/docs/docs/typescript/unused-exports.md +87 -0
  52. package/docs/docs/writing-a-plugin/argument-parsing.md +202 -0
  53. package/docs/docs/writing-a-plugin/index.md +376 -0
  54. package/docs/docs/writing-a-plugin/inputs.md +162 -0
  55. package/package.json +5 -3
@@ -0,0 +1,441 @@
1
+ ---
2
+ title: FAQ
3
+ date: 2024-08-20
4
+ ---
5
+
6
+ ## Introduction
7
+
8
+ Knip finds and fixes unused dependencies, exports and files. As a "kitchen sink"
9
+ in the npm ecosystem, it creates comprehensive module and dependency graphs of
10
+ your project.
11
+
12
+ :::note[Rationale]
13
+
14
+ The JavaScript/TypeScript ecosystem has a vast amount of frameworks and tools.
15
+ Additionally, file locations, configuration semantics, command-line arguments
16
+ and so on vary wildly. Files and dependencies are referenced in many ways. Knip
17
+ tries harder than you think to cover it all.
18
+
19
+ :::
20
+
21
+ This FAQ is an attempt to provide some perspective on a few design decisions and
22
+ why certain things work the way they do. Here and there it's intentionally a bit
23
+ more in-depth than the rest of the docs.
24
+
25
+ ## Comparison
26
+
27
+ ### Why isn't Knip an ESLint plugin?
28
+
29
+ Linters like ESLint analyze files separately, while Knip lints projects as a
30
+ whole.
31
+
32
+ Knip requires full module and dependency graphs to find clutter across the
33
+ project. Creating these comprehensive graphs is not a trivial task and it seems
34
+ no such tool exists today, even more so when it comes to monorepos.
35
+
36
+ File-oriented linters like ESLint are complementary to Knip.
37
+
38
+ ### Isn't tree-shaking enough?
39
+
40
+ In short: no. They share an important goal: improve UX by removing unused code.
41
+ The main takeaway here is that tree-shaking and Knip are different and
42
+ complementary tools.
43
+
44
+ Tree-shaking is a build or compile-time activity to reduce production bundle
45
+ size. It typically operates on bundled production code, which might include
46
+ external/third-party code. An optimization in the build process, "out of your
47
+ hands".
48
+
49
+ On the other hand, Knip is a project linter that should be part of the QA phase.
50
+ It lints, reports and fixes only your own source code. Moreover, in contrast
51
+ with other linters, focuses on inter-file dependencies, so dead code within a
52
+ file may not be caught by Knip.
53
+
54
+ Issues reported by the linter are then for you to handle (except for everything
55
+ that Knip [auto-fixes][1] for you).
56
+
57
+ Besides those differences, Knip has a broader scope:
58
+
59
+ - Improve DX (see [less is more][2]).
60
+ - Include non-production code and dependencies in the process by default.
61
+ - Handle more [types of issues][3] (such as unlisted dependencies).
62
+
63
+ ## Synergy
64
+
65
+ ### Why does Knip have plugins?
66
+
67
+ Plugins are an essential part of Knip. They prevent you from a lot of
68
+ configuration out of the box, by adding entry files as accurately as possible
69
+ and only for the tools actually installed. Yet the real magic is in their custom
70
+ parsers for configuration files and command-line argument definitions.
71
+
72
+ For instance, Vitest has the `environment` configuration option. The Vitest
73
+ plugin knows `"node"` is the default value for `environment` which does not
74
+ require an extra package, but will translate `"edge-runtime"` to the
75
+ `@edge-runtime/vm` package. This allows Knip to report it if this package is not
76
+ listed in `package.json`, or when it is no longer used after changes in the
77
+ Vitest configuration.
78
+
79
+ Configuration files may also contain references to entry files. For instance,
80
+ Jest has `setupFilesAfterEnv: "<rootDir>/jest.setup.js"` or a reference may
81
+ point to a file in another workspace in the same monorepo, e.g.
82
+ `setupFiles: ['@org/shared/jest-setup.ts']`. Those entry files may also contain
83
+ imports of internal modules or external dependencies, and so on.
84
+
85
+ ### Why is Knip so heavily engineered?
86
+
87
+ Even though a modular approach has its merits, for Knip it makes sense to have
88
+ all the pieces in a single tool.
89
+
90
+ Building up the module and dependency graphs requires non-standard module
91
+ resolution and not only static but also dynamic analysis (i.e. actually load and
92
+ execute modules), such as for parsers of plugins to receive the exported value
93
+ of dynamic tooling configuration files. Additionally, [exports consumed by
94
+ external libraries][4] require type information, as supported by the TypeScript
95
+ backend. Last but not least, shell script parsing is required to find the right
96
+ entry files, configuration files and dependencies accurately.
97
+
98
+ The rippling effect of plugins and recursively adding entry files and
99
+ dependencies to build up the graphs is also exactly what's meant by
100
+ ["comprehensive" here][5].
101
+
102
+ ## Building the graphs
103
+
104
+ ### Where does Knip look for entry files?
105
+
106
+ - In default locations such as `index.js` and `src/index.ts`
107
+ - In `main`, `bin` and `exports` fields in `package.json`
108
+ - In the entry files as configured by enabled plugins
109
+ - In `config` files as configured and parsed by enabled plugins
110
+ - The `config` files themselves are entry files
111
+ - In dynamic imports (i.e. `require()` and `import()` calls)
112
+ - In `require.resolve('./entry.js')`
113
+ - In `import.meta.resolve('./entry.mjs')`
114
+ - Through scripts inside template strings in source files such as:
115
+
116
+ ```ts
117
+ await $({ stdio: 'inherit' })`c8 node hydrate.js`; // execa
118
+ await $`node scripts/parse.js`; // bun/zx
119
+ ```
120
+
121
+ - Through scripts in `package.json` such as:
122
+
123
+ ```json
124
+ {
125
+ "name": "my-lib",
126
+ "scripts": {
127
+ "start": "node --import tsx/esm run.ts",
128
+ "start": "vitest -c config/vitest.config.ts"
129
+ }
130
+ }
131
+ ```
132
+
133
+ - Through plugins handling CI workflow files like `.github/workflows/ci.yml`:
134
+
135
+ ```yaml
136
+ jobs:
137
+ test:
138
+ steps:
139
+ run: playwright test e2e/**/*.spec.ts --config playwright.e2e.config.ts
140
+ run: node --import tsx/esm run.ts
141
+ ```
142
+
143
+ Scripts like the ones shown here may also contain references to configuration
144
+ files (`config/vitest.config.ts` and `playwright.e2e.config.ts` in the examples
145
+ above). They're recognized as configuration files and passed to their respective
146
+ plugins, and may contain additional entry files.
147
+
148
+ Entry files are added to the module graph. [Module resolution][6] might result
149
+ in additional entry files recursively until no more entry files are found.
150
+
151
+ ### What does Knip look for in source files?
152
+
153
+ The TypeScript source file parser is powerful and fault-tolerant. Knip visits
154
+ all nodes of the generated AST to find:
155
+
156
+ - Imports and dynamic imports of internal modules and external dependencies
157
+ - Exports
158
+ - Accessed properties on namespace imports and re-exports to track individual
159
+ export usage
160
+ - Calls to `require.resolve` and `import.meta.resolve`
161
+ - Scripts in template strings (passed to [script parser][7])
162
+
163
+ ### What's in the graphs?
164
+
165
+ Once the module and dependency graphs are created, they contain the information
166
+ required to create the report including all issue types:
167
+
168
+ - Unused files
169
+ - Unused dependencies
170
+ - Unused devDependencies
171
+ - Referenced optional peerDependencies
172
+ - Unlisted dependencies
173
+ - Unlisted binaries
174
+ - Unresolved imports
175
+ - Unused exports
176
+ - Unused exported types
177
+ - Unused exported enum members
178
+ - Duplicate exports
179
+
180
+ And optionally more issue types like individual exports and exported types in
181
+ namespace imports, and unused class members.
182
+
183
+ The graphs allows to report more interesting details, such as:
184
+
185
+ - Circular references
186
+ - Usage numbers per export
187
+ - Export usage across workspaces in a monorepo
188
+ - List of all binaries used
189
+ - List of all used (OS) binaries not installed in `node_modules`
190
+
191
+ ### Why doesn't Knip just read the lockfile?
192
+
193
+ Knip reads the `package.json` file of each dependency. Most of the information
194
+ required is in the lockfile as well, which would be more efficient. However,
195
+ there are a few issues with this approach:
196
+
197
+ - It requires lockfile parsing for each lockfile format and version of each
198
+ package manager.
199
+ - The lockfile doesn't contain whether the package [has types included][8].
200
+
201
+ ## Module Resolution
202
+
203
+ ### Why doesn't Knip use an existing module resolver?
204
+
205
+ Runtimes like Node.js provide `require.resolve` and `import.meta.resolve`.
206
+ TypeScript comes with module resolution built-in. More module resolvers are out
207
+ there and bundlers are known to use or come with module resolvers. None of them
208
+ seem to meet all requirements to be usable on its own by Knip:
209
+
210
+ - Support non-standard extensions like `.css`, `.svelte` and `.png`
211
+ - Support path aliases
212
+ - Support `exports` map in `package.json`
213
+ - Support self-referencing imports
214
+ - Rewire `package.json#main` build artifacts like `dist/module.js` to its source
215
+ at `src/module.ts`
216
+ - Don't resolve to type definition paths like `module.d.ts` but source code at
217
+ `module.js`
218
+
219
+ A few strategies have been tried and tweaked, and Knip currently uses a
220
+ combination of [oxc-resolver][9], the TypeScript module resolver and a few
221
+ customizations. This single custom module resolver function is hooked into the
222
+ TypeScript compiler and language service hosts.
223
+
224
+ Everything else is handled by `oxc-resolver` for things like [script parsing][7]
225
+ and resolving references to files in other workspaces.
226
+
227
+ ### How does Knip handle non-standard import syntax?
228
+
229
+ Knip tries to be resilient against import syntax like what's used by e.g.
230
+ webpack loaders or Vite asset imports. Knip strips off the prefixes and suffixes
231
+ in import specifiers like this:
232
+
233
+ ```ts title="component.ts"
234
+ import Icon from './icon.svg?raw';
235
+ import Styles from '-!style-loader!css-loader?modules!./styles.css';
236
+ ```
237
+
238
+ In this example, the `style-loader` and `css-loader` dependencies should be
239
+ dependencies found in webpack configuration, handled by Knip's webpack plugin.
240
+
241
+ ## TypeScript
242
+
243
+ ### What's the difference between workspaces, projects and programs?
244
+
245
+ A workspace is a directory with a `package.json` file. They're configured in
246
+ `package.json#workspaces` (or `pnpm-workspaces.yml`). In case a directory has a
247
+ `package.json` file, but is not a workspace (from a package manager
248
+ perspective), it can be added as a workspace to the Knip configuration.
249
+
250
+ Projects - in the context of TypeScript - are directories with a `tsconfig.json`
251
+ file. They're not a concept in Knip.
252
+
253
+ A TypeScript program has a 1-to-1 relationship with workspaces if they're
254
+ analyzed in isolation. However, by default Knip optimizes for performance and
255
+ utilizes [workspace sharing][10]. That's why debug output contains messages like
256
+ "Installed 2 programs for 29 workspaces".
257
+
258
+ ### Why doesn't Knip match my TypeScript project structure?
259
+
260
+ Repositories and workspaces in a monorepo aren't necessarily structured like
261
+ TypeScript projects. Put simply, the location of `package.json` files isn't
262
+ always adjacent to `tsconfig.json` files. Knip follows the structure of
263
+ workspaces in a monorepo.
264
+
265
+ An additional layering of TypeScript projects would complicate things. The
266
+ downside is that a `tsconfig.json` file not used by Knip may have conflicting
267
+ module resolution settings, potentially resulting in missed files.
268
+
269
+ In practice, this is rarely an issue. Knip sticks to the workspaces structure
270
+ and installs a single "kitchen sink" module resolver function per workspace.
271
+ Different strategies might add more complexity and performance penalties, while
272
+ the current strategy is simple, fast and good enough.
273
+
274
+ Note that any directory with a `package.json` not listed in the root
275
+ `package.json#workspaces` can be added to the Knip configuration manually to
276
+ have it handled as a separate workspace.
277
+
278
+ ### Why doesn't Knip analyze workspaces in isolation by default?
279
+
280
+ Knip creates TypeScript programs to create a module graph and traverse file
281
+ ASTs. In a monorepo, it would make a lot of sense to create one program per
282
+ workspace. However, this slows down the whole process considerably. That's why
283
+ Knip shares the files of multiple workspaces in a single program if their
284
+ configuration allows it. This optimization is enabled by default, while it also
285
+ allows the module resolver (one per program) to do some more caching.
286
+
287
+ Also see [workspace sharing][10].
288
+
289
+ ### Why doesn't Knip just use `ts.findReferences`?
290
+
291
+ TypeScript has a very good "Find references" feature, that you might be using in
292
+ your IDE as well. Yet at scale this becomes too slow. That's why Knip builds up
293
+ its own module graph to look up export usages. Additional benefits for this
294
+ comprehensive graph include:
295
+
296
+ - serializable and cacheable
297
+ - enables more features
298
+ - usable for other tools to build upon as well
299
+
300
+ Without sacrificing these benefits, Knip does use `ts.findReferences` to find
301
+ references to class members (i.e. when the issue type `classMembers` is
302
+ included). In case analysis of exports requires type information of external
303
+ dependencies, the [`--include-libs ` flag][4] will trigger the same.
304
+
305
+ ### Why can't I use path aliases to reference other workspaces?
306
+
307
+ Projects can use `compilerOptions.paths` to alias paths in other workspaces in
308
+ the same monorepo. Knip doesn't understand those paths might represent internal
309
+ workspaces and might report false positives. How we ended up here is a bit
310
+ complicated, but a major reason is that Knip does it's job based on the
311
+ workspace graph, while [workspaces are different from TypeScript programs][11]
312
+ and [the workspace graph doesn't match TypeScript project structure][12].
313
+
314
+ The recommendation and best practice is to list such workspaces/dependencies in
315
+ `package.json`, and import them as such. Other tooling should not have any
316
+ issues with this standard approach either.
317
+
318
+ Also see the example in [TypeScript path aliases in monorepos][13].
319
+
320
+ ### What's up with that configurable `tsconfig.json` location?
321
+
322
+ There's a difference between `--tsConfig [file]` as a CLI argument and the
323
+ `typescript.config` option in Knip configuration.
324
+
325
+ The [`--tsConfig [file]` option][14] is used to provide an alternative location
326
+ for the default root `tsconfig.json` file. Relevant `compilerOptions` include
327
+ `paths` and `moduleResolution`. This setting is only available at the root
328
+ level.
329
+
330
+ On the other hand, the [`config` option of the plugin][15] can be set per
331
+ workspace. The TypeScript plugin extracts referenced external dependencies such
332
+ as those in `extends`, `compilerOptions.types` and JSX settings:
333
+
334
+ ```json title="tsconfig.json"
335
+ {
336
+ "extends": "@tsconfig/node20/tsconfig.json",
337
+ "compilerOptions": {
338
+ "jsxImportSource": "hastscript/svg"
339
+ }
340
+ }
341
+ ```
342
+
343
+ From this example, Knip can determine whether the `@tsconfig/node20` and
344
+ `hastscript` dependencies are properly listed in `package.json`.
345
+
346
+ #### Notes
347
+
348
+ - The TypeScript plugin doesn't add support for TypeScript to Knip (that's
349
+ already built-in). Like other plugins, it extracts dependencies from
350
+ `tsconfig.json`. With the `typescript.config` option an alternative location
351
+ for `tsconfig.json` can be set per workspace.
352
+ - In case path aliases from `compilerOptions.paths` aren't picked up by Knip,
353
+ either use `--tsConfig [file]` to target a different `tsconfig.json`, or
354
+ manually add [paths][16] to the Knip configuration. The latter can be done per
355
+ workspace.
356
+
357
+ ## Compilers
358
+
359
+ ### How does Knip handle Svelte or Astro files?
360
+
361
+ To further increase the coverage of the module graph, non-standard files other
362
+ than JavaScript and TypeScript modules should be included as well. For instance,
363
+ `.mdx` and `.astro` files can import each other, internal modules and external
364
+ dependencies.
365
+
366
+ Knip includes basic "compilers" for a few common file types (Astro, MDX, Svelte,
367
+ Vue). Knip does not include actual compilers for reasons of potential
368
+ incompatibility with the existing compiler, and dependency size. Knip allows to
369
+ override them with the compilers in your project, and add additional ones for
370
+ other file types.
371
+
372
+ ### Why are the exports of my `.vue` files not used?
373
+
374
+ Knip comes with basic "compilers" for a few common non-standard file types.
375
+ They're not actual compilers, they're regular expressions only to extract import
376
+ statements. Override the built-in Vue "compiler" with the real one in your
377
+ project. Also see the answer to the previous question and [Compilers][17].
378
+
379
+ ## Miscellaneous
380
+
381
+ ### Why isn't production mode the default?
382
+
383
+ The default mode of Knip includes all source files, tests, dependencies, dev
384
+ dependencies and tooling configuration.
385
+
386
+ On the other hand, production mode considers only source files and production
387
+ dependencies. Plugins add only production entry files.
388
+
389
+ Which mode should've been the default? They both have their merits:
390
+
391
+ - Production mode catches dead production code and dependencies. This mode has
392
+ the most impact on UX, since less code tends to be faster and safer.
393
+ - Default mode potentially catches more issues, e.g. lots of unused plugins of
394
+ tooling, including most issues found in production mode. This mode has the
395
+ most impact on DX, for the same reason.
396
+
397
+ Also see [production mode][18].
398
+
399
+ ### Why doesn't Knip have...?
400
+
401
+ Examples of features that have been requested include:
402
+
403
+ - Expose programmatic API
404
+ - Add local/custom plugins
405
+ - Expose the module and dependency graphs
406
+ - Custom AST visitors, e.g. to find and return:
407
+ - Unused interface/type members
408
+ - Unused object members (and e.g. React component props)
409
+ - Unused object props in function return values
410
+ - Analyze workspaces in parallel
411
+ - Plugins for editors like VS Code and WebStorm (LSP-based?)
412
+ - Support Deno
413
+ - Improve internal code structures and accessibility to support contributions
414
+ - One-shot dead code removal (more comprehensive removal of unused variables,
415
+ duplicate exports, dead code, etc).
416
+ - Replace dependencies for better performance and correctness, such as for shell
417
+ script parsing, module resolution and globbing with "unignores".
418
+
419
+ These are all interesting ideas, but most increase the API surface area, and all
420
+ require more development efforts and maintenance. Time is limited and
421
+ [sponsorships][19] currently don't cover - this can change though!
422
+
423
+ [1]: ../features/auto-fix.mdx
424
+ [2]: ../explanations/why-use-knip.md#less-is-more
425
+ [3]: ./issue-types.md
426
+ [4]: ../guides/handling-issues.mdx#external-libraries
427
+ [5]: ../explanations/why-use-knip.md#comprehensive
428
+ [6]: #module-resolution
429
+ [7]: ../features/script-parser.md
430
+ [8]: ../guides/handling-issues.mdx#types-packages
431
+ [9]: https://oxc.rs/docs/guide/usage/resolver.html
432
+ [10]: ../guides/performance.md#workspace-sharing
433
+ [11]: #whats-the-difference-between-workspaces-projects-and-programs
434
+ [12]: #why-doesnt-knip-match-my-typescript-project-structure
435
+ [13]: ../guides/handling-issues.mdx#typescript-path-aliases-in-monorepos
436
+ [14]: ../reference/cli.md#--tsconfig-file
437
+ [15]: ../explanations/plugins.md#configuration-files
438
+ [16]: ../reference/configuration.md#paths
439
+ [17]: ../features/compilers.md
440
+ [18]: ../features/production-mode.md
441
+ [19]: /sponsors
@@ -0,0 +1,43 @@
1
+ ---
2
+ title: Issue Types
3
+ tableOfContents: false
4
+ ---
5
+
6
+ Knip reports the following types of issues:
7
+
8
+ | Title | Description | | Key |
9
+ | :----------------------------------- | :--------------------------------------------------------- | ----- | :------------- |
10
+ | Unused files | Unable to find a reference to this file | 🔧 | `files` |
11
+ | Unused dependencies | Unable to find a reference to this dependency | 🔧 | `dependencies` |
12
+ | Unused devDependencies | Unable to find a reference to this devDependency | 🔧 | `dependencies` |
13
+ | Referenced optional peerDependencies | Optional peer dependency is referenced | | `dependencies` |
14
+ | Unlisted dependencies | Used dependencies not listed in package.json | | `unlisted` |
15
+ | Unlisted binaries | Binaries from dependencies not listed in package.json | | `binaries` |
16
+ | Unused catalog entries | Unable to find a reference to this catalog entry | 🔧 | `catalog` |
17
+ | Unresolved imports | Unable to resolve this (import) specifier | | `unresolved` |
18
+ | Unused exports | Unable to find a reference to this export | 🔧 | `exports` |
19
+ | Unused exported types | Unable to find a reference to this exported type | 🔧 | `types` |
20
+ | Exports in used namespace | Namespace with export is referenced, but not export itself | 🔧 🟠 | `nsExports` |
21
+ | Exported types in used namespace | Namespace with type is referenced, but not type itself | 🔧 🟠 | `nsTypes` |
22
+ | Unused exported enum members | Unable to find a reference to this enum member | 🔧 | `enumMembers` |
23
+ | Unused exported class members | Unable to find a reference to this class member | 🔧 🟠 | `classMembers` |
24
+ | Duplicate exports | This is exported more than once | | `duplicates` |
25
+
26
+ ## Legend
27
+
28
+ | | Description |
29
+ | --- | :-------------------------------------------------- |
30
+ | 🔧 | [Auto-fixable][1] issue types |
31
+ | 🟠 | Not included by default (include with [filters][2]) |
32
+
33
+ ## Notes
34
+
35
+ - When an issue type has zero issues, it is not shown.
36
+ - The `devDependencies` and `optionalPeerDependencies` are covered in a single
37
+ key for all `dependencies`. In [strict production mode][3], `devDependencies`
38
+ are not included.
39
+ - The `types` issue type includes `enum`, `interface` and `type` exports.
40
+
41
+ [1]: ../features/auto-fix.mdx
42
+ [2]: ../features/rules-and-filters.md#filters
43
+ [3]: ../features/production-mode.md#strict-mode
@@ -0,0 +1,122 @@
1
+ ---
2
+ title: JSDoc & TSDoc Tags
3
+ ---
4
+
5
+ JSDoc or TSDoc tags can be used to make exceptions for unused or duplicate
6
+ exports.
7
+
8
+ Knip tries to minimize configuration and introduces no new syntax. That's why it
9
+ hooks into JSDoc and TSDoc tags.
10
+
11
+ :::caution
12
+
13
+ Adding tags or excluding a certain type of issues from the report is usually not
14
+ recommended. It hides issues, which is often a sign of code smell or ambiguity
15
+ and ends up harder to maintain. It's usually better to refactor the code (or
16
+ report an issue with Knip for false positives).
17
+
18
+ :::
19
+
20
+ JSDoc comments always start with `/**` (not `//`) and can be single or
21
+ multi-line.
22
+
23
+ ## Tags
24
+
25
+ Use arbitrary [tags][1] to exclude or include tagged exports from the report.
26
+ Example:
27
+
28
+ ```ts
29
+ /** @lintignore */
30
+ export const myUnusedExport = 1;
31
+
32
+ /** @lintignore */
33
+ import Unresolved from './generated/lib.js';
34
+ ```
35
+
36
+ And then include (`+`) or exclude (`-`) these tagged exports from the report
37
+ like so:
38
+
39
+ ```shell
40
+ knip --tags=-lintignore,-internal
41
+ ```
42
+
43
+ Tags can also be [configured in `knip.json`][2].
44
+
45
+ ## `@public`
46
+
47
+ By default, Knip reports unused exports in non-entry files.
48
+
49
+ Tag the export as `@public` and Knip will not report it.
50
+
51
+ Example:
52
+
53
+ ```ts
54
+ /**
55
+ * @public
56
+ */
57
+ export const unusedFunction = () => {};
58
+ ```
59
+
60
+ This tag can also be used to make exceptions in entry files when using
61
+ [--include-entry-exports][3].
62
+
63
+ [JSDoc: @public][4] and [TSDoc: @public][5]
64
+
65
+ ## `@internal`
66
+
67
+ Internal exports are not meant for public consumption, but only for internal
68
+ usage such as tests. This means they would be reported in [production mode][6].
69
+
70
+ Mark the export with `@internal` and Knip will not report the export in
71
+ production mode.
72
+
73
+ Example:
74
+
75
+ ```ts
76
+ /** @internal */
77
+ export const internalTestedFunction = () => {};
78
+ ```
79
+
80
+ In general it's not recommended to expose and test implementation details, but
81
+ exceptions are possible. Those should not be reported as false positives, so
82
+ when using production mode you'll need to help Knip out by tagging them as
83
+ `@internal`.
84
+
85
+ [TSDoc: @internal][7]
86
+
87
+ ## `@alias`
88
+
89
+ Knip reports duplicate exports. To prevent this, tag one of the exports as
90
+ `@alias`.
91
+
92
+ Example:
93
+
94
+ ```ts
95
+ export const Component = () => {};
96
+
97
+ /** @alias */
98
+ export default Component;
99
+ ```
100
+
101
+ An alternative solution is to use `--exclude duplicates` and exclude all
102
+ duplicates from being reported.
103
+
104
+ [JSDoc: @alias][8]
105
+
106
+ ## `@beta`
107
+
108
+ Works identical to [`@public`][9]. Knip ignores other tags like `@alpha` and
109
+ `@experimental`.
110
+
111
+ [TSDoc: @beta][10]
112
+
113
+ [1]: ../reference/cli.md#--tags
114
+ [2]: ./configuration.md#tags
115
+ [3]: ./cli.md#--include-entry-exports
116
+ [4]: https://jsdoc.app/tags-public.html
117
+ [5]: https://tsdoc.org/pages/tags/public/
118
+ [6]: ../features/production-mode.md
119
+ [7]: https://tsdoc.org/pages/tags/internal/
120
+ [8]: https://jsdoc.app/tags-alias.html
121
+ [9]: #public
122
+ [10]: https://tsdoc.org/pages/tags/beta/
@@ -0,0 +1,64 @@
1
+ ---
2
+ title: Known Issues
3
+ ---
4
+
5
+ This page contains a list of known issues you might run into when using Knip.
6
+
7
+ ## Exceptions from config files
8
+
9
+ An exception may be thrown when a Knip plugin loads a JavaScript or TypeScript
10
+ configuration file such as `webpack.config.js` or `vite.config.ts`. Knip may
11
+ load such files differently, in a different environment, or without certain
12
+ environment variables set.
13
+
14
+ If it isn't clear what's throwing the exception, try another run with `--debug`
15
+ to locate the cause of the issue with more details. Examples of issues when Knip
16
+ loads configuration files:
17
+
18
+ - Missing environment variable
19
+ - Relative path (e.g. which is sometimes not resolved from correct directory,
20
+ try `import.meta.dirname` or `__dirname`)
21
+
22
+ As a last resort, the [plugin can be disabled][1].
23
+
24
+ ## Path aliases in config files
25
+
26
+ Loading the configuration file (e.g. `cypress.config.ts`) for one of Knip's
27
+ plugins may give an error:
28
+
29
+ ```
30
+ Analyzing workspace ....
31
+ Error loading .../cypress.config.ts
32
+ Reason: Cannot find module '@alias/name'
33
+ Require stack:
34
+ - .../cypress.config.ts
35
+ ```
36
+
37
+ Some tools (such as Cypress and Jest) support using TypeScript path aliases in
38
+ the configuration file. Jiti does support aliases, but in a different format
39
+ compared to `tsconfig.json#compilerOptions.paths` and `knip.json#paths` (e.g.
40
+ the target values are not arrays).
41
+
42
+ Potential workarounds:
43
+
44
+ - Rewrite the import in the configuration file to a relative import.
45
+ - Use Bun with [knip-bun][2].
46
+ - [Disable the plugin][1] (not recommended, try the other options first).
47
+
48
+ ## Nx Daemon
49
+
50
+ In Nx projects you might encounter this error:
51
+
52
+ ```sh
53
+ NX Daemon process terminated and closed the connection
54
+ ```
55
+
56
+ The solution is to [disable the Nx Daemon][3]:
57
+
58
+ ```sh
59
+ NX_DAEMON=false knip
60
+ ```
61
+
62
+ [1]: ./configuration.md#plugins
63
+ [2]: ./cli.md#knip-bun
64
+ [3]: https://nx.dev/concepts/nx-daemon#turning-it-off
File without changes