knip 2.31.0 → 2.32.0

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 (100) hide show
  1. package/README.md +15 -24
  2. package/dist/ConfigurationValidator.d.ts +56 -0
  3. package/dist/ConfigurationValidator.js +1 -0
  4. package/dist/WorkspaceWorker.d.ts +8 -6
  5. package/dist/WorkspaceWorker.js +33 -59
  6. package/dist/binaries/index.js +1 -1
  7. package/dist/binaries/resolvers/c8.js +2 -1
  8. package/dist/binaries/resolvers/dotenv.js +2 -1
  9. package/dist/binaries/resolvers/fallback.js +2 -1
  10. package/dist/binaries/resolvers/nodemon.js +2 -1
  11. package/dist/binaries/resolvers/npx.js +2 -1
  12. package/dist/binaries/resolvers/nx.js +1 -1
  13. package/dist/binaries/resolvers/pnpm.js +1 -1
  14. package/dist/binaries/resolvers/rollup.js +2 -1
  15. package/dist/binaries/resolvers/yarn.js +1 -1
  16. package/dist/binaries/util.d.ts +0 -3
  17. package/dist/binaries/util.js +1 -3
  18. package/dist/constants.d.ts +0 -1
  19. package/dist/constants.js +0 -4
  20. package/dist/index.js +19 -18
  21. package/dist/plugins/ava/index.d.ts +1 -1
  22. package/dist/plugins/ava/index.js +23 -5
  23. package/dist/plugins/ava/types.d.ts +1 -0
  24. package/dist/plugins/babel/index.js +3 -1
  25. package/dist/plugins/capacitor/index.js +3 -1
  26. package/dist/plugins/changesets/index.js +3 -1
  27. package/dist/plugins/commitizen/index.d.ts +1 -0
  28. package/dist/plugins/commitizen/index.js +4 -1
  29. package/dist/plugins/commitlint/index.js +3 -1
  30. package/dist/plugins/cspell/index.js +3 -1
  31. package/dist/plugins/cypress/index.d.ts +3 -1
  32. package/dist/plugins/cypress/index.js +14 -4
  33. package/dist/plugins/drizzle/index.js +3 -5
  34. package/dist/plugins/eslint/helpers.js +2 -1
  35. package/dist/plugins/eslint/index.d.ts +1 -0
  36. package/dist/plugins/eslint/index.js +4 -1
  37. package/dist/plugins/gatsby/index.js +10 -2
  38. package/dist/plugins/github-actions/index.js +3 -1
  39. package/dist/plugins/husky/index.js +3 -1
  40. package/dist/plugins/index.d.ts +1 -0
  41. package/dist/plugins/index.js +1 -0
  42. package/dist/plugins/jest/index.d.ts +1 -1
  43. package/dist/plugins/jest/index.js +16 -7
  44. package/dist/plugins/lefthook/index.js +4 -2
  45. package/dist/plugins/lint-staged/index.d.ts +1 -0
  46. package/dist/plugins/lint-staged/index.js +4 -1
  47. package/dist/plugins/markdownlint/index.js +3 -1
  48. package/dist/plugins/mocha/index.d.ts +1 -0
  49. package/dist/plugins/mocha/index.js +10 -6
  50. package/dist/plugins/next/index.d.ts +2 -1
  51. package/dist/plugins/next/index.js +4 -0
  52. package/dist/plugins/node-test-runner/index.d.ts +6 -0
  53. package/dist/plugins/node-test-runner/index.js +17 -0
  54. package/dist/plugins/npm-package-json-lint/index.d.ts +1 -0
  55. package/dist/plugins/npm-package-json-lint/index.js +5 -2
  56. package/dist/plugins/nx/index.js +3 -1
  57. package/dist/plugins/nyc/index.js +3 -1
  58. package/dist/plugins/playwright/index.d.ts +3 -1
  59. package/dist/plugins/playwright/index.js +22 -2
  60. package/dist/plugins/playwright-ct/index.d.ts +3 -1
  61. package/dist/plugins/playwright-ct/index.js +13 -2
  62. package/dist/plugins/postcss/index.js +8 -6
  63. package/dist/plugins/prettier/index.js +3 -1
  64. package/dist/plugins/release-it/index.d.ts +1 -0
  65. package/dist/plugins/release-it/index.js +5 -2
  66. package/dist/plugins/remark/index.d.ts +1 -0
  67. package/dist/plugins/remark/index.js +10 -3
  68. package/dist/plugins/remix/index.d.ts +2 -1
  69. package/dist/plugins/remix/index.js +10 -0
  70. package/dist/plugins/rollup/index.d.ts +2 -1
  71. package/dist/plugins/rollup/index.js +7 -0
  72. package/dist/plugins/semantic-release/index.d.ts +1 -0
  73. package/dist/plugins/semantic-release/index.js +7 -2
  74. package/dist/plugins/sentry/index.d.ts +2 -1
  75. package/dist/plugins/sentry/index.js +7 -0
  76. package/dist/plugins/storybook/index.js +6 -2
  77. package/dist/plugins/stryker/index.js +9 -8
  78. package/dist/plugins/stylelint/index.js +3 -1
  79. package/dist/plugins/svelte/index.d.ts +2 -1
  80. package/dist/plugins/svelte/index.js +6 -0
  81. package/dist/plugins/typedoc/index.d.ts +1 -0
  82. package/dist/plugins/typedoc/index.js +5 -2
  83. package/dist/plugins/typescript/index.js +3 -1
  84. package/dist/plugins/vite/index.js +2 -2
  85. package/dist/plugins/vitest/index.d.ts +3 -3
  86. package/dist/plugins/vitest/index.js +16 -23
  87. package/dist/plugins/vitest/types.d.ts +1 -0
  88. package/dist/plugins/webpack/index.js +3 -1
  89. package/dist/types/config.d.ts +3 -2
  90. package/dist/types/plugins.d.ts +3 -3
  91. package/dist/util/compilers.d.ts +10 -0
  92. package/dist/util/object.d.ts +1 -0
  93. package/dist/util/object.js +1 -0
  94. package/dist/util/plugin.d.ts +1 -1
  95. package/dist/util/protocols.d.ts +9 -0
  96. package/dist/util/protocols.js +9 -0
  97. package/dist/version.d.ts +1 -1
  98. package/dist/version.js +1 -1
  99. package/package.json +2 -1
  100. package/schema.json +4 -0
