knip 2.0.0-alpha.0 → 2.0.0-alpha.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/README.md CHANGED
@@ -21,27 +21,29 @@ The dots don't connect themselves. This is where Knip comes in:
21
21
  - [x] Finds unused members of classes and enums
22
22
  - [x] Built-in support for [monorepos/workspaces][1]
23
23
  - [x] Growing list of [built-in plugins][2]
24
+ - [x] Use [compilers][3] to include other file types (e.g. MDX, Svelte, Astro)
24
25
  - [x] Checks npm scripts for used and unlisted dependencies
25
26
  - [x] Supports JavaScript (without `tsconfig.json`, or TypeScript `allowJs: true`)
26
- - [x] Features multiple [reporters][3] and supports [custom reporters][4]
27
+ - [x] Features multiple [reporters][4] and supports [custom reporters][5]
27
28
  - [x] Run Knip as part of your CI environment to detect issues and prevent regressions
28
29
 
29
30
  Knip shines in both small and large projects. It's a fresh take on keeping your projects clean & tidy!
30
31
 
31
- [![An orange cow with scissors, Van Gogh style][6]][5] <sup>_“An orange cow with scissors, Van Gogh style” - generated
32
+ [![An orange cow with scissors, Van Gogh style][7]][6] <sup>_“An orange cow with scissors, Van Gogh style” - generated
32
33
  with OpenAI_</sup>
33
34
 
34
- ## Migrating to v1.0.0
35
+ ## Migrating to v2.0.0
35
36
 
