knip 2.30.1 → 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 (103) hide show
  1. package/README.md +15 -24
  2. package/dist/ConfigurationChief.d.ts +2 -0
  3. package/dist/ConfigurationChief.js +57 -61
  4. package/dist/ConfigurationValidator.d.ts +377 -321
  5. package/dist/ConfigurationValidator.js +2 -1
  6. package/dist/WorkspaceWorker.d.ts +8 -6
  7. package/dist/WorkspaceWorker.js +41 -62
  8. package/dist/binaries/index.js +1 -1
  9. package/dist/binaries/resolvers/c8.js +2 -1
  10. package/dist/binaries/resolvers/dotenv.js +2 -1
  11. package/dist/binaries/resolvers/fallback.js +2 -1
  12. package/dist/binaries/resolvers/nodemon.js +2 -1
  13. package/dist/binaries/resolvers/npx.js +2 -1
  14. package/dist/binaries/resolvers/nx.js +1 -1
  15. package/dist/binaries/resolvers/pnpm.js +1 -1
  16. package/dist/binaries/resolvers/rollup.js +2 -1
  17. package/dist/binaries/resolvers/yarn.js +1 -1
  18. package/dist/binaries/util.d.ts +0 -3
  19. package/dist/binaries/util.js +1 -3
  20. package/dist/constants.d.ts +0 -1
  21. package/dist/constants.js +0 -4
  22. package/dist/index.js +27 -20
  23. package/dist/plugins/ava/index.d.ts +1 -1
  24. package/dist/plugins/ava/index.js +23 -5
  25. package/dist/plugins/ava/types.d.ts +1 -0
  26. package/dist/plugins/babel/index.js +3 -1
  27. package/dist/plugins/capacitor/index.js +3 -1
  28. package/dist/plugins/changesets/index.js +3 -1
  29. package/dist/plugins/commitizen/index.d.ts +1 -0
  30. package/dist/plugins/commitizen/index.js +4 -1
  31. package/dist/plugins/commitlint/index.js +3 -1
  32. package/dist/plugins/cspell/index.js +3 -1
  33. package/dist/plugins/cypress/index.d.ts +3 -1
  34. package/dist/plugins/cypress/index.js +14 -4
  35. package/dist/plugins/drizzle/index.js +3 -5
  36. package/dist/plugins/eslint/helpers.js +2 -1
  37. package/dist/plugins/eslint/index.d.ts +1 -0
  38. package/dist/plugins/eslint/index.js +4 -1
  39. package/dist/plugins/gatsby/index.js +10 -2
  40. package/dist/plugins/github-actions/index.js +3 -1
  41. package/dist/plugins/husky/index.js +3 -1
  42. package/dist/plugins/index.d.ts +1 -0
  43. package/dist/plugins/index.js +1 -0
  44. package/dist/plugins/jest/index.d.ts +1 -1
  45. package/dist/plugins/jest/index.js +16 -7
  46. package/dist/plugins/lefthook/index.js +4 -2
  47. package/dist/plugins/lint-staged/index.d.ts +1 -0
  48. package/dist/plugins/lint-staged/index.js +4 -1
  49. package/dist/plugins/markdownlint/index.js +3 -1
  50. package/dist/plugins/mocha/index.d.ts +1 -0
  51. package/dist/plugins/mocha/index.js +10 -6
  52. package/dist/plugins/next/index.d.ts +2 -1
  53. package/dist/plugins/next/index.js +4 -0
  54. package/dist/plugins/node-test-runner/index.d.ts +6 -0
  55. package/dist/plugins/node-test-runner/index.js +17 -0
  56. package/dist/plugins/npm-package-json-lint/index.d.ts +1 -0
  57. package/dist/plugins/npm-package-json-lint/index.js +5 -2
  58. package/dist/plugins/nx/index.js +3 -1
  59. package/dist/plugins/nyc/index.js +3 -1
  60. package/dist/plugins/playwright/index.d.ts +3 -1
  61. package/dist/plugins/playwright/index.js +22 -2
  62. package/dist/plugins/playwright-ct/index.d.ts +3 -1
  63. package/dist/plugins/playwright-ct/index.js +13 -2
  64. package/dist/plugins/postcss/index.js +8 -6
  65. package/dist/plugins/prettier/index.js +3 -1
  66. package/dist/plugins/release-it/index.d.ts +1 -0
  67. package/dist/plugins/release-it/index.js +5 -2
  68. package/dist/plugins/remark/index.d.ts +1 -0
  69. package/dist/plugins/remark/index.js +10 -3
  70. package/dist/plugins/remix/index.d.ts +2 -1
  71. package/dist/plugins/remix/index.js +10 -0
  72. package/dist/plugins/rollup/index.d.ts +2 -1
  73. package/dist/plugins/rollup/index.js +7 -0
  74. package/dist/plugins/semantic-release/index.d.ts +1 -0
  75. package/dist/plugins/semantic-release/index.js +7 -2
  76. package/dist/plugins/sentry/index.d.ts +2 -1
  77. package/dist/plugins/sentry/index.js +7 -0
  78. package/dist/plugins/storybook/index.js +6 -2
  79. package/dist/plugins/stryker/index.js +9 -8
  80. package/dist/plugins/stylelint/index.js +3 -1
  81. package/dist/plugins/svelte/index.d.ts +2 -1
  82. package/dist/plugins/svelte/index.js +6 -0
  83. package/dist/plugins/typedoc/index.d.ts +1 -0
  84. package/dist/plugins/typedoc/index.js +5 -2
  85. package/dist/plugins/typescript/index.js +3 -1
  86. package/dist/plugins/vite/index.js +2 -2
  87. package/dist/plugins/vitest/index.d.ts +3 -3
  88. package/dist/plugins/vitest/index.js +16 -23
  89. package/dist/plugins/vitest/types.d.ts +1 -0
  90. package/dist/plugins/webpack/index.js +3 -1
  91. package/dist/types/config.d.ts +5 -3
  92. package/dist/types/plugins.d.ts +3 -3
  93. package/dist/util/compilers.d.ts +90 -80
  94. package/dist/util/object.d.ts +1 -0
  95. package/dist/util/object.js +1 -0
  96. package/dist/util/plugin.d.ts +2 -2
  97. package/dist/util/plugin.js +2 -2
  98. package/dist/util/protocols.d.ts +9 -0
  99. package/dist/util/protocols.js +9 -0
  100. package/dist/version.d.ts +1 -1
  101. package/dist/version.js +1 -1
  102. package/package.json +2 -1
  103. 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