package/README.md CHANGED
@@ -266,6 +266,7 @@ Knip contains a growing list of plugins:
266
266
  - [markdownlint][plugin-markdownlint]
267
267
  - [Mocha][plugin-mocha]
268
268
  - [Next.js][plugin-next]
269
+ - [Node.js Test Runner][plugin-node-test-runner]
269
270
  - [npm-package-json-lint][plugin-npm-package-json-lint]
270
271
  - [Nx][plugin-nx]
271
272
  - [nyc][plugin-nyc]
@@ -310,35 +311,28 @@ unused or missing.
310
311
  Other configuration files use `require` or `import` statements to use dependencies, so they don't need special handing
311
312
  and can be analyzed like any other source file. That's why these configuration files are also used as `entry` files.
312
313
 
313
- #### Override plugin configuration
314
+ #### Plugin configuration
314
315
 
315
- Usually, no custom configuration is required for plugins, but if your project uses custom file locations then Knip
316
- allows you to override any defaults. Let's take Cypress for example. By default it uses `cypress.config.js`, but your
317
- project uses `config/cypress.js`. Also, the default pattern for test files is `cypress/e2e/**/*.cy.js`, but your project
318
- has them at `e2e-tests/*.spec.ts`. Here's how to configure this:
316
+ Is a plugin not behaving as expected? There are a few options to modify its behavior:
319
317
 
320
- ```json
321
- {
322
- "cypress": {
323
- "entry": ["config/cypress.js", "e2e-tests/*.spec.js"]
324
- }
325
- }
326
- ```
327
-
328
- When overriding any plugin's configuration, the options object is fully replaced. Look at the plugin's page to find the
329
- default configuration and use that as a basis. Here's another example to override the test file pattern for Vitest:
318
+ - Disable a plugin by setting its value to `false` (.e.g `"webpack": false`)
319
+ - Force-enable a plugin by setting its value to `true`
320
+ - Override a plugin's `config` or `entry` location, for example:
330
321
 
331
322
  ```json
332
323
  {
333
- "vitest": {
334
- "config": ["vitest.config.ts"],
335
- "entry": ["**/*.vitest.ts"]
324
+ "mocha": {
325
+ "config": "config/mocha.config.js",
326
+ "entry": ["**/*.spec.js"]
336
327
  }
337
328
  }
338
329
  ```
339
330
 
340
- This plugin configuration can be set on root and workspace level. If set on root level, it can be disabled on workspace
341
- level by setting it to `false` there.
331
+ It's rarely necessary to override the `entry` patterns, since plugins also read custom test file patterns from the
332
+ tooling configuration (e.g. from the `include` option for Vitest or `testMatch` for Playwright).
333
+
334
+ Plugin configuration can be set on root and workspace level. If enabled on root level, it can be disabled on workspace
335
+ level by setting it to `false` there, and vice versa.
342
336
 
343
337
  #### Multi-project repositories
344
338
 
@@ -354,10 +348,6 @@ own Cypress configuration and test files. In that case, we could configure the C
354
348
  }