36
- When coming from version v0.13.3 or before, please see [migration to v1][7].
37
+ Migrating from v1 to v2 requires no changes in configuration. Please see [https://github.com/webpro/knip/issues/73][8]
38
+ for the full story.
37
39
 
38
40
  ## Issues
39
41
 
40
- Are you seeing false positives? Please report them by [opening an issue in this repo][8]. Bonus points for linking to a
42
+ Are you seeing false positives? Please report them by [opening an issue in this repo][9]. Bonus points for linking to a
41
43
  public repository using Knip, or even opening a pull request with a directory and example files in `test/fixtures`.
42
44
  Correctness and bug fixes have priority over performance and new features.
43
45
 
44
- Also see the [FAQ][9].
46
+ Also see the [FAQ][10].
45
47
 
46
48
  ## Installation
47
49
 
@@ -52,14 +54,24 @@ Knip supports LTS versions of Node.js, and currently requires at least Node.js v
52
54
  ## Usage
53
55
 
54
56
  Knip has good defaults and you can run it without any configuration, but especially larger projects get more out of Knip
55
- with a configuration file (or a `knip` property in `package.json`). Let's name this file `knip.json` with these contents
56
- (you might want to adjust right away for your project):
57
+ with a configuration file (or a `knip` property in `package.json`). Here's a `knip.json` with the default values:
57
58
 
58
59
  ```json
59
60
  {
60
- "$schema": "https://unpkg.com/knip@1/schema.json",
61
- "entry": ["src/index.ts"],
62
- "project": ["src/**/*.ts"]
61
+ "$schema": "https://unpkg.com/knip@next/schema.json",
62
+ "entry": ["index.{js,mjs,cjs,jsx,ts,tsx}", "src/index.{js,mjs,cjs,jsx,ts,tsx}"],
63
+ "project": ["**/*.{js,mjs,cjs,jsx,ts,tsx}"]
64
+ }
65
+ ```
66
+
67
+ If this matches your project, you don't need any configuration. But let's say you are using `.ts` files excusively and
68
+ have all source files in the `lib` directory:
69
+
70
+ ```json
71
+ {
72
+ "$schema": "https://unpkg.com/knip@next/schema.json",
73
+ "entry": ["lib/index.ts"],
74
+ "project": ["lib/**/*.ts"]
63
75
  }
64
76
  ```
65
77
 
@@ -79,7 +91,7 @@ const config: KnipConfig = {
79
91
  export default config;
80
92
  ```
81
93
 
82
- If you have, please see [workspaces & monorepos][1].
94
+ Please see [workspaces & monorepos][1] for more details about configuring them.
83
95
 
84
96
  Then run the checks with `npx knip`. Or first add this script to `package.json`:
85
97
 
@@ -124,7 +136,7 @@ Use `npm run knip` to analyze the project and output unused files, dependencies
124
136
  --h, --help Print this help text
125
137
  --V, version Print version
126
138
 
127
- (1) Issue types: files, dependencies, unlisted, exports, nsExports, classMembers, types, nsTypes, enumMembers, duplicates
139
+ (1) Issue types: files, dependencies, unlisted, unresolved, exports, nsExports, classMembers, types, nsTypes, enumMembers, duplicates
128
140
 
129
141
  Examples:
130
142
 
@@ -153,20 +165,19 @@ The report contains the following types of issues:
153
165
 
154
166
  - **Unused files**: did not find references to this file
155
167
  - **Unused dependencies**: did not find references to this dependency
156
- - **Unlisted or unresolved dependencies**: used dependencies, but not listed in package.json _(1)_
168
+ - **Unlisted dependencies**: used dependencies, but not listed in package.json
169
+ - **Unresolved imports**: import specifiers that could not be resolved
157
170
  - **Unused exports**: did not find references to this exported variable
158
- - **Unused exports in namespaces**: did not find direct references to this exported variable _(2)_
171
+ - **Unused exports in namespaces**: did not find direct references to this exported variable _(1)_
159
172
  - **Unused exported types**: did not find references to this exported type
160
- - **Unused exported types in namespaces**: did not find direct references to this exported variable _(2)_
173
+ - **Unused exported types in namespaces**: did not find direct references to this exported variable _(1)_
161
174
  - **Unused exported enum members**: did not find references to this member of the exported enum
162
175
  - **Unused exported class members**: did not find references to this member of the exported class
163
176
  - **Duplicate exports**: the same thing is exported more than once
164
177
 
165
178
  When an issue type has zero issues, it is not shown.
166
179
 
167
- _(1)_ This includes imports that could not be resolved.
168
-
169
- _(2)_ The variable or type is not referenced directly, and has become a member of a namespace. Knip can't find a
180
+ _(1)_ The variable or type is not referenced directly, and has become a member of a namespace. Knip can't find a
170
181
  reference to it, so you can _probably_ remove it.
171
182
 
172
183
  ### Output filters
@@ -174,9 +185,6 @@ reference to it, so you can _probably_ remove it.
174
185
  You can `--include` or `--exclude` any of the types to slice & dice the report to your needs. Alternatively, they can be
175
186
  added to the configuration (e.g. `"exclude": ["dependencies"]`).
176
187
 
177
- Knip finds issues of type `files`, `dependencies`, `unlisted` and `duplicates` very fast. Finding unused exports
178
- requires deeper analysis (`exports`, `nsExports`, `classMembers`, `types`, `nsTypes`, `enumMembers`).
179
-
180
188
  Use `--include` to report only specific issue types (the following example commands do the same):
181
189
 
182
190
  knip --include files --include dependencies
@@ -188,8 +196,8 @@ Use `--exclude` to ignore reports you're not interested in:
188
196
 
189
197
  Use `--dependencies` or `--exports` as shortcuts to combine groups of related types.
190
198
 
191
- Still not happy with the results? Getting too much output/false positives? The [FAQ][9] may be useful. Feel free to open
192
- an issue and I'm happy to look into it. Also see the next section on how to [ignore][10] certain false positives:
199
+ Still not happy with the results? Getting too much output/false positives? The [FAQ][10] may be useful. Feel free to
200
+ open an issue and I'm happy to look into it. Also see the next section on how to [ignore][11] certain false positives:
193
201
 
194
202
  ## Ignore
195
203
 
@@ -204,6 +212,8 @@ There are a few ways to tell Knip to ignore certain packages, binaries, dependen
204
212
  }
205
213
  ```
206
214
 
215
+ They can also be configured per workspace (except for `ignoreWorkspaces`).
216
+
207
217
  ## Now what?
208
218
 
209
219
  This is the fun part! Knip, knip, knip ✂️
@@ -213,6 +223,7 @@ As always, make sure to backup files or use Git before deleting files or making
213
223
  - Unused files can be removed.
214
224
  - Unused dependencies can be removed from `package.json`.
215
225
  - Unlisted dependencies should be added to `package.json`.
226
+ - Unresolved imports should be reviewed.
216
227
  - Unused exports and types: remove the `export` keyword in front of unused exports. Then you can see whether the
217
228
  variable or type is used within the same file. If this is not the case, it can be removed.
218
229
  - Duplicate exports can be removed so they're exported only once.
@@ -243,7 +254,13 @@ be part of the analysis. Here's an example:
243
254
  }
244
255
  ```
245
256
 
246
- Note that if you have a root workspace, it must be under `workspaces` and have the `"."` key like in the example.
257
+ It might be useful to run Knip first with no or little configuration to see where it needs different entry and/or
258
+ project files. The default configuration of each workspace is the same as in the introduction of this documentation.
259
+
260
+ Workspaces are sometimes also referred to as packages in a monorepo. Knip uses the term workspaces exclusively to
261
+ indicate the directories that have a `package.json`.
262
+
263
+ Root workspaces must be under `workspaces` and have the `"."` key like in the example.
247
264
 
248
265
  Knip supports workspaces as defined in three possible locations:
249
266
 
@@ -251,8 +268,6 @@ Knip supports workspaces as defined in three possible locations:
251
268
  - In the `workspaces.packages` array in `package.json`.
252
269
  - In the `packages` array in `pnpm-workspace.yaml`.
253
270
 
254
- Every directory with a match in `workspaces` of `knip.json` is part of the analysis.
255
-
256
271
  Extra "workspaces" not configured as a workspace in the root `package.json` can be configured as well, Knip is happy to
257
272
  analyze unused dependencies and exports from any directory with a `package.json`.
258
273
 
@@ -260,42 +275,44 @@ Here's some example output when running Knip in a workspace:
260
275
 
261
276
  <img src="./assets/screenshot-workspaces.png" alt="example output in workspaces" width="578">
262
277
 
278
+ Use `--debug` to get more verbose output.
279
+
263
280
  ## Plugins
264
281
 
265
282
  Knip contains a growing list of plugins:
266
283
 
267
- - [Babel][11]
268
- - [Capacitor][12]
269
- - [Changesets][13]
270
- - [commitlint][14]
271
- - [Cypress][15]
272
- - [ESLint][16]
273
- - [Gatsby][17]
274
- - [GitHub Actions][18]
275
- - [husky][19]
276
- - [Jest][20]
277
- - [Lefthook][21]
278
- - [lint-staged][22]
279
- - [markdownlint][23]
280
- - [Mocha][24]
281
- - [Next.js][25]
282
- - [npm-package-json-lint][26]
283
- - [Nx][27]
284
- - [nyc][28]
285
- - [Playwright][29]
286
- - [PostCSS][30]
287
- - [Prettier][31]
288
- - [Release It][32]
289
- - [Remark][33]
290
- - [Remix][34]
291
- - [Rollup][35]
292
- - [Sentry][36]
293
- - [Storybook][37]
294
- - [Stryker][38]
295
- - [TypeDoc][39]
296
- - [TypeScript][40]
297
- - [Vitest][41]
298
- - [Webpack][42]
284
+ - [Babel][12]
285
+ - [Capacitor][13]
286
+ - [Changesets][14]
287
+ - [commitlint][15]
288
+ - [Cypress][16]
289
+ - [ESLint][17]
290
+ - [Gatsby][18]
291
+ - [GitHub Actions][19]
292
+ - [husky][20]
293
+ - [Jest][21]
294
+ - [Lefthook][22]
295
+ - [lint-staged][23]
296
+ - [markdownlint][24]
297
+ - [Mocha][25]
298
+ - [Next.js][26]
299
+ - [npm-package-json-lint][27]
300
+ - [Nx][28]
301
+ - [nyc][29]
302
+ - [Playwright][30]
303
+ - [PostCSS][31]
304
+ - [Prettier][32]
305
+ - [Release It][33]
306
+ - [Remark][34]
307
+ - [Remix][35]
308
+ - [Rollup][36]
309
+ - [Sentry][37]
310
+ - [Storybook][38]
311
+ - [Stryker][39]
312
+ - [TypeDoc][40]
313
+ - [TypeScript][41]
314
+ - [Vitest][42]
315
+ - [Webpack][43]
299
316
 
300
317
  Plugins are automatically activated. Each plugin is automatically enabled based on simple heuristics. Most of them check
301
318
  whether one or one of a few (dev) dependencies are listed in `package.json`. Once enabled, they add a set of
@@ -303,7 +320,7 @@ configuration and/or entry files for Knip to analyze. These defaults can be over
303
320
 
304
321
  Most plugins use one or both of the following file types:
305
322
 
306
- - `config` - custom dependency resolvers are applied to the [config files][43]
323
+ - `config` - custom dependency resolvers are applied to the [config files][44]
307
324
  - `entry` - files to include with the analysis of the rest of the source code
308
325
 
309
326
  See each plugin's documentation for its default values.
@@ -328,6 +345,14 @@ rest to find which of those dependencies are unused or missing.
328
345
  Other configuration files use `require` or `import` statements to use dependencies, so they can be analyzed like the
329
346
  rest of the source files. These configuration files are also considered `entry` files.
330
347
 
348
+ For plugins related to test files, it's good to know that the following glob patterns are always included by default:
349
+
350
+ - `**/*.{test,spec}.{js,jsx,ts,tsx,mjs,cjs}`
351
+ - `**/__tests__/**/*.{js,jsx,ts,tsx,mjs,cjs}`
352
+ - `**/test/**/*.{js,jsx,ts,tsx,mjs,cjs}`
353
+
354
+ Files matching these patterns are excluded in [production mode][45].
355
+
331
356
  ### Disable a plugin
332
357
 
333
358
  In case a plugin causes issues, it can be disabled by using `false` as its value (e.g. `"webpack": false`).
@@ -339,6 +364,25 @@ get started:
339
364
 
340
365
  npm run create-plugin -- --name [myplugin]
341
366
 
367
+ ## Compilers
368
+
369
+ Knip v2 introduces compilers which allows to include files that are not JavaScript or TypeScript in the process of
370
+ finding unused or missing dependencies.
371
+
372
+ Currently this is only supported by using `knip.js` or `knip.ts`. Provide a `compilers` object in the configuration
373
+ where each key represents the extension and the value is a function that takes the contents of these files as input and
374
+ returns JavaScript or TypeScript as output. For example:
375
+
376
+ ```js
377
+ import { compileSync } from 'mdx-js/mdx';
378
+
379
+ export default {
380
+ compilers: {
381
+ mdx: compileSync,
382
+ },
383
+ };
384
+ ```
385
+
342
386
  ## Production Mode
343
387
 
344
388
  The default mode for Knip is holistic and targets all project code, including configuration files and tests. Test files
@@ -387,7 +431,7 @@ aliases. They can be configured manually:
387
431
 
388
432
  ```json
389
433
  {
390
- "$schema": "https://unpkg.com/knip@1/schema.json",
434
+ "$schema": "https://unpkg.com/knip@next/schema.json",
391
435
  "paths": {
392
436
  "@lib": ["./lib/index.ts"],
393
437
  "@lib/*": ["./lib/*"]
@@ -404,10 +448,10 @@ Each workspace can also have its own `paths` configured. Note that Knip `paths`
404
448
 
405
449
  Knip provides the following built-in reporters:
406
450
 
407
- - [`codeowners`][44]
408
- - [`compact`][45]
409
- - [`json`][46]
410
- - [`symbol`][47] (default)
451
+ - codeowners
452
+ - compact
453
+ - json
454
+ - symbol
411
455
 
412
456
  The `compact` reporter shows the sorted files first, and then a list of symbols:
413
457
 
@@ -434,13 +478,12 @@ type ReporterOptions = {
434
478
 
435
479
  The data can then be used to write issues to `stdout`, a JSON or CSV file, or sent to a service.
436
480
 
437
- Find more details and ideas in [custom reporters][48].
481
+ Find more details and ideas in [custom reporters][46].
438
482
 
439
- ## Libraries and "unused" exports
483
+ ## Public exports
440
484
 
441
- Libraries and applications are identical when it comes to files and dependencies: whatever is unused should be removed.
442
- Yet libraries usually have exports meant to be used by other libraries or applications. Such public variables and types
443
- in libraries can be marked with the JSDoc `@public` tag:
485
+ Sometimes a file that's not an entry file has one or more exports that are public, and should not be reported as unused.
486
+ Such variables and types can be marked with the JSDoc `@public` tag:
444
487
 
445
488
  ```js
446
489
  /**
@@ -481,7 +524,7 @@ an issue.
481
524
 
482
525
  When the list of unused files is too long, this means the gap between the set of `entry` and the set of `project` files
483
526
  needs tweaking. The gap can be narrowed down by increasing the `entry` files or reducing the `project` files, for
484
- instance by ignoring specific folders that are not related to the source code imported by the `entry` files.
527
+ instance by ignoring specific directories that are not related to the source code imported by the `entry` files.
485
528
 
486
529
  #### Too many unused dependencies
487
530
 
@@ -492,7 +535,7 @@ When unused dependencies are related to dependencies having a Knip [plugin][1],
492
535
  for that dependency are at custom locations. The default values are at the plugin's documentation, and can be overridden
493
536
  to match the custom location(s).
494
537
 
495
- When the dependencies don't have a Knip plugin yet, please file an issue or [create a new plugin][49].
538
+ When the dependencies don't have a Knip plugin yet, please file an issue or [create a new plugin][47].
496
539
 
497
540
  #### Too many unused exports
498
541
 
@@ -500,7 +543,7 @@ When the project is a library and the exports are meant to be used by consumers
500
543
 
501
544
  1. By default, unused exports of `entry` files are not reported. You could re-export from an existing entry file, or
502
545
  add the containing file to the `entry` array in the configuration.
503
- 2. The exported values or types can be marked [using the JSDoc `@public` tag][50].
546
+ 2. The exported values or types can be marked [using the JSDoc `@public` tag][48]
504
547
 
505
548
  ### How to start using Knip in CI while having too many issues to sort out?
506
549
 
@@ -520,22 +563,23 @@ All of this is hiding problems, so please make sure to plan for fixing them and/
520
563
 
521
564
  This table is an ongoing comparison. Based on their docs (please report any mistakes):
522
565
 
523
- | Feature | **knip** | [depcheck][51] | [unimported][52] | [ts-unused-exports][53] | [ts-prune][54] |
524
- | :--------------------------------- | :------: | :------------: | :--------------: | :---------------------: | :------------: |
525
- | Unused files | ✅ | - | ✅ | - | - |
526
- | Unused dependencies | ✅ | ✅ | ✅ | - | - |
527
- | Unlisted dependencies | ✅ | ✅ | ✅ | - | - |
528
- | [Plugins][1] | ✅ | ✅ | ❌ | - | - |
529
- | Unused exports | ✅ | - | - | | |
530
- | Unused class members | ✅ | - | - | - | - |
531
- | Unused enum members | ✅ | - | - | - | - |
532
- | Duplicate exports | ✅ | - | - | | |
533
- | Search namespaces | ✅ | - | - | | ❌ |
534
- | Custom reporters | ✅ | - | - | - | - |
535
- | JavaScript support | ✅ | | | - | - |
536
- | Configure entry files | ✅ | | ✅ | | |
537
- | [Support workspaces/monorepos][52] | ✅ | ❌ | | - | - |
538
- | ESLint plugin available | - | - | - | | - |
566
+ | Feature | **knip** | [depcheck][49] | [unimported][50] | [ts-unused-exports][51] | [ts-prune][52] |
567
+ | :-------------------------------- | :------: | :------------: | :--------------: | :---------------------: | :------------: |
568
+ | Unused files | ✅ | - | ✅ | - | - |
569
+ | Unused dependencies | ✅ | ✅ | ✅ | - | - |
570
+ | Unlisted dependencies | ✅ | ✅ | ✅ | - | - |
571
+ | [Plugins][2] | ✅ | ✅ | ❌ | - | - |
572
+ | [Compilers][3] | ✅ | - | - | - | - |
573
+ | Unused exports | ✅ | - | - | | |
574
+ | Unused class members | ✅ | - | - | - | - |
575
+ | Unused enum members | ✅ | - | - | - | - |
576
+ | Duplicate exports | ✅ | - | - | | ❌ |
577
+ | Search namespaces | ✅ | - | - | | |
578
+ | Custom reporters | ✅ | - | - | - | - |
579
+ | JavaScript support | ✅ | | ✅ | - | - |
580
+ | Configure entry files | ✅ | ❌ | | | |
581
+ | [Support workspaces/monorepos][1] | | | | - | - |
582
+ | ESLint plugin available | - | - | - | ✅ | - |
539
583
 
540
584
  ✅ = Supported, ❌ = Not supported, - = Out of scope
541
585
 
@@ -555,7 +599,7 @@ The following commands are similar:
555
599
  unimported
556
600
  knip --production --dependencies --include files
557
601
 
558
- Also see [production mode][55].
602
+ Also see [production mode][45].
559
603
 
560
604
  ### ts-unused-exports
561
605
 
@@ -573,13 +617,6 @@ The following commands are similar:
573
617
  knip --include exports,types
574
618
  knip --exports # Adds unused exports/types in namespaces and unused enum/class members
575
619
 
576
- ## TypeScript language services
577
-
578
- TypeScript language services could play a major role in most of the "unused" areas, as they have an overview of the
579
- project as a whole. This powers things in VS Code like "Find references" or the "Module "./some" declares 'Thing'
580
- locally, but it is not exported" message. I think features like "duplicate exports" or "custom dependency resolvers" are
581
- userland territory, much like code linters.
582
-
583
620
  ## Knip?!
584
621
 
585
622
  Knip is Dutch for a "cut". A Dutch expression is "to be ge**knip**t for something", which means to be perfectly suited
@@ -587,56 +624,53 @@ for the job. I'm motivated to make knip perfectly suited for the job of cutting
587
624
 
588
625
  [1]: #workspaces--monorepos
589
626
  [2]: #plugins
590
- [3]: #reporters
591
- [4]: #custom-reporters
592
- [5]: https://labs.openai.com/s/xZQACaLepaKya0PRUPtIN5dC
593
- [6]: ./assets/cow-with-orange-scissors-van-gogh-style.webp
594
- [7]: ./docs/migration-to-v1.md
595
- [8]: https://github.com/webpro/knip/issues
596
- [9]: #faq
597
- [10]: #ignore
598
- [11]: ./src/plugins/babel
599
- [12]: ./src/plugins/capacitor
600
- [13]: ./src/plugins/changesets
601
- [14]: ./src/plugins/commitlint
602
- [15]: ./src/plugins/cypress
603
- [16]: ./src/plugins/eslint
604
- [17]: ./src/plugins/gatsby
605
- [18]: ./src/plugins/github-actions
606
- [19]: ./src/plugins/husky
607
- [20]: ./src/plugins/jest
608
- [21]: ./src/plugins/lefthook
609
- [22]: ./src/plugins/lint-staged
610
- [23]: ./src/plugins/markdownlint
611
- [24]: ./src/plugins/mocha
612
- [25]: ./src/plugins/next
613
- [26]: ./src/plugins/npm-package-json-lint
614
- [27]: ./src/plugins/nx
615
- [28]: ./src/plugins/nyc
616
- [29]: ./src/plugins/playwright
617
- [30]: ./src/plugins/postcss
618
- [31]: ./src/plugins/prettier
619
- [32]: ./src/plugins/release-it
620
- [33]: ./src/plugins/remark
621
- [34]: ./src/plugins/remix
622
- [35]: ./src/plugins/rollup
623
- [36]: ./src/plugins/sentry
624
- [37]: ./src/plugins/storybook
625
- [38]: ./src/plugins/stryker
626
- [39]: ./src/plugins/typedoc
627
- [40]: ./src/plugins/typescript
628
- [41]: ./src/plugins/vitest
629
- [42]: ./src/plugins/webpack
630
- [43]: #config
631
- [44]: #code-owners
632
- [45]: #compact
633
- [46]: #json
634
- [47]: #symbol-default
635
- [48]: ./docs/custom-reporters.md
636
- [49]: #create-a-new-plugin
637
- [50]: #libraries-and-unused-exports
638
- [51]: https://github.com/depcheck/depcheck
639
- [52]: https://github.com/smeijer/unimported
640
- [53]: https://github.com/pzavolinsky/ts-unused-exports
641
- [54]: https://github.com/nadeesha/ts-prune
642
- [55]: #production-mode
627
+ [3]: #compilers
628
+ [4]: #reporters
629
+ [5]: #custom-reporters
630
+ [6]: https://labs.openai.com/s/xZQACaLepaKya0PRUPtIN5dC
631
+ [7]: ./assets/cow-with-orange-scissors-van-gogh-style.webp
632
+ [8]: https://github.com/webpro/knip/issues/73
633
+ [9]: https://github.com/webpro/knip/issues
634
+ [10]: #faq
635
+ [11]: #ignore
636
+ [12]: ./src/plugins/babel
637
+ [13]: ./src/plugins/capacitor
638
+ [14]: ./src/plugins/changesets
639
+ [15]: ./src/plugins/commitlint
640
+ [16]: ./src/plugins/cypress
641
+ [17]: ./src/plugins/eslint
642
+ [18]: ./src/plugins/gatsby
643
+ [19]: ./src/plugins/github-actions
644
+ [20]: ./src/plugins/husky
645
+ [21]: ./src/plugins/jest
646
+ [22]: ./src/plugins/lefthook
647
+ [23]: ./src/plugins/lint-staged
648
+ [24]: ./src/plugins/markdownlint
649
+ [25]: ./src/plugins/mocha
650
+ [26]: ./src/plugins/next
651
+ [27]: ./src/plugins/npm-package-json-lint
652
+ [28]: ./src/plugins/nx
653
+ [29]: ./src/plugins/nyc
654
+ [30]: ./src/plugins/playwright
655
+ [31]: ./src/plugins/postcss
656
+ [32]: ./src/plugins/prettier
657
+ [33]: ./src/plugins/release-it
658
+ [34]: ./src/plugins/remark
659
+ [35]: ./src/plugins/remix
660
+ [36]: ./src/plugins/rollup
661
+ [37]: ./src/plugins/sentry
662
+ [38]: ./src/plugins/storybook
663
+ [39]: ./src/plugins/stryker
664
+ [40]: ./src/plugins/typedoc
665
+ [41]: ./src/plugins/typescript
666
+ [42]: ./src/plugins/vitest
667
+ [43]: ./src/plugins/webpack
668
+ [44]: #config
669
+ [45]: #production-mode
670
+ [46]: ./docs/custom-reporters.md
671
+ [47]: #create-a-new-plugin
672
+ [48]: #public-exports
673
+ [49]: https://github.com/depcheck/depcheck
674
+ [50]: https://github.com/smeijer/unimported
675
+ [51]: https://github.com/pzavolinsky/ts-unused-exports
676
+ [52]: https://github.com/nadeesha/ts-prune
@@ -17,10 +17,10 @@ import { resolveIncludedIssueTypes } from './util/resolve-included-issue-types.j
17
17
  import { byPathDepth } from './util/workspace.js';
18
18
  const { config: rawConfigArg, workspace: rawWorkspaceArg, include = [], exclude = [], dependencies = false, exports = false, } = parsedArgs.values;
19
19
  const getDefaultWorkspaceConfig = (extensions) => {
20
- const extsGlobStr = [...DEFAULT_EXTENSIONS, ...(extensions ?? [])].map(ext => ext.slice(1)).join(',');
20
+ const exts = [...DEFAULT_EXTENSIONS, ...(extensions ?? [])].map(ext => ext.slice(1)).join(',');
21
21
  return {
22
- entry: [`index.{${extsGlobStr}}!`, `src/index.{${extsGlobStr}}!`],
23
- project: [`**/*.{${extsGlobStr}}!`],
22
+ entry: [`index.{${exts}}!`, `src/index.{${exts}}!`],
23
+ project: [`**/*.{${exts}}!`],
24
24
  paths: {},
25
25
  ignore: [],
26
26
  ignoreBinaries: [],
package/dist/constants.js CHANGED
@@ -1,7 +1,11 @@
1
1
  export const ROOT_WORKSPACE_NAME = '.';
2
2
  export const KNIP_CONFIG_LOCATIONS = ['knip.json', 'knip.jsonc', '.knip.json', '.knip.jsonc', 'knip.ts', 'knip.js'];
3
3
  export const DEFAULT_EXTENSIONS = ['.js', '.mjs', '.cjs', '.jsx', '.ts', '.tsx'];
4
- export const TEST_FILE_PATTERNS = ['**/*.{test,spec}.{js,jsx,ts,tsx}', '**/__tests__/**/*.{js,jsx,ts,tsx}'];
4
+ export const TEST_FILE_PATTERNS = [
5
+ '**/*.{test,spec}.{js,jsx,ts,tsx,mjs,cjs}',
6
+ '**/__tests__/**/*.{js,jsx,ts,tsx,mjs,cjs}',
7
+ '**/test/**/*.{js,jsx,ts,tsx,mjs,cjs}',
8
+ ];
5
9
  export const IGNORED_GLOBAL_BINARIES = [
6
10
  'bun',
7
11
  'deno',
@@ -3,5 +3,5 @@ export declare const NAME = "Jest";
3
3
  export declare const ENABLERS: string[];
4
4
  export declare const isEnabled: IsPluginEnabledCallback;
5
5
  export declare const CONFIG_FILE_PATTERNS: string[];
6
- export declare const ENTRY_FILE_PATTERNS: string[];
6
+ export declare const ENTRY_FILE_PATTERNS: never[];
7
7
  export declare const findDependencies: GenericPluginCallback;
@@ -9,7 +9,7 @@ export const NAME = 'Jest';
9
9
  export const ENABLERS = ['jest'];
10
10
  export const isEnabled = ({ dependencies }) => hasDependency(dependencies, ENABLERS);
11
11
  export const CONFIG_FILE_PATTERNS = ['jest.config.{js,ts,mjs,cjs,json}'];
12
- export const ENTRY_FILE_PATTERNS = ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'];
12
+ export const ENTRY_FILE_PATTERNS = [];
13
13
  const join = (base, id) => (isAbsolute(id) ? id : path.join(path.dirname(base), id));
14
14
  const resolveExtensibleConfig = async (configFilePath) => {
15
15
  const config = await _load(configFilePath);
@@ -3,5 +3,5 @@ export declare const NAME = "Mocha";
3
3
  export declare const ENABLERS: string[];
4
4
  export declare const isEnabled: IsPluginEnabledCallback;
5
5
  export declare const CONFIG_FILE_PATTERNS: string[];
6
- export declare const ENTRY_FILE_PATTERNS: string[];
6
+ export declare const ENTRY_FILE_PATTERNS: never[];
7
7
  export declare const findDependencies: GenericPluginCallback;
@@ -6,7 +6,7 @@ export const NAME = 'Mocha';
6
6
  export const ENABLERS = ['mocha'];
7
7
  export const isEnabled = ({ dependencies }) => hasDependency(dependencies, ENABLERS);
8
8
  export const CONFIG_FILE_PATTERNS = ['.mocharc.{js,cjs,json,jsonc,yml,yaml}', 'package.json'];
9
- export const ENTRY_FILE_PATTERNS = ['test/**/*.{js,cjs,mjs}'];
9
+ export const ENTRY_FILE_PATTERNS = [];
10
10
  const findMochaDependencies = async (configFilePath, { manifest }) => {
11
11
  const config = configFilePath.endsWith('package.json') ? manifest.mocha : await _load(configFilePath);
12
12
  if (config) {
@@ -2,4 +2,4 @@ import { hasDependency } from '../../util/plugin.js';
2
2
  export const NAME = 'Playwright';
3
3
  export const ENABLERS = ['@playwright/test'];
4
4
  export const isEnabled = ({ dependencies }) => hasDependency(dependencies, ENABLERS);
5
- export const ENTRY_FILE_PATTERNS = ['playwright.config.{js,ts}', '.*{test,spec}.{js,ts,mjs}'];
5
+ export const ENTRY_FILE_PATTERNS = ['playwright.config.{js,ts}'];
@@ -12,6 +12,6 @@ export const CONFIG_FILE_PATTERNS = [
12
12
  ];
13
13
  const findPluginDependencies = async (configFilePath) => {
14
14
  const config = await _load(configFilePath);
15
- return config?.plugins ?? [];
15
+ return config?.plugin ?? [];
16
16
  };
17
17
  export const findDependencies = timerify(findPluginDependencies);
@@ -1,3 +1,3 @@
1
1
  export type PluginConfig = {
2
- plugins?: string[];
2
+ plugin?: string[];
3
3
  };
@@ -3,5 +3,5 @@ export declare const NAME = "Vitest";
3
3
  export declare const ENABLERS: string[];
4
4
  export declare const isEnabled: IsPluginEnabledCallback;
5
5
  export declare const CONFIG_FILE_PATTERNS: string[];
6
- export declare const ENTRY_FILE_PATTERNS: string[];
6
+ export declare const ENTRY_FILE_PATTERNS: never[];
7
7
  export declare const findDependencies: GenericPluginCallback;
@@ -7,7 +7,7 @@ export const NAME = 'Vitest';
7
7
  export const ENABLERS = ['vitest'];
8
8
  export const isEnabled = ({ dependencies }) => hasDependency(dependencies, ENABLERS);
9
9
  export const CONFIG_FILE_PATTERNS = ['vitest.config.ts', 'vite.config.ts'];
10
- export const ENTRY_FILE_PATTERNS = ['**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'];
10
+ export const ENTRY_FILE_PATTERNS = [];
11
11
  const findVitestDependencies = async (configFilePath) => {
12
12
  const config = await _load(configFilePath);
13
13
  if (!config || !config.test)
@@ -1,4 +1,4 @@
1
- export declare const helpText = "\u2702\uFE0F Find unused files, dependencies and exports in your JavaScript and TypeScript projects\n\nUsage: knip [options]\n\nOptions:\n -c, --config [file] Configuration file path (default: [.]knip.json[c], knip.js, knip.ts or package.json#knip)\n -t, --tsConfig [file] TypeScript configuration path (default: tsconfig.json)\n --production Analyze only production source files (e.g. no tests, devDependencies, exported types)\n --strict Consider only direct dependencies of workspace (not devDependencies, not other workspaces)\n --workspace Analyze a single workspace (default: analyze all configured workspaces)\n --ignore Ignore files matching this glob pattern, can be repeated\n --no-gitignore Don't use .gitignore\n --include Report only provided issue type(s), can be comma-separated or repeated (1)\n --exclude Exclude provided issue type(s) from report, can be comma-separated or repeated (1)\n --dependencies Shortcut for --include dependencies,unlisted\n --exports Shortcut for --include exports,nsExports,classMembers,types,nsTypes,enumMembers,duplicates\n --no-progress Don't show dynamic progress updates\n --reporter Select reporter: symbols, compact, codeowners, json (default: symbols)\n --reporter-options Pass extra options to the reporter (as JSON string, see example)\n --no-exit-code Always exit with code zero (0)\n --max-issues Maximum number of issues before non-zero exit code (default: 0)\n --debug Show debug output\n --debug-file-filter Filter for files in debug output (regex as string)\n --performance Measure count and running time of expensive functions and display stats table\n --h, --help Print this help text\n --V, version Print version\n\n(1) Issue types: files, dependencies, unlisted, exports, nsExports, classMembers, types, nsTypes, enumMembers, duplicates\n\nExamples:\n\n$ knip\n$ knip --production\n$ knip --workspace packages/client --include files,dependencies\n$ knip -c ./config/knip.json --reporter compact\n$ knip --reporter codeowners --reporter-options '{\"path\":\".github/CODEOWNERS\"}'\n$ knip --debug --debug-file-filter '(specific|particular)-module'\n\nMore documentation and bug reports: https://github.com/webpro/knip";
1
+ export declare const helpText = "\u2702\uFE0F Find unused files, dependencies and exports in your JavaScript and TypeScript projects\n\nUsage: knip [options]\n\nOptions:\n -c, --config [file] Configuration file path (default: [.]knip.json[c], knip.js, knip.ts or package.json#knip)\n -t, --tsConfig [file] TypeScript configuration path (default: tsconfig.json)\n --production Analyze only production source files (e.g. no tests, devDependencies, exported types)\n --strict Consider only direct dependencies of workspace (not devDependencies, not other workspaces)\n --workspace Analyze a single workspace (default: analyze all configured workspaces)\n --ignore Ignore files matching this glob pattern, can be repeated\n --no-gitignore Don't use .gitignore\n --include Report only provided issue type(s), can be comma-separated or repeated (1)\n --exclude Exclude provided issue type(s) from report, can be comma-separated or repeated (1)\n --dependencies Shortcut for --include dependencies,unlisted\n --exports Shortcut for --include exports,nsExports,classMembers,types,nsTypes,enumMembers,duplicates\n --no-progress Don't show dynamic progress updates\n --reporter Select reporter: symbols, compact, codeowners, json (default: symbols)\n --reporter-options Pass extra options to the reporter (as JSON string, see example)\n --no-exit-code Always exit with code zero (0)\n --max-issues Maximum number of issues before non-zero exit code (default: 0)\n --debug Show debug output\n --debug-file-filter Filter for files in debug output (regex as string)\n --performance Measure count and running time of expensive functions and display stats table\n --h, --help Print this help text\n --V, version Print version\n\n(1) Issue types: files, dependencies, unlisted, unresolved, exports, nsExports, classMembers, types, nsTypes, enumMembers, duplicates\n\nExamples:\n\n$ knip\n$ knip --production\n$ knip --workspace packages/client --include files,dependencies\n$ knip -c ./config/knip.json --reporter compact\n$ knip --reporter codeowners --reporter-options '{\"path\":\".github/CODEOWNERS\"}'\n$ knip --debug --debug-file-filter '(specific|particular)-module'\n\nMore documentation and bug reports: https://github.com/webpro/knip";
2
2
  declare const _default: {
3
3
  values: {
4
4
  config: string | undefined;
@@ -26,7 +26,7 @@ Options:
26
26
  --h, --help Print this help text
27
27
  --V, version Print version
28
28
 
29
- (1) Issue types: files, dependencies, unlisted, exports, nsExports, classMembers, types, nsTypes, enumMembers, duplicates
29
+ (1) Issue types: files, dependencies, unlisted, unresolved, exports, nsExports, classMembers, types, nsTypes, enumMembers, duplicates
30
30
 
31
31
  Examples:
32
32
 
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const version = "2.0.0-alpha.0";
1
+ export declare const version = "2.0.0-alpha.2";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = '2.0.0-alpha.0';
1
+ export const version = '2.0.0-alpha.2';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "knip",
3
- "version": "2.0.0-alpha.0",
3
+ "version": "2.0.0-alpha.2",
4
4
  "description": "Find unused files, dependencies and exports in your TypeScript and JavaScript projects",
5
5
  "homepage": "https://github.com/webpro/knip",
6
6
  "repository": "github:webpro/knip",
@@ -22,7 +22,7 @@
22
22
  "lint": "eslint src",
23
23
  "lint:fix": "eslint src --fix",
24
24
  "format": "prettier scripts src test --with-node-modules --write --config .prettierrc",
25
- "test": "globstar -- node --loader tsx --test \"test/**/*.test.ts\"",
25
+ "test": "globstar -- node --loader tsx --test \"tests/**/*.test.ts\"",
26
26
  "watch": "tsc --watch",
27
27
  "prebuild": "rm -rf dist",
28
28
  "build": "tsc",
@@ -30,6 +30,7 @@
30
30
  "docs:cli": "tsx ./scripts/update-cli-usage-in-readme.ts",
31
31
  "docs:plugins": "tsx ./scripts/generate-plugin-docs.ts",
32
32
  "docs:format": "remark README.md docs/*.md src/plugins/*/README.md -o",
33
+ "qa": "npm run lint && npm run build && npm run knip && npm run knip:production && npm test",
33
34
  "release": "release-it",
34
35
  "create-plugin": "tsx ./scripts/create-new-plugin.ts"
35
36
  },
@@ -57,24 +58,24 @@
57
58
  "zod": "^3.20.6"
58
59
  },
59
60
  "devDependencies": {
60
- "@jest/types": "29.4.3",
61
+ "@jest/types": "29.5.0",
61
62
  "@npmcli/package-json": "3.0.0",
62
63
  "@release-it/bumper": "4.0.2",
63
64
  "@types/eslint": "8.21.1",
64
65
  "@types/js-yaml": "4.0.5",
65
66
  "@types/micromatch": "4.0.2",
66
67
  "@types/minimist": "1.2.2",
67
- "@types/node": "18.14.2",
68
+ "@types/node": "18.14.6",
68
69
  "@types/npmcli__map-workspaces": "3.0.0",
69
70
  "@types/webpack": "5.28.0",
70
- "@typescript-eslint/eslint-plugin": "5.54.0",
71
- "@typescript-eslint/parser": "5.54.0",
71
+ "@typescript-eslint/eslint-plugin": "5.54.1",
72
+ "@typescript-eslint/parser": "5.54.1",
72
73
  "eslint": "8.35.0",
73
74
  "eslint-import-resolver-typescript": "3.5.3",
74
75
  "eslint-plugin-import": "2.27.5",
75
76
  "globstar": "1.0.0",
76
77
  "prettier": "2.8.4",
77
- "release-it": "^15.6.1",
78
+ "release-it": "^15.7.0",
78
79
  "remark-cli": "11.0.0",
79
80
  "remark-preset-webpro": "0.0.1",
80
81
  "tsx": "3.12.3",
package/schema.json CHANGED
@@ -129,17 +129,17 @@
129
129
  "title": "Files to ignore in the analysis.",
130
130
  "example": ["**/fixtures", "mocks"],
131
131
  "default": [],
132
- "$ref": "#/definitions/globPatterns",
133
- "ignoreBinaries": {
134
- "title": "Binaries to ignore",
135
- "examples": ["rm", "docker-compose", "curl"],
136
- "$ref": "#/definitions/list"
137
- },
138
- "ignoreDependencies": {
139
- "title": "Dependencies from package.json to ignore",
140
- "examples": ["husky", "lint-staged"],
141
- "$ref": "#/definitions/list"
142
- }
132
+ "$ref": "#/definitions/globPatterns"
133
+ },
134
+ "ignoreBinaries": {
135
+ "title": "Binaries to ignore",
136
+ "examples": ["rm", "docker-compose", "curl"],
137
+ "$ref": "#/definitions/list"
138
+ },
139
+ "ignoreDependencies": {
140
+ "title": "Dependencies from package.json to ignore",
141
+ "examples": ["husky", "lint-staged"],
142
+ "$ref": "#/definitions/list"
143
143
  }
144
144
  }
145
145
  },