@@ -27,6 +27,7 @@ export declare class ConfigurationChief {
27
27
  enabledWorkspaces: Workspace[];
28
28
  localWorkspaces: Set<string>;
29
29
  resolvedConfigFilePath?: string;
30
+ rawConfig?: any;
30
31
  constructor({ cwd, isProduction }: ConfigurationManagerOptions);
31
32
  init(): Promise<void>;
32
33
  getCompilers(): [SyncCompilers, AsyncCompilers];
@@ -36,6 +37,7 @@ export declare class ConfigurationChief {
36
37
  private getListedWorkspaces;
37
38
  private getIgnoredWorkspacePatterns;
38
39
  private getManifestWorkspaces;
40
+ private getConfiguredWorkspaceKeys;
39
41
  private getAdditionalWorkspaceNames;
40
42
  private getAvailableWorkspaceNames;
41
43
  private getEnabledWorkspaces;
@@ -41,9 +41,8 @@ const defaultConfig = {
41
41
  ignoreWorkspaces: [],
42
42
  syncCompilers: new Map(),
43
43
  asyncCompilers: new Map(),
44
- workspaces: {
45
- [ROOT_WORKSPACE_NAME]: getDefaultWorkspaceConfig(),
46
- },
44
+ defaultWorkspaceConfig: getDefaultWorkspaceConfig(),
45
+ rootPluginConfigs: {},
47
46
  };
48
47
  const PLUGIN_NAMES = Object.keys(plugins);
49
48
  export class ConfigurationChief {
@@ -60,6 +59,7 @@ export class ConfigurationChief {
60
59
  enabledWorkspaces = [];
61
60
  localWorkspaces = new Set();
62
61
  resolvedConfigFilePath;
62
+ rawConfig;
63
63
  constructor({ cwd, isProduction }) {
64
64
  this.cwd = cwd;
65
65
  this.isProduction = isProduction;
@@ -86,11 +86,9 @@ export class ConfigurationChief {
86
86
  if (rawConfigArg && !this.resolvedConfigFilePath && !manifest.knip) {
87
87
  throw new ConfigurationError(`Unable to find ${rawConfigArg} or package.json#knip`);
88
88
  }
89
- const rawLocalConfig = this.resolvedConfigFilePath ? await _load(this.resolvedConfigFilePath) : manifest.knip;
90
- if (rawLocalConfig) {
91
- const parsedConfig = ConfigurationValidator.parse(partitionCompilers(rawLocalConfig));
92
- this.config = this.normalize(parsedConfig);
93
- }
89
+ this.rawConfig = this.resolvedConfigFilePath ? await _load(this.resolvedConfigFilePath) : manifest.knip;
90
+ const parsedConfig = this.rawConfig ? ConfigurationValidator.parse(partitionCompilers(this.rawConfig)) : {};
91
+ this.config = this.normalize(parsedConfig);
94
92
  await this.setWorkspaces();
95
93
  }
96
94
  getCompilers() {
@@ -99,58 +97,25 @@ export class ConfigurationChief {
99
97
  getRules() {
100
98
  return this.config.rules;
101
99
  }
102
- normalize(rawLocalConfig) {
103
- const initialWorkspaces = rawLocalConfig.workspaces ?? {
104
- [ROOT_WORKSPACE_NAME]: {
105
- ...rawLocalConfig,
106
- },
107
- };
108
- const rules = { ...defaultRules, ...rawLocalConfig.rules };
109
- const include = rawLocalConfig.include ?? defaultConfig.include;
110
- const exclude = rawLocalConfig.exclude ?? defaultConfig.exclude;
111
- const ignore = arrayify(rawLocalConfig.ignore ?? defaultConfig.ignore);
112
- const ignoreBinaries = rawLocalConfig.ignoreBinaries ?? [];
113
- const ignoreExportsUsedInFile = rawLocalConfig.ignoreExportsUsedInFile ?? false;
114
- const ignoreDependencies = rawLocalConfig.ignoreDependencies ?? [];
115
- const ignoreWorkspaces = rawLocalConfig.ignoreWorkspaces ?? defaultConfig.ignoreWorkspaces;
116
- const { syncCompilers, asyncCompilers } = rawLocalConfig;
100
+ normalize(rawConfig) {
101
+ const rules = { ...defaultRules, ...rawConfig.rules };
102
+ const include = rawConfig.include ?? defaultConfig.include;
103
+ const exclude = rawConfig.exclude ?? defaultConfig.exclude;
104
+ const ignore = arrayify(rawConfig.ignore ?? defaultConfig.ignore);
105
+ const ignoreBinaries = rawConfig.ignoreBinaries ?? [];
106
+ const ignoreExportsUsedInFile = rawConfig.ignoreExportsUsedInFile ?? false;
107
+ const ignoreDependencies = rawConfig.ignoreDependencies ?? [];
108
+ const ignoreWorkspaces = rawConfig.ignoreWorkspaces ?? defaultConfig.ignoreWorkspaces;
109
+ const { syncCompilers, asyncCompilers } = rawConfig;
117
110
  const extensions = [...Object.keys(syncCompilers ?? {}), ...Object.keys(asyncCompilers ?? {})];
118
111
  const defaultWorkspaceConfig = getDefaultWorkspaceConfig(extensions);
119
112
  const rootPluginConfigs = {};
120
- for (const [name, pluginConfig] of Object.entries(rawLocalConfig)) {
113
+ for (const [name, pluginConfig] of Object.entries(rawConfig)) {
121
114
  const pluginName = toCamelCase(name);
122
115
  if (PLUGIN_NAMES.includes(pluginName)) {
123
116
  rootPluginConfigs[pluginName] = normalizePluginConfig(pluginConfig);
124
117
  }
125
118
  }
126
- const workspaces = Object.entries(initialWorkspaces)
127
- .filter(([workspaceName]) => !ignoreWorkspaces.includes(workspaceName))
128
- .reduce((workspaces, workspace) => {
129
- const [workspaceName, workspaceConfig] = workspace;
130
- const entry = workspaceConfig.entry ? arrayify(workspaceConfig.entry) : defaultWorkspaceConfig.entry;
131
- const project = workspaceConfig.project ? arrayify(workspaceConfig.project) : defaultWorkspaceConfig.project;
132
- const paths = workspaceConfig.paths ?? defaultWorkspaceConfig.paths;
133
- workspaces[workspaceName] = {
134
- entry,
135
- project,
136
- paths,
137
- ignore: arrayify(workspaceConfig.ignore),
138
- ignoreBinaries: arrayify(workspaceConfig.ignoreBinaries),
139
- ignoreDependencies: arrayify(workspaceConfig.ignoreDependencies),
140
- };
141
- for (const [name, pluginConfig] of Object.entries(rootPluginConfigs)) {
142
- const pluginName = toCamelCase(name);
143
- if (pluginConfig)
144
- workspaces[workspaceName][pluginName] = pluginConfig;
145
- }
146
- for (const [name, pluginConfig] of Object.entries(workspaceConfig)) {
147
- const pluginName = toCamelCase(name);
148
- if (PLUGIN_NAMES.includes(pluginName)) {
149
- workspaces[workspaceName][pluginName] = normalizePluginConfig(pluginConfig);
150
- }
151
- }
152
- return workspaces;
153
- }, {});
154
119
  return {
155
120
  rules,
156
121
  include,
@@ -162,7 +127,8 @@ export class ConfigurationChief {
162
127
  ignoreWorkspaces,
163
128
  syncCompilers: new Map(Object.entries(syncCompilers ?? {})),
164
129
  asyncCompilers: new Map(Object.entries(asyncCompilers ?? {})),
165
- workspaces,
130
+ rootPluginConfigs,
131
+ defaultWorkspaceConfig,
166
132
  };
167
133
  }
168
134
  async setWorkspaces() {
@@ -197,14 +163,21 @@ export class ConfigurationChief {
197
163
  });
198
164
  const manifestWorkspaces = new Map();
199
165
  for (const [pkgName, dir] of workspaces.entries()) {
200
- manifestWorkspaces.set(relative(this.cwd, dir), pkgName);
166
+ manifestWorkspaces.set(relative(this.cwd, dir) || ROOT_WORKSPACE_NAME, pkgName);
201
167
  }
202
168
  return manifestWorkspaces;
203
169
  }
170
+ getConfiguredWorkspaceKeys() {
171
+ const initialWorkspaces = this.rawConfig?.workspaces
172
+ ? Object.keys(this.rawConfig.workspaces)
173
+ : [ROOT_WORKSPACE_NAME];
174
+ const ignoreWorkspaces = this.rawConfig?.ignoreWorkspaces ?? defaultConfig.ignoreWorkspaces;
175
+ return initialWorkspaces.filter(workspaceName => !ignoreWorkspaces.includes(workspaceName));
176
+ }
204
177
  async getAdditionalWorkspaceNames() {
205
- const additionalWorkspaceKeys = Object.keys(this.config.workspaces);
206
- const patterns = additionalWorkspaceKeys.filter(key => key.includes('*'));
207
- const dirs = additionalWorkspaceKeys.filter(key => !key.includes('*'));
178
+ const workspaceKeys = this.getConfiguredWorkspaceKeys();
179
+ const patterns = workspaceKeys.filter(key => key.includes('*'));
180
+ const dirs = workspaceKeys.filter(key => !key.includes('*'));
208
181
  const globbedDirs = await _dirGlob({ patterns, cwd: this.cwd });
209
182
  return new Set([...dirs, ...globbedDirs].filter(name => name !== ROOT_WORKSPACE_NAME &&
210
183
  !this.manifestWorkspaces.has(name) &&
@@ -260,16 +233,39 @@ export class ConfigurationChief {
260
233
  .map(workspaceName => `!${workspaceName}`);
261
234
  }
262
235
  getConfigKeyForWorkspace(workspaceName) {
263
- return Object.keys(this.config.workspaces)
236
+ return this.getConfiguredWorkspaceKeys()
264
237
  .sort(byPathDepth)
265
238
  .reverse()
266
239
  .find(pattern => micromatch.isMatch(workspaceName, pattern));
267
240
  }
268
241
  getConfigForWorkspace(workspaceName) {
269
242
  const key = this.getConfigKeyForWorkspace(workspaceName);
270
- if (key && this.config?.workspaces?.[key])
271
- return this.config.workspaces[key];
272
- return getDefaultWorkspaceConfig();
243
+ const defaultConfig = this.config.defaultWorkspaceConfig;
244
+ const workspaces = this.rawConfig?.workspaces ?? {};
245
+ const workspaceConfig = (key
246
+ ? key === ROOT_WORKSPACE_NAME && !(ROOT_WORKSPACE_NAME in workspaces)
247
+ ? this.rawConfig
248
+ : workspaces[key]
249
+ : {}) ?? {};
250
+ const entry = workspaceConfig.entry ? arrayify(workspaceConfig.entry) : defaultConfig.entry;
251
+ const project = workspaceConfig.project ? arrayify(workspaceConfig.project) : defaultConfig.project;
252
+ const paths = workspaceConfig.paths ?? defaultConfig.paths;
253
+ const ignore = arrayify(workspaceConfig.ignore);
254
+ const ignoreBinaries = arrayify(workspaceConfig.ignoreBinaries);
255
+ const ignoreDependencies = arrayify(workspaceConfig.ignoreDependencies);
256
+ const plugins = {};
257
+ for (const [name, pluginConfig] of Object.entries(this.config.rootPluginConfigs)) {
258
+ const pluginName = toCamelCase(name);
259
+ if (typeof pluginConfig !== 'undefined')
260
+ plugins[pluginName] = pluginConfig;
261
+ }
262
+ for (const [name, pluginConfig] of Object.entries(workspaceConfig)) {
263
+ const pluginName = toCamelCase(name);
264
+ if (PLUGIN_NAMES.includes(pluginName)) {
265
+ plugins[pluginName] = normalizePluginConfig(pluginConfig);
266
+ }
267
+ }
268
+ return { entry, project, paths, ignore, ignoreBinaries, ignoreDependencies, ...plugins };
273
269
  }
274
270
  getIssueTypesToReport() {
275
271
  const cliArgs = { include, exclude, dependencies, exports };