355
349
  ```
356
350
 
357
- #### Disable a plugin
358
-
359
- In case a plugin causes issues, it can be disabled by using `false` as its value (e.g. `"webpack": false`).
360
-
361
351
  #### Create a new plugin
362
352
 
363
353
  Getting false positives because a plugin is missing? Want to help out? Please read more at [writing a plugin][47]. This
@@ -920,6 +910,7 @@ Special thanks to the wonderful people who have contributed to this project:
920
910
  [plugin-markdownlint]: ./src/plugins/markdownlint
921
911
  [plugin-mocha]: ./src/plugins/mocha
922
912
  [plugin-next]: ./src/plugins/next
913
+ [plugin-node-test-runner]: ./src/plugins/node-test-runner
923
914
  [plugin-npm-package-json-lint]: ./src/plugins/npm-package-json-lint
924
915
  [plugin-nx]: ./src/plugins/nx
925
916
  [plugin-nyc]: ./src/plugins/nyc
@@ -281,6 +281,19 @@ export declare const ConfigurationValidator: z.ZodObject<{
281
281
  entry?: string | string[] | undefined;
282
282
  project?: string | string[] | undefined;
283
283
  }>]>>;
284
+ 'node-test-runner': z.ZodOptional<z.ZodUnion<[z.ZodBoolean, z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>, z.ZodObject<{
285
+ config: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>>;
286
+ entry: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>>;
287
+ project: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>>;
288
+ }, "strip", z.ZodTypeAny, {
289
+ config?: string | string[] | undefined;
290
+ entry?: string | string[] | undefined;
291
+ project?: string | string[] | undefined;
292
+ }, {
293
+ config?: string | string[] | undefined;
294
+ entry?: string | string[] | undefined;
295
+ project?: string | string[] | undefined;
296
+ }>]>>;
284
297
  'npm-package-json-lint': z.ZodOptional<z.ZodUnion<[z.ZodBoolean, z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>, z.ZodObject<{
285
298
  config: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>>;
286
299
  entry: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>>;
@@ -656,6 +669,11 @@ export declare const ConfigurationValidator: z.ZodObject<{
656
669
  entry?: string | string[] | undefined;
657
670
  project?: string | string[] | undefined;
658
671
  } | undefined;
672
+ 'node-test-runner'?: string | boolean | string[] | {
673
+ config?: string | string[] | undefined;
674
+ entry?: string | string[] | undefined;
675
+ project?: string | string[] | undefined;
676
+ } | undefined;
659
677
  'npm-package-json-lint'?: string | boolean | string[] | {
660
678
  config?: string | string[] | undefined;
661
679
  entry?: string | string[] | undefined;
@@ -863,6 +881,11 @@ export declare const ConfigurationValidator: z.ZodObject<{
863
881
  entry?: string | string[] | undefined;
864
882
  project?: string | string[] | undefined;
865
883
  } | undefined;
884
+ 'node-test-runner'?: string | boolean | string[] | {
885
+ config?: string | string[] | undefined;
886
+ entry?: string | string[] | undefined;
887
+ project?: string | string[] | undefined;
888
+ } | undefined;
866
889
  'npm-package-json-lint'?: string | boolean | string[] | {
867
890
  config?: string | string[] | undefined;
868
891
  entry?: string | string[] | undefined;
@@ -1216,6 +1239,19 @@ export declare const ConfigurationValidator: z.ZodObject<{
1216
1239
  entry?: string | string[] | undefined;
1217
1240
  project?: string | string[] | undefined;
1218
1241
  }>]>>;
1242
+ 'node-test-runner': z.ZodOptional<z.ZodUnion<[z.ZodBoolean, z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>, z.ZodObject<{
1243
+ config: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>>;
1244
+ entry: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>>;
1245
+ project: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>>;
1246
+ }, "strip", z.ZodTypeAny, {
1247
+ config?: string | string[] | undefined;
1248
+ entry?: string | string[] | undefined;
1249
+ project?: string | string[] | undefined;
1250
+ }, {
1251
+ config?: string | string[] | undefined;
1252
+ entry?: string | string[] | undefined;
1253
+ project?: string | string[] | undefined;
1254
+ }>]>>;
1219
1255
  'npm-package-json-lint': z.ZodOptional<z.ZodUnion<[z.ZodBoolean, z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>, z.ZodObject<{
1220
1256
  config: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>>;
1221
1257
  entry: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>>;
@@ -1606,6 +1642,11 @@ export declare const ConfigurationValidator: z.ZodObject<{
1606
1642
  entry?: string | string[] | undefined;
1607
1643
  project?: string | string[] | undefined;
1608
1644
  } | undefined;
1645
+ 'node-test-runner'?: string | boolean | string[] | {
1646
+ config?: string | string[] | undefined;
1647
+ entry?: string | string[] | undefined;
1648
+ project?: string | string[] | undefined;
1649
+ } | undefined;
1609
1650
  'npm-package-json-lint'?: string | boolean | string[] | {
1610
1651
  config?: string | string[] | undefined;
1611
1652
  entry?: string | string[] | undefined;
@@ -1807,6 +1848,11 @@ export declare const ConfigurationValidator: z.ZodObject<{
1807
1848
  entry?: string | string[] | undefined;
1808
1849
  project?: string | string[] | undefined;
1809
1850
  } | undefined;
1851
+ 'node-test-runner'?: string | boolean | string[] | {
1852
+ config?: string | string[] | undefined;
1853
+ entry?: string | string[] | undefined;
1854
+ project?: string | string[] | undefined;
1855
+ } | undefined;
1810
1856
  'npm-package-json-lint'?: string | boolean | string[] | {
1811
1857
  config?: string | string[] | undefined;
1812
1858
  entry?: string | string[] | undefined;
@@ -2029,6 +2075,11 @@ export declare const ConfigurationValidator: z.ZodObject<{
2029
2075
  entry?: string | string[] | undefined;
2030
2076
  project?: string | string[] | undefined;
2031
2077
  } | undefined;
2078
+ 'node-test-runner'?: string | boolean | string[] | {
2079
+ config?: string | string[] | undefined;
2080
+ entry?: string | string[] | undefined;
2081
+ project?: string | string[] | undefined;
2082
+ } | undefined;
2032
2083
  'npm-package-json-lint'?: string | boolean | string[] | {
2033
2084
  config?: string | string[] | undefined;
2034
2085
  entry?: string | string[] | undefined;
@@ -2230,6 +2281,11 @@ export declare const ConfigurationValidator: z.ZodObject<{
2230
2281
  entry?: string | string[] | undefined;
2231
2282
  project?: string | string[] | undefined;
2232
2283
  } | undefined;
2284
+ 'node-test-runner'?: string | boolean | string[] | {
2285
+ config?: string | string[] | undefined;
2286
+ entry?: string | string[] | undefined;
2287
+ project?: string | string[] | undefined;
2288
+ } | undefined;
2233
2289
  'npm-package-json-lint'?: string | boolean | string[] | {
2234
2290
  config?: string | string[] | undefined;
2235
2291
  entry?: string | string[] | undefined;
@@ -79,6 +79,7 @@ const pluginsSchema = z.object({
79
79
  markdownlint: pluginSchema,
80
80
  mocha: pluginSchema,
81
81
  next: pluginSchema,
82
+ 'node-test-runner': pluginSchema,
82
83
  'npm-package-json-lint': pluginSchema,
83
84
  nx: pluginSchema,
84
85
  nyc: pluginSchema,
@@ -31,19 +31,19 @@ export declare class WorkspaceWorker {
31
31
  hostDependencies: HostDependencies;
32
32
  installedBinaries: InstalledBinaries;
33
33
  hasTypesIncluded: Set<string>;
34
+ entryFilePatterns: Set<string>;
35
+ productionEntryFilePatterns: Set<string>;
34
36
  constructor({ name, dir, cwd, config, manifest, isProduction, isStrict, rootIgnore, negatedWorkspacePatterns, enabledPluginsInAncestors, }: WorkspaceManagerOptions);
35
37
  init(): Promise<void>;
36
38
  private setEnabledPlugins;
37
39
  private initReferencedDependencies;
38
40
  private getConfigForPlugin;
39
41
  getEntryFilePatterns(): string[];
40
- getProjectFilePatterns(): string[];
41
- getPluginEntryFilePatterns(isIncludeProductionEntryFiles?: boolean): string[];
42
+ getProjectFilePatterns(testFilePatterns: string[]): string[];
42
43
  getPluginProjectFilePatterns(): string[];
43
44
  getPluginConfigPatterns(): string[];
44
- getProductionEntryFilePatterns(): string[];
45
- getProductionProjectFilePatterns(): string[];
46
- getProductionPluginEntryFilePatterns(): string[];
45
+ getProductionEntryFilePatterns(negatedTestFilePatterns: string[]): string[];
46
+ getProductionProjectFilePatterns(negatedTestFilePatterns: string[]): string[];
47
47
  private getConfigurationFilePatterns;
48
48
  getIgnorePatterns(): string[];
49
49
  private findDependenciesByPlugins;
@@ -52,7 +52,9 @@ export declare class WorkspaceWorker {
52
52
  installedBinaries: InstalledBinaries;
53
53
  referencedDependencies: ReferencedDependencies;
54
54
  hasTypesIncluded: Set<string>;
55
- enabledPlugins: ("angular" | "ava" | "babel" | "capacitor" | "changesets" | "commitizen" | "commitlint" | "cspell" | "cypress" | "eslint" | "gatsby" | "husky" | "jest" | "lefthook" | "markdownlint" | "mocha" | "next" | "nx" | "nyc" | "playwright" | "postcss" | "prettier" | "remark" | "remix" | "rollup" | "sentry" | "storybook" | "stryker" | "stylelint" | "tailwind" | "typedoc" | "typescript" | "vite" | "vitest" | "webpack" | "drizzle" | "githubActions" | "lintStaged" | "npmPackageJsonLint" | "playwrightCt" | "releaseIt" | "semanticRelease" | "svelte")[];
55
+ enabledPlugins: ("angular" | "ava" | "babel" | "capacitor" | "changesets" | "commitizen" | "commitlint" | "cspell" | "cypress" | "eslint" | "gatsby" | "husky" | "jest" | "lefthook" | "markdownlint" | "mocha" | "next" | "nx" | "nyc" | "playwright" | "postcss" | "prettier" | "remark" | "remix" | "rollup" | "sentry" | "storybook" | "stryker" | "stylelint" | "tailwind" | "typedoc" | "typescript" | "vite" | "vitest" | "webpack" | "drizzle" | "githubActions" | "lintStaged" | "nodeTestRunner" | "npmPackageJsonLint" | "playwrightCt" | "releaseIt" | "semanticRelease" | "svelte")[];
56
+ entryFilePatterns: string[];
57
+ productionEntryFilePatterns: string[];
56
58
  }>;
57
59
  }
58
60
  export {};
@@ -1,11 +1,10 @@
1
- import { TEST_FILE_PATTERNS } from './constants.js';
2
1
  import * as npm from './manifest/index.js';
3
2
  import * as plugins from './plugins/index.js';
4
3
  import { debugLogArray, debugLogObject } from './util/debug.js';
5
4
  import { _pureGlob, negate, hasProductionSuffix, hasNoProductionSuffix, prependDirToPattern } from './util/glob.js';
6
- import { getKeysByValue } from './util/object.js';
5
+ import { get, getKeysByValue } from './util/object.js';
7
6
  import { join, toPosix } from './util/path.js';
8
- const negatedTestFilePatterns = TEST_FILE_PATTERNS.map(negate);
7
+ import { fromEntryPattern, fromProductionEntryPattern, isEntryPattern, isProductionEntryPattern, } from './util/protocols.js';
9
8
  const nullConfig = { config: null, entry: null, project: null };
10
9
  export class WorkspaceWorker {
11
10
  name;
@@ -24,6 +23,8 @@ export class WorkspaceWorker {
24
23
  hostDependencies = new Map();
25
24
  installedBinaries = new Map();
26
25
  hasTypesIncluded = new Set();
26
+ entryFilePatterns = new Set();
27
+ productionEntryFilePatterns = new Set();
27
28
  constructor({ name, dir, cwd, config, manifest, isProduction, isStrict, rootIgnore, negatedWorkspacePatterns, enabledPluginsInAncestors, }) {
28
29
  this.name = name;
29
30
  this.dir = dir;
@@ -77,61 +78,38 @@ export class WorkspaceWorker {
77
78
  this.hostDependencies = hostDependencies;
78
79
  this.installedBinaries = installedBinaries;
79
80
  this.hasTypesIncluded = hasTypesIncluded;
81
+ this.hasTypesIncluded = hasTypesIncluded;
80
82
  }
81
83
  getConfigForPlugin(pluginName) {
82
- return this.config[pluginName] ?? nullConfig;
84
+ return this.config[pluginName] !== true ? this.config[pluginName] ?? nullConfig : nullConfig;
83
85
  }
84
86
  getEntryFilePatterns() {
85
87
  const { entry } = this.config;
86
88
  if (entry.length === 0)
87
89
  return [];
88
- return [entry, TEST_FILE_PATTERNS, this.negatedWorkspacePatterns].flat();
90
+ return [entry, this.negatedWorkspacePatterns].flat();
89
91
  }
90
- getProjectFilePatterns() {
92
+ getProjectFilePatterns(testFilePatterns) {
91
93
  const { project } = this.config;
92
94
  if (project.length === 0)
93
95
  return [];
94
96
  const negatedPluginConfigPatterns = this.getPluginConfigPatterns().map(negate);
95
- const negatedPluginEntryFilePatterns = this.getPluginEntryFilePatterns(false).map(negate);
96
97
  const negatedPluginProjectFilePatterns = this.getPluginProjectFilePatterns().map(negate);
97
98
  return [
98
99
  project,
99
100
  negatedPluginConfigPatterns,
100
- negatedPluginEntryFilePatterns,
101
101
  negatedPluginProjectFilePatterns,
102
- TEST_FILE_PATTERNS,
102
+ testFilePatterns,
103
103
  this.negatedWorkspacePatterns,
104
104
  ].flat();
105
105
  }
106
- getPluginEntryFilePatterns(isIncludeProductionEntryFiles = true) {
107
- const patterns = [];
108
- for (const [pluginName, plugin] of Object.entries(plugins)) {
109
- const pluginConfig = this.getConfigForPlugin(pluginName);
110
- if (this.enabled[pluginName] && pluginConfig) {
111
- const { entry } = pluginConfig === true ? nullConfig : pluginConfig;
112
- const defaultEntryFiles = 'ENTRY_FILE_PATTERNS' in plugin ? plugin.ENTRY_FILE_PATTERNS : [];
113
- patterns.push(...(entry ?? defaultEntryFiles));
114
- if (isIncludeProductionEntryFiles) {
115
- const entry = 'PRODUCTION_ENTRY_FILE_PATTERNS' in plugin ? plugin.PRODUCTION_ENTRY_FILE_PATTERNS : [];
116
- patterns.push(...entry);
117
- }
118
- }
119
- }
120
- return [patterns, this.negatedWorkspacePatterns].flat();
121
- }
122
106
  getPluginProjectFilePatterns() {
123
107
  const patterns = [];
124
108
  for (const [pluginName, plugin] of Object.entries(plugins)) {
125
109
  const pluginConfig = this.getConfigForPlugin(pluginName);
126
110
  if (this.enabled[pluginName] && pluginConfig) {
127
111
  const { entry, project } = pluginConfig === true ? nullConfig : pluginConfig;
128
- patterns.push(...(project ??
129
- entry ??
130
- ('PROJECT_FILE_PATTERNS' in plugin
131
- ? plugin.PROJECT_FILE_PATTERNS
132
- : 'ENTRY_FILE_PATTERNS' in plugin
133
- ? plugin.ENTRY_FILE_PATTERNS
134
- : [])));
112
+ patterns.push(...(project ?? entry ?? ('PROJECT_FILE_PATTERNS' in plugin ? plugin.PROJECT_FILE_PATTERNS : [])));
135
113
  }
136
114
  }
137
115
  return [patterns, this.negatedWorkspacePatterns].flat();
@@ -148,17 +126,17 @@ export class WorkspaceWorker {
148
126
  }
149
127
  return patterns;
150
128
  }
151
- getProductionEntryFilePatterns() {
129
+ getProductionEntryFilePatterns(negatedTestFilePatterns) {
152
130
  const entry = this.config.entry.filter(hasProductionSuffix);
153
131
  if (entry.length === 0)
154
132
  return [];
155
133
  const negatedEntryFiles = this.config.entry.filter(hasNoProductionSuffix).map(negate);
156
134
  return [entry, negatedEntryFiles, negatedTestFilePatterns, this.negatedWorkspacePatterns].flat();
157
135
  }
158
- getProductionProjectFilePatterns() {
136
+ getProductionProjectFilePatterns(negatedTestFilePatterns) {
159
137
  const project = this.config.project;
160
138
  if (project.length === 0)
161
- return this.getProductionEntryFilePatterns();
139
+ return this.getProductionEntryFilePatterns(negatedTestFilePatterns);
162
140
  const _project = this.config.project.map(pattern => {
163
141
  if (!pattern.endsWith('!') && !pattern.startsWith('!'))
164
142
  return negate(pattern);
@@ -166,32 +144,16 @@ export class WorkspaceWorker {
166
144
  });
167
145
  const negatedEntryFiles = this.config.entry.filter(hasNoProductionSuffix).map(negate);
168
146
  const negatedPluginConfigPatterns = this.getPluginConfigPatterns().map(negate);
169
- const negatedPluginEntryFilePatterns = this.getPluginEntryFilePatterns(false).map(negate);
170
147
  const negatedPluginProjectFilePatterns = this.getPluginProjectFilePatterns().map(negate);
171
148
  return [
172
149
  _project,
173
150
  negatedEntryFiles,
174
151
  negatedPluginConfigPatterns,
175
- negatedPluginEntryFilePatterns,
176
152
  negatedPluginProjectFilePatterns,
177
153
  negatedTestFilePatterns,
178
154
  this.negatedWorkspacePatterns,
179
155
  ].flat();
180
156
  }
181
- getProductionPluginEntryFilePatterns() {
182
- const patterns = [];
183
- for (const [pluginName, plugin] of Object.entries(plugins)) {
184
- const pluginConfig = this.getConfigForPlugin(pluginName);
185
- if (this.enabled[pluginName] && pluginConfig) {
186
- if ('PRODUCTION_ENTRY_FILE_PATTERNS' in plugin) {
187
- patterns.push(...((pluginConfig === true ? null : pluginConfig.entry) ?? plugin.PRODUCTION_ENTRY_FILE_PATTERNS));
188
- }
189
- }
190
- }
191
- if (patterns.length === 0)
192
- return [];
193
- return [patterns.flat(), negatedTestFilePatterns].flat();
194
- }
195
157
  getConfigurationFilePatterns(pluginName) {
196
158
  const plugin = plugins[pluginName];
197
159
  const pluginConfig = this.getConfigForPlugin(pluginName);
@@ -208,31 +170,41 @@ export class WorkspaceWorker {
208
170
  const cwd = this.dir;
209
171
  const ignore = this.getIgnorePatterns();
210
172
  for (const [pluginName, plugin] of Object.entries(plugins)) {
211
- const isIncludePlugin = this.isProduction ? `PRODUCTION_ENTRY_FILE_PATTERNS` in plugin : true;
212
- if (this.enabled[pluginName] && isIncludePlugin) {
173
+ if (this.enabled[pluginName]) {
213
174
  const hasDependencyFinder = 'findDependencies' in plugin && typeof plugin.findDependencies === 'function';
214
175
  if (hasDependencyFinder) {
215
176
  const pluginConfig = this.getConfigForPlugin(pluginName);
216
177
  if (!pluginConfig)
217
178
  continue;
218
179
  const patterns = this.getConfigurationFilePatterns(pluginName);
219
- const configFilePaths = await _pureGlob({ patterns, cwd, ignore, gitignore: false });
180
+ const allConfigFilePaths = await _pureGlob({ patterns, cwd, ignore, gitignore: false });
181
+ const configFilePaths = allConfigFilePaths.filter(filePath => !filePath.endsWith('package.json') ||
182
+ get(this.manifest, 'PACKAGE_JSON_PATH' in plugin ? plugin.PACKAGE_JSON_PATH : pluginName));
220
183
  debugLogArray(`Found ${plugin.NAME} config file paths`, configFilePaths);
221
- if (configFilePaths.length === 0)
184
+ if (patterns.length > 0 && configFilePaths.length === 0)
222
185
  continue;
186
+ if (patterns.length === 0)
187
+ configFilePaths.push('__FAKE__');
223
188
  const pluginDependencies = new Set();
224
189
  for (const configFilePath of configFilePaths) {
225
190
  const dependencies = await plugin.findDependencies(configFilePath, {
226
191
  cwd,
227
192
  manifest: this.manifest,
228
- config: pluginConfig,
193
+ config: pluginConfig === true ? nullConfig : pluginConfig,
229
194
  isProduction: this.isProduction,
230
195
  });
231
- dependencies.map(toPosix).forEach(specifier => {
196
+ dependencies.forEach(specifier => {
232
197
  pluginDependencies.add(specifier);
233
- this.referencedDependencies.add([configFilePath, specifier]);
198
+ if (isEntryPattern(specifier)) {
199
+ this.entryFilePatterns.add(fromEntryPattern(specifier));
200
+ }
201
+ else if (isProductionEntryPattern(specifier)) {
202
+ this.productionEntryFilePatterns.add(fromProductionEntryPattern(specifier));
203
+ }
204
+ else {
205
+ this.referencedDependencies.add([configFilePath, toPosix(specifier)]);
206
+ }
234
207
  });
235
- dependencies.forEach(dependency => pluginDependencies.add(dependency));
236
208
  }
237
209
  debugLogArray(`Dependencies referenced in ${plugin.NAME}`, pluginDependencies);
238
210
  }
@@ -247,6 +219,8 @@ export class WorkspaceWorker {
247
219
  referencedDependencies: this.referencedDependencies,
248
220
  hasTypesIncluded: this.hasTypesIncluded,
249
221
  enabledPlugins: this.enabledPlugins,
222
+ entryFilePatterns: Array.from(this.entryFilePatterns),
223
+ productionEntryFilePatterns: Array.from(this.productionEntryFilePatterns),
250
224
  };
251
225
  }
252
226
  }
@@ -2,8 +2,8 @@ import { compact } from '../util/array.js';
2
2
  import { getPackageNameFromModuleSpecifier } from '../util/modules.js';
3
3
  import { isInternal } from '../util/path.js';
4
4
  import { timerify } from '../util/Performance.js';
5
+ import { isBinary } from '../util/protocols.js';
5
6
  import { getBinariesFromScript } from './bash-parser.js';
6
- import { isBinary } from './util.js';
7
7
  const defaultCwd = process.cwd();
8
8
  const getDependenciesFromScripts = (npmScripts, options = {}) => {
9
9
  const { cwd = defaultCwd, manifest = {}, knownGlobalsOnly = false } = options;
@@ -1,5 +1,6 @@
1
1
  import parseArgs from 'minimist';
2
- import { toBinary, argsFrom } from '../util.js';
2
+ import { toBinary } from '../../util/protocols.js';
3
+ import { argsFrom } from '../util.js';
3
4
  export const resolve = (binary, args, { fromArgs }) => {
4
5
  const parsed = parseArgs(args, {
5
6
  boolean: ['all', 'check-coverage', 'clean', 'exclude-after-remap', 'per-file', 'skip-full'],
@@ -1,5 +1,6 @@
1
1
  import parseArgs from 'minimist';
2
- import { toBinary, argsFrom } from '../util.js';
2
+ import { toBinary } from '../../util/protocols.js';
3
+ import { argsFrom } from '../util.js';
3
4
  export const resolve = (binary, args, { fromArgs }) => {
4
5
  const parsed = parseArgs(args);
5
6
  return [toBinary(binary), ...fromArgs(argsFrom(args, parsed._[0]))];
@@ -1,6 +1,7 @@
1
1
  import parseArgs from 'minimist';
2
2
  import { compact } from '../../util/array.js';
3
- import { toBinary, tryResolveFilePath, tryResolveSpecifiers } from '../util.js';
3
+ import { toBinary } from '../../util/protocols.js';
4
+ import { tryResolveFilePath, tryResolveSpecifiers } from '../util.js';
4
5
  const withPositional = parsed => [parsed._[0], parsed.require].flat();
5
6
  const withoutPositional = parsed => [parsed.require].flat();
6
7
  const argFilters = {
@@ -1,6 +1,7 @@
1
1
  import parseArgs from 'minimist';
2
2
  import { compact } from '../../util/array.js';
3
- import { toBinary, tryResolveFilePath, tryResolveSpecifiers } from '../util.js';
3
+ import { toBinary } from '../../util/protocols.js';
4
+ import { tryResolveFilePath, tryResolveSpecifiers } from '../util.js';
4
5
  export const resolve = (binary, args, { cwd, fromArgs }) => {
5
6
  const parsed = parseArgs(args, {
6
7
  string: ['r', 'exec'],
@@ -1,7 +1,8 @@
1
1
  import parseArgs from 'minimist';
2
2
  import { isInternal } from '../../util/path.js';
3
+ import { toBinary } from '../../util/protocols.js';
3
4
  import { getBinariesFromScript } from '../bash-parser.js';
4
- import { argsFrom, stripVersionFromSpecifier, toBinary } from '../util.js';
5
+ import { argsFrom, stripVersionFromSpecifier } from '../util.js';
5
6
  export const resolve = (binary, args, { cwd, fromArgs, manifest }) => {
6
7
  const parsed = parseArgs(args, {
7
8
  boolean: ['yes', 'no'],
@@ -1,5 +1,5 @@
1
1
  import parseArgs from 'minimist';
2
- import { toBinary } from '../util.js';
2
+ import { toBinary } from '../../util/protocols.js';
3
3
  export const resolve = (binary, args, { fromArgs }) => {
4
4
  const parsed = parseArgs(args);
5
5
  const [command] = parsed._;
@@ -1,5 +1,5 @@
1
1
  import parseArgs from 'minimist';
2
- import { toBinary } from '../util.js';
2
+ import { toBinary } from '../../util/protocols.js';
3
3
  const commands = [
4
4
  'add',
5
5
  'audit',
@@ -1,6 +1,7 @@
1
1
  import parseArgs from 'minimist';
2
2
  import { compact } from '../../util/array.js';
3
- import { toBinary, tryResolveSpecifiers } from '../util.js';
3
+ import { toBinary } from '../../util/protocols.js';
4
+ import { tryResolveSpecifiers } from '../util.js';
4
5
  export const resolve = (binary, args, { cwd, fromArgs }) => {
5
6
  const safeArgs = args.filter(arg => arg !== '--watch');
6
7
  const parsed = parseArgs(safeArgs, { alias: { plugin: 'p' } });
@@ -1,5 +1,5 @@
1
1
  import parseArgs from 'minimist';
2
- import { toBinary } from '../util.js';
2
+ import { toBinary } from '../../util/protocols.js';
3
3
  const commands = [
4
4
  'add',
5
5
  'bin',
@@ -1,8 +1,5 @@
1
1
  export declare const tryResolveFilePath: (cwd: string, specifier: string, acceptModuleSpecifier?: boolean) => string | undefined;
2
2
  export declare const tryResolveSpecifiers: (cwd: string, specifiers: string[]) => (string | undefined)[];
3
- export declare const toBinary: (specifier: string) => string;
4
- export declare const fromBinary: (specifier: string) => string;
5
- export declare const isBinary: (specifier: string) => boolean;
6
3
  export declare const stripVersionFromSpecifier: (specifier: string) => string;
7
4
  export declare const stripBinaryPath: (command: string) => string;
8
5
  export declare const argsFrom: (args: string[], from: string) => string[];
@@ -1,5 +1,6 @@
1
1
  import { getPackageNameFromFilePath, getPackageNameFromModuleSpecifier } from '../util/modules.js';
2
2
  import { isInNodeModules, join } from '../util/path.js';
3
+ import { toBinary } from '../util/protocols.js';
3
4
  import { _tryResolve } from '../util/require.js';
4
5
  export const tryResolveFilePath = (cwd, specifier, acceptModuleSpecifier) => {
5
6
  if (specifier) {
@@ -22,9 +23,6 @@ export const tryResolveFilePath = (cwd, specifier, acceptModuleSpecifier) => {
22
23
  }
23
24
  };
24
25
  export const tryResolveSpecifiers = (cwd, specifiers) => specifiers.map(specifier => tryResolveFilePath(cwd, specifier, true));
25
- export const toBinary = (specifier) => specifier.replace(/^(bin:)?/, 'bin:');
26
- export const fromBinary = (specifier) => specifier.replace(/^(bin:)?/, '');
27
- export const isBinary = (specifier) => specifier.startsWith('bin:');
28
26
  export const stripVersionFromSpecifier = (specifier) => specifier.replace(/(\S+)@.*/, '$1');
29
27
  const stripNodeModulesFromPath = (command) => command.replace(/^(\.\/)?node_modules\//, '');
30
28
  export const stripBinaryPath = (command) => stripVersionFromSpecifier(stripNodeModulesFromPath(command)
@@ -2,7 +2,6 @@ import type { IssueType } from './types/issues.js';
2
2
  export declare const ROOT_WORKSPACE_NAME = ".";
3
3
  export declare const KNIP_CONFIG_LOCATIONS: string[];
4
4
  export declare const DEFAULT_EXTENSIONS: string[];
5
- export declare const TEST_FILE_PATTERNS: string[];
6
5
  export declare const GLOBAL_IGNORE_PATTERNS: string[];
7
6
  export declare const IGNORED_GLOBAL_BINARIES: string[];
8
7
  export declare const IGNORED_DEPENDENCIES: string[];
package/dist/constants.js CHANGED
@@ -1,10 +1,6 @@
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', '.mts', '.cts'];
4
- export const TEST_FILE_PATTERNS = [
5
- '**/*{.,-}{test,spec}.{js,jsx,ts,tsx,mjs,cjs}',
6
- '**/{test,__tests__}/**/*.{js,jsx,ts,tsx,mjs,cjs}',
7
- ];
8
4
  export const GLOBAL_IGNORE_PATTERNS = ['**/node_modules/**', '.yarn'];
9
5
  export const IGNORED_GLOBAL_BINARIES = [
10
6
  'bash',