knip 6.44.1 → 6.45.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 (231) hide show
  1. package/dist/ConfigurationChief.d.ts +23 -1033
  2. package/dist/ConfigurationChief.js +71 -15
  3. package/dist/DependencyDeputy.d.ts +10 -6
  4. package/dist/DependencyDeputy.js +51 -27
  5. package/dist/IssueCollector.d.ts +14 -11
  6. package/dist/IssueCollector.js +25 -20
  7. package/dist/IssueFixer.js +1 -1
  8. package/dist/PackagePeeker.d.ts +6 -0
  9. package/dist/PackagePeeker.js +43 -14
  10. package/dist/ProjectPrincipal.d.ts +6 -3
  11. package/dist/ProjectPrincipal.js +83 -32
  12. package/dist/WorkspaceWorker.d.ts +5 -1
  13. package/dist/WorkspaceWorker.js +39 -22
  14. package/dist/binaries/bash-parser.js +1 -7
  15. package/dist/binaries/command.d.ts +3 -3
  16. package/dist/binaries/command.js +6 -2
  17. package/dist/binaries/fallback.js +1 -1
  18. package/dist/binaries/plugins.d.ts +4 -1
  19. package/dist/binaries/plugins.js +66 -18
  20. package/dist/binaries/resolvers/bun.js +2 -2
  21. package/dist/binaries/resolvers/index.d.ts +1 -0
  22. package/dist/binaries/resolvers/index.js +2 -0
  23. package/dist/binaries/resolvers/npm.js +7 -5
  24. package/dist/binaries/resolvers/npx.js +15 -12
  25. package/dist/binaries/resolvers/nub.js +2 -2
  26. package/dist/binaries/resolvers/vp.d.ts +2 -0
  27. package/dist/binaries/resolvers/vp.js +52 -0
  28. package/dist/binaries/util.d.ts +13 -1
  29. package/dist/binaries/util.js +35 -1
  30. package/dist/cli.js +10 -84
  31. package/dist/compilers/scss.js +2 -1
  32. package/dist/configuration-hints.d.ts +16 -0
  33. package/dist/configuration-hints.js +64 -0
  34. package/dist/constants.d.ts +2 -1
  35. package/dist/constants.js +37 -0
  36. package/dist/graph/SubpathReferences.d.ts +33 -0
  37. package/dist/graph/SubpathReferences.js +114 -0
  38. package/dist/graph/analyze.d.ts +4 -1
  39. package/dist/graph/analyze.js +16 -5
  40. package/dist/graph/build.d.ts +3 -1
  41. package/dist/graph/build.js +47 -23
  42. package/dist/graph-explorer/operations/resolve-export-origins.js +1 -1
  43. package/dist/plugins/angular/index.js +10 -17
  44. package/dist/plugins/aws-cdk/index.d.ts +3 -0
  45. package/dist/plugins/aws-cdk/index.js +32 -0
  46. package/dist/plugins/aws-cdk/types.d.ts +5 -0
  47. package/dist/plugins/aws-cdk/types.js +1 -0
  48. package/dist/plugins/babel/index.d.ts +1 -1
  49. package/dist/plugins/babel/index.js +25 -6
  50. package/dist/plugins/babel/types.d.ts +1 -0
  51. package/dist/plugins/bun/index.js +8 -7
  52. package/dist/plugins/c8/index.js +17 -4
  53. package/dist/plugins/changesets/index.js +3 -2
  54. package/dist/plugins/commitizen/index.js +6 -3
  55. package/dist/plugins/commitlint/index.js +10 -11
  56. package/dist/plugins/cypress/index.js +4 -2
  57. package/dist/plugins/danger/index.js +1 -1
  58. package/dist/plugins/docusaurus/helpers.js +6 -6
  59. package/dist/plugins/docusaurus/index.js +2 -2
  60. package/dist/plugins/electron-forge/index.js +2 -2
  61. package/dist/plugins/electron-vite/index.js +2 -2
  62. package/dist/plugins/eleventy/index.js +3 -2
  63. package/dist/plugins/eslint/helpers.js +4 -3
  64. package/dist/plugins/eslint/resolveFromAST.js +2 -2
  65. package/dist/plugins/evalite/index.d.ts +3 -0
  66. package/dist/plugins/evalite/index.js +26 -0
  67. package/dist/plugins/flue/index.js +2 -2
  68. package/dist/plugins/gatsby/index.js +1 -1
  69. package/dist/plugins/github-action/index.js +2 -2
  70. package/dist/plugins/github-actions/index.js +2 -2
  71. package/dist/plugins/graphql-codegen/index.js +13 -9
  72. package/dist/plugins/index.d.ts +5 -0
  73. package/dist/plugins/index.js +10 -0
  74. package/dist/plugins/jest/index.js +5 -5
  75. package/dist/plugins/knex/index.js +2 -2
  76. package/dist/plugins/laravel-vite-plugin/resolveFromAST.js +2 -2
  77. package/dist/plugins/lunaria/index.js +5 -2
  78. package/dist/plugins/markdownlint/index.js +8 -2
  79. package/dist/plugins/marko/index.js +2 -2
  80. package/dist/plugins/marko/taglibs.d.ts +1 -1
  81. package/dist/plugins/marko/taglibs.js +4 -28
  82. package/dist/plugins/mdx/index.d.ts +2 -1
  83. package/dist/plugins/mdx/index.js +7 -4
  84. package/dist/plugins/mdxlint/index.js +18 -15
  85. package/dist/plugins/mikro-orm/index.d.ts +3 -0
  86. package/dist/plugins/mikro-orm/index.js +77 -0
  87. package/dist/plugins/mikro-orm/types.d.ts +21 -0
  88. package/dist/plugins/mikro-orm/types.js +1 -0
  89. package/dist/plugins/msw/index.js +6 -3
  90. package/dist/plugins/msw/types.d.ts +1 -1
  91. package/dist/plugins/netlify/index.js +7 -3
  92. package/dist/plugins/next/index.js +63 -9
  93. package/dist/plugins/next/types.d.ts +5 -0
  94. package/dist/plugins/next-mdx/resolveFromAST.js +15 -10
  95. package/dist/plugins/nitro/index.js +5 -6
  96. package/dist/plugins/node/index.js +4 -4
  97. package/dist/plugins/npm-package-json-lint/index.js +10 -3
  98. package/dist/plugins/nuxt/index.js +18 -10
  99. package/dist/plugins/nuxt/types.d.ts +4 -0
  100. package/dist/plugins/nx/index.js +6 -3
  101. package/dist/plugins/openclaw/index.js +11 -11
  102. package/dist/plugins/oxlint/index.js +2 -2
  103. package/dist/plugins/payload/index.js +1 -1
  104. package/dist/plugins/playwright/index.js +3 -3
  105. package/dist/plugins/pm2/index.js +7 -7
  106. package/dist/plugins/prettier/index.js +2 -1
  107. package/dist/plugins/prisma/index.js +2 -2
  108. package/dist/plugins/quasar/resolveFromAST.js +14 -9
  109. package/dist/plugins/react-router/index.js +12 -15
  110. package/dist/plugins/release-it/index.js +6 -2
  111. package/dist/plugins/remix/index.js +3 -3
  112. package/dist/plugins/retry-cli/index.d.ts +3 -0
  113. package/dist/plugins/retry-cli/index.js +14 -0
  114. package/dist/plugins/rollup/index.js +1 -4
  115. package/dist/plugins/rstest/index.js +3 -3
  116. package/dist/plugins/semantic-release/index.js +3 -3
  117. package/dist/plugins/serverless-framework/index.js +10 -8
  118. package/dist/plugins/solidjs-vite-plugin/resolveFromAST.js +2 -2
  119. package/dist/plugins/stencil/resolveFromAST.js +5 -5
  120. package/dist/plugins/storybook/index.js +9 -3
  121. package/dist/plugins/stryker/index.js +2 -2
  122. package/dist/plugins/stylelint/index.js +2 -2
  123. package/dist/plugins/swc/index.js +4 -3
  124. package/dist/plugins/tailwind/index.js +2 -2
  125. package/dist/plugins/tanstack-router/index.js +2 -2
  126. package/dist/plugins/ts-node/index.js +6 -1
  127. package/dist/plugins/turbo/index.js +3 -3
  128. package/dist/plugins/typedoc/index.js +2 -2
  129. package/dist/plugins/typescript/content-mappers.d.ts +1 -1
  130. package/dist/plugins/typescript/index.js +6 -9
  131. package/dist/plugins/unocss/index.js +3 -0
  132. package/dist/plugins/varlock/scan.js +1 -1
  133. package/dist/plugins/vite/helpers.d.ts +1 -0
  134. package/dist/plugins/vite/helpers.js +13 -3
  135. package/dist/plugins/vite/index.js +5 -2
  136. package/dist/plugins/vite-plugin-pwa/resolveFromAST.js +2 -2
  137. package/dist/plugins/vitest/index.js +26 -18
  138. package/dist/plugins/vitest/types.d.ts +1 -0
  139. package/dist/plugins/vue/index.js +1 -1
  140. package/dist/plugins/web-test-runner/index.d.ts +3 -0
  141. package/dist/plugins/web-test-runner/index.js +86 -0
  142. package/dist/plugins/wrangler/index.js +3 -3
  143. package/dist/plugins/yarn/index.js +5 -5
  144. package/dist/plugins.js +2 -5
  145. package/dist/reporters/github-actions.js +3 -7
  146. package/dist/reporters/json.d.ts +1 -0
  147. package/dist/reporters/json.js +1 -0
  148. package/dist/reporters/util/configuration-hints.d.ts +3 -1
  149. package/dist/reporters/util/configuration-hints.js +40 -21
  150. package/dist/reporters/util/util.d.ts +1 -1
  151. package/dist/run-cli.d.ts +2 -0
  152. package/dist/run-cli.js +82 -0
  153. package/dist/run.js +12 -0
  154. package/dist/session/incremental.d.ts +1 -0
  155. package/dist/session/incremental.js +1 -0
  156. package/dist/session/watch.js +2 -1
  157. package/dist/types/PluginNames.d.ts +2 -2
  158. package/dist/types/PluginNames.js +5 -0
  159. package/dist/types/args.d.ts +3 -1
  160. package/dist/types/config.d.ts +17 -10
  161. package/dist/types/configuration.d.ts +45 -0
  162. package/dist/types/configuration.js +1 -0
  163. package/dist/types/issues.d.ts +4 -0
  164. package/dist/types/module-graph.d.ts +1 -0
  165. package/dist/types/options.d.ts +1 -0
  166. package/dist/types/package-json.d.ts +3 -8
  167. package/dist/types/workspace.d.ts +1 -0
  168. package/dist/typescript/get-imports-and-exports.js +18 -6
  169. package/dist/typescript/resolve-module-names.js +7 -45
  170. package/dist/typescript/visitors/calls.d.ts +1 -1
  171. package/dist/typescript/visitors/calls.js +14 -10
  172. package/dist/typescript/visitors/exports.js +16 -26
  173. package/dist/typescript/visitors/imports.d.ts +2 -1
  174. package/dist/typescript/visitors/imports.js +41 -24
  175. package/dist/typescript/visitors/members.d.ts +1 -1
  176. package/dist/typescript/visitors/members.js +38 -24
  177. package/dist/typescript/visitors/shadow.d.ts +2 -0
  178. package/dist/typescript/visitors/shadow.js +25 -0
  179. package/dist/typescript/visitors/walk.d.ts +20 -12
  180. package/dist/typescript/visitors/walk.js +78 -48
  181. package/dist/util/Performance.d.ts +25 -3
  182. package/dist/util/Performance.js +51 -27
  183. package/dist/util/cli-arguments.d.ts +2 -1
  184. package/dist/util/cli-arguments.js +5 -3
  185. package/dist/util/create-input-handler.js +138 -119
  186. package/dist/util/create-options.d.ts +4 -2071
  187. package/dist/util/create-options.js +6 -4
  188. package/dist/util/create-workspace-graph.js +2 -1
  189. package/dist/util/errors.d.ts +5 -2
  190. package/dist/util/errors.js +11 -18
  191. package/dist/util/fs.js +4 -2
  192. package/dist/util/input.d.ts +2 -2
  193. package/dist/util/input.js +2 -2
  194. package/dist/util/is-plugin-name.d.ts +2 -0
  195. package/dist/util/is-plugin-name.js +3 -0
  196. package/dist/util/jiti.d.ts +2 -1
  197. package/dist/util/jiti.js +11 -3
  198. package/dist/util/load-config.d.ts +1 -1
  199. package/dist/util/load-config.js +3 -2
  200. package/dist/util/loader.js +3 -2
  201. package/dist/util/map-workspaces.js +23 -1
  202. package/dist/util/module-graph.js +1 -0
  203. package/dist/util/modules.d.ts +2 -0
  204. package/dist/util/modules.js +78 -0
  205. package/dist/util/package-json.d.ts +12 -5
  206. package/dist/util/package-json.js +54 -26
  207. package/dist/util/parse-and-convert-gitignores.d.ts +2 -2
  208. package/dist/util/parse-and-convert-gitignores.js +5 -5
  209. package/dist/util/parse-configuration.d.ts +2 -0
  210. package/dist/util/parse-configuration.js +66 -0
  211. package/dist/util/plugin.js +8 -12
  212. package/dist/util/resolve.d.ts +1 -0
  213. package/dist/util/resolve.js +3 -2
  214. package/dist/util/scripts.d.ts +0 -5
  215. package/dist/util/scripts.js +1 -32
  216. package/dist/util/to-source-path.d.ts +4 -1
  217. package/dist/util/to-source-path.js +2 -2
  218. package/dist/util/unhandled-rejection.d.ts +8 -0
  219. package/dist/util/unhandled-rejection.js +29 -0
  220. package/dist/util/validation.d.ts +11 -0
  221. package/dist/util/validation.js +68 -0
  222. package/dist/util/watch.d.ts +3 -1
  223. package/dist/util/watch.js +5 -2
  224. package/dist/version.d.ts +1 -1
  225. package/dist/version.js +1 -1
  226. package/package.json +9 -10
  227. package/schema.json +34 -0
  228. package/dist/schema/configuration.d.ts +0 -3103
  229. package/dist/schema/configuration.js +0 -94
  230. package/dist/schema/plugins.d.ts +0 -1019
  231. package/dist/schema/plugins.js +0 -215
@@ -1,4 +1,4 @@
1
- import { PerformanceObserver, createHistogram, performance, } from 'node:perf_hooks';
1
+ import { createHistogram, performance } from 'node:perf_hooks';
2
2
  import { memoryUsage } from 'node:process';
3
3
  import { parseArgs } from 'node:util';
4
4
  import { Table } from './table.js';
@@ -22,6 +22,7 @@ const getMemInfo = (label) => {
22
22
  return { label, heapUsed: usage.heapUsed, heapTotal: usage.heapTotal, rss: usage.rss };
23
23
  };
24
24
  const twoFixed = (value) => (typeof value === 'number' ? value.toFixed(2) : value);
25
+ const formatDuration = (ms) => typeof ms === 'number' ? (ms < 0.01 ? `${(ms * 1000).toFixed(2)}µs` : `${ms.toFixed(2)}ms`) : String(ms);
25
26
  const inMB = (bytes) => bytes / 1024 / 1024;
26
27
  const keys = ['heapUsed', 'heapTotal', 'rss'];
27
28
  const logHead = () => console.log(['phase', ...keys].map(key => key.padStart(10)).join(' '));
@@ -32,21 +33,16 @@ class Performance {
32
33
  isMemoryUsageEnabled;
33
34
  isDurationEnabled;
34
35
  startTime = performance.now();
36
+ endTime;
35
37
  memId = `mem-${Math.floor(performance.now() * 100)}`;
36
38
  histograms = new Map();
37
39
  memEntries = [];
38
- memObserver;
39
40
  constructor({ isTimerifyFunctions = false, isMemoryUsageEnabled = false, isDurationEnabled = false }) {
40
41
  this.isEnabled = isTimerifyFunctions || isMemoryUsageEnabled;
41
42
  this.isTimerifyFunctions = isTimerifyFunctions;
42
43
  this.isMemoryUsageEnabled = isMemoryUsageEnabled;
43
44
  this.isDurationEnabled = isDurationEnabled;
44
45
  if (isMemoryUsageEnabled) {
45
- this.memObserver = new PerformanceObserver(items => {
46
- for (const entry of items.getEntries())
47
- this.memEntries.push(entry);
48
- });
49
- this.memObserver.observe({ type: 'mark' });
50
46
  if (isMemoryRealtime)
51
47
  logHead();
52
48
  this.addMemoryMark('start');
@@ -65,25 +61,45 @@ class Performance {
65
61
  return;
66
62
  const detail = getMemInfo(label);
67
63
  performance.mark(`${this.memId}:${label}`, { detail });
64
+ this.memEntries.push(detail);
68
65
  if (isMemoryRealtime)
69
66
  log(detail);
70
67
  }
71
- getTimerifiedFunctionsTable() {
72
- const totalDuration = this.getCurrentDurationInMs();
73
- const table = new Table({ header: true });
68
+ getSnapshot() {
69
+ const processMs = this.getProcessDurationInMs();
70
+ const functions = [];
74
71
  for (const [name, h] of this.histograms) {
75
72
  if (h.count === 0)
76
73
  continue;
77
74
  const meanMs = h.mean / 1e6;
78
- const sumMs = meanMs * h.count;
75
+ functions.push({
76
+ name,
77
+ count: h.count,
78
+ minMs: h.min / 1e6,
79
+ maxMs: h.max / 1e6,
80
+ meanMs,
81
+ medianMs: h.percentile(50) / 1e6,
82
+ p95Ms: h.percentile(95) / 1e6,
83
+ sumMs: meanMs * h.count,
84
+ });
85
+ }
86
+ const memory = [];
87
+ for (const entry of this.memEntries)
88
+ memory.push({ ...entry });
89
+ return { durationMs: processMs - this.startTime, processMs, functions, memory };
90
+ }
91
+ getTimerifiedFunctionsTable() {
92
+ const { durationMs, functions } = this.getSnapshot();
93
+ const table = new Table({ header: true });
94
+ for (const entry of functions) {
79
95
  table.row();
80
- table.cell('Name', name);
81
- table.cell('size', h.count);
82
- table.cell('min', h.min / 1e6, twoFixed);
83
- table.cell('max', h.max / 1e6, twoFixed);
84
- table.cell('median', h.percentile(50) / 1e6, twoFixed);
85
- table.cell('sum', sumMs, twoFixed);
86
- table.cell('%', (sumMs / totalDuration) * 100, v => (typeof v === 'number' ? `${v.toFixed(0)}%` : ''));
96
+ table.cell('Name', entry.name);
97
+ table.cell('size', entry.count);
98
+ table.cell('min', entry.minMs, formatDuration);
99
+ table.cell('max', entry.maxMs, formatDuration);
100
+ table.cell('median', entry.medianMs, formatDuration);
101
+ table.cell('sum', entry.sumMs, formatDuration);
102
+ table.cell('%', (entry.sumMs / durationMs) * 100, v => (typeof v === 'number' ? `${v.toFixed(0)}%` : ''));
87
103
  }
88
104
  table.sort('sum', 'desc');
89
105
  return table.toString();
@@ -94,9 +110,7 @@ class Performance {
94
110
  let peakHeapUsed = 0;
95
111
  let peakRss = 0;
96
112
  for (const entry of this.memEntries) {
97
- if (!entry.detail)
98
- continue;
99
- const { label, heapUsed, rss } = entry.detail;
113
+ const { label, heapUsed, rss } = entry;
100
114
  const delta = heapUsed - prevHeapUsed;
101
115
  if (heapUsed > peakHeapUsed)
102
116
  peakHeapUsed = heapUsed;
@@ -117,7 +131,14 @@ class Performance {
117
131
  return table.toString();
118
132
  }
119
133
  getCurrentDurationInMs() {
120
- return performance.now() - this.startTime;
134
+ return this.getProcessDurationInMs() - this.startTime;
135
+ }
136
+ getProcessDurationInMs() {
137
+ return this.endTime ?? performance.now();
138
+ }
139
+ startRun() {
140
+ this.startTime = performance.now();
141
+ this.endTime = undefined;
121
142
  }
122
143
  getMemHeapUsage() {
123
144
  return memoryUsage().heapUsed;
@@ -125,16 +146,19 @@ class Performance {
125
146
  getCurrentMemUsageInMb() {
126
147
  return twoFixed(inMB(this.getMemHeapUsage()));
127
148
  }
128
- async finalize() {
129
- if (!this.isEnabled)
149
+ finalize() {
150
+ if (!this.isEnabled && !this.isDurationEnabled)
130
151
  return;
152
+ this.endTime = performance.now();
131
153
  this.addMemoryMark('end');
132
- await new Promise(resolve => setTimeout(resolve, 1));
133
154
  }
134
155
  reset() {
135
- this.histograms.clear();
156
+ for (const histogram of this.histograms.values())
157
+ histogram.reset();
158
+ for (const entry of this.memEntries)
159
+ performance.clearMarks(`${this.memId}:${entry.label}`);
136
160
  this.memEntries.length = 0;
137
- this.memObserver?.disconnect();
161
+ this.startRun();
138
162
  }
139
163
  }
140
164
  export const perfObserver = new Performance({ isTimerifyFunctions, isMemoryUsageEnabled, isDurationEnabled });
@@ -1,4 +1,4 @@
1
- export declare const helpText = "\u2702\uFE0F Find unused dependencies, exports and files in your JavaScript and TypeScript projects\n\nUsage: knip [options]\n\nOptions:\n -h, --help Print this help text\n -V, --version Print version\n -n, --no-progress Don't show dynamic progress updates (automatically enabled in CI environments)\n -c, --config [file] Configuration file path\n (default: [.]knip.json[c], knip.(js|ts), knip.config.(js|ts) or package.json#knip)\n --use-tsconfig-files Use tsconfig.json to define project files (override `project` patterns)\n -t, --tsConfig [file] TypeScript configuration path (default: tsconfig.json)\n\nMode\n --cache Enable caching\n --cache-location Change cache location (default: node_modules/.cache/knip)\n --include-entry-exports Include entry files when reporting unused exports\n --no-gitignore Don't respect .gitignore\n -p, --production Analyze only production source files (e.g. no test files, devDependencies)\n -s, --strict Consider only direct dependencies of workspace (not devDependencies, not other workspaces)\n -w, --watch Watch mode\n\nScope\n -W, --workspace [filter] Filter workspaces by name, directory, or glob (can be repeated)\n -D, --directory [dir] Run process from a different directory (default: cwd)\n --include Include 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,binaries,unresolved,catalog,catalogReferences\n --exports Shortcut for --include exports,nsExports,types,nsTypes,enumMembers,namespaceMembers,duplicates\n --files Shortcut for --include files\n --cycles Shortcut for --include cycles (circular dependencies)\n --tags Include or exclude tagged exports\n\nFix\n -f, --fix Fix issues (modifies files in your repo)\n --fix-type Fix only issues of type, can be comma-separated or repeated (2)\n --allow-remove-files Allow Knip to remove files (with --fix)\n -F, --format Format modified files after --fix using the local formatter\n\nOutput\n --preprocessor Preprocess the results before providing it to the reporter(s), can be repeated\n --preprocessor-options Pass extra options to the preprocessor (as JSON string, see --reporter-options example)\n --reporter Select reporter (default: symbols), can be repeated (3)\n --reporter-options Pass extra options to the reporter (as JSON string, see example)\n --output-file Write reporter output to a file instead of stdout ([reporter=]path), can be repeated\n --no-config-hints Suppress configuration hints\n --no-tag-hints Suppress tag hints\n --treat-config-hints-as-errors Exit with non-zero code (1) if there are any configuration hints\n --treat-tag-hints-as-errors Exit with non-zero code (1) if there are any tag hints\n --max-issues Maximum number of total issues before non-zero exit code (default: 0)\n --max-show-issues Maximum number of issues to display per type\n --no-exit-code Always exit with code zero (0)\n\nTroubleshooting\n -d, --debug Show debug output\n --memory Measure memory usage and display data table\n --memory-realtime Log memory usage in realtime\n --performance Measure count and running time of key functions and display stats table\n --performance-fn [name] Measure only function [name]\n -u, --duration Print total running time (zero overhead, no instrumentation)\n --trace Show trace output\n --trace-dependency [name] Show files that import the named dependency\n --trace-export [name] Show trace output for named export(s)\n --trace-file [file] Show trace output for exports in file\n\n(1) Issue types: files, dependencies, unlisted, unresolved, exports, nsExports, types, nsTypes, enumMembers, namespaceMembers, duplicates, catalog, catalogReferences, cycles\n(2) Fixable issue types: dependencies, exports, types, files, catalog\n(3) Built-in reporters: symbols (default), compact, codeowners, cycles, json, codeclimate, markdown, disclosure, github-actions, sarif\n\nExamples:\n\n$ knip\n$ knip --production\n$ knip --workspace packages/client --include files,dependencies\n$ knip --workspace @myorg/* --workspace '!@myorg/legacy'\n$ knip --workspace './apps/*' --workspace '@shared/utils'\n$ knip -c ./config/knip.json --reporter compact\n$ knip --reporter codeowners --reporter-options '{\"path\":\".github/CODEOWNERS\"}'\n$ knip --reporter symbols --reporter json --output-file json=knip.json\n$ knip --tags=-lintignore\n\nWebsite: https://knip.dev";
1
+ export declare const helpText = "\u2702\uFE0F Find unused dependencies, exports and files in your JavaScript and TypeScript projects\n\nUsage: knip [options]\n\nOptions:\n -h, --help Print this help text\n -V, --version Print version\n -n, --no-progress Don't show dynamic progress updates (automatically enabled in CI environments)\n -c, --config [file] Configuration file path\n (default: [.]knip.json[c], knip.(js|ts), knip.config.(js|ts) or package.json#knip)\n --use-tsconfig-files Use tsconfig.json to define project files (override `project` patterns)\n -t, --tsConfig [file] TypeScript configuration path (default: tsconfig.json)\n\nMode\n --cache Enable caching\n --cache-location Change cache location (default: node_modules/.cache/knip)\n --include-entry-exports Include entry files when reporting unused exports\n --include-subpath-exports Report unused subpaths in package.json#exports\n --no-gitignore Don't respect .gitignore\n -p, --production Analyze only production source files (e.g. no test files, devDependencies)\n -s, --strict Consider only direct dependencies of workspace (not devDependencies, not other workspaces)\n -w, --watch Watch mode\n\nScope\n -W, --workspace [filter] Filter workspaces by name, directory, or glob (can be repeated)\n -D, --directory [dir] Run process from a different directory (default: cwd)\n --include Include 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,binaries,unresolved,catalog,catalogReferences\n --exports Shortcut for --include exports,nsExports,types,nsTypes,enumMembers,namespaceMembers,duplicates\n --files Shortcut for --include files\n --cycles Shortcut for --include cycles (circular dependencies)\n --tags Include or exclude tagged exports\n\nFix\n -f, --fix Fix issues (modifies files in your repo)\n --fix-type Fix only issues of type, can be comma-separated or repeated (2)\n --allow-remove-files Allow Knip to remove files (with --fix)\n -F, --format Format modified files after --fix using the local formatter\n\nOutput\n --preprocessor Preprocess the results before providing them to the reporter(s), can be repeated\n --preprocessor-options Pass extra options to the preprocessor (as JSON string, see --reporter-options example)\n --reporter Select reporter (default: symbols), can be repeated (3)\n --reporter-options Pass extra options to the reporter (as JSON string, see example)\n --output-file Write reporter output to a file instead of stdout ([reporter=]path), can be repeated\n --no-config-hints Suppress configuration hints\n --no-tag-hints Suppress tag hints\n --treat-config-hints-as-errors Exit with non-zero code (1) if there are any configuration hints\n --treat-tag-hints-as-errors Exit with non-zero code (1) if there are any tag hints\n --max-issues Maximum number of total issues before non-zero exit code (default: 0)\n --max-show-issues Maximum number of issues to display per type\n --no-exit-code Always exit with code zero (0)\n\nTroubleshooting\n -d, --debug Show debug output\n --memory Measure memory usage and display data table\n --memory-realtime Log memory usage in realtime\n --performance Measure count and running time of key functions and display stats table\n --performance-fn [name] Measure only function [name]\n -u, --duration Print run time and startup overhead (no instrumentation)\n --trace Show trace output\n --trace-dependency [name] Show files that import the named dependency\n --trace-export [name] Show trace output for named export(s)\n --trace-file [file] Show trace output for exports in file\n\n(1) Issue types: files, subpaths, dependencies, unlisted, unresolved, exports, nsExports, types, nsTypes, enumMembers, namespaceMembers, duplicates, catalog, catalogReferences, cycles\n(2) Fixable issue types: dependencies, exports, types, files, catalog\n(3) Built-in reporters: symbols (default), compact, codeowners, cycles, json, codeclimate, markdown, disclosure, github-actions, sarif\n\nExamples:\n\n$ knip\n$ knip --production\n$ knip --workspace packages/client --include files,dependencies\n$ knip --workspace @myorg/* --workspace '!@myorg/legacy'\n$ knip --workspace './apps/*' --workspace '@shared/utils'\n$ knip -c ./config/knip.json --reporter compact\n$ knip --reporter codeowners --reporter-options '{\"path\":\".github/CODEOWNERS\"}'\n$ knip --reporter symbols --reporter json --output-file json=knip.json\n$ knip --tags=-lintignore\n\nWebsite: https://knip.dev";
2
2
  export type ParsedCLIArgs = ReturnType<typeof parseCLIArgs>;
3
3
  export declare const parseNumericOption: (value: string | undefined, option: string) => number | undefined;
4
4
  export default function parseCLIArgs(): {
@@ -20,6 +20,7 @@ export default function parseCLIArgs(): {
20
20
  help?: boolean | undefined;
21
21
  include?: string[] | undefined;
22
22
  'include-entry-exports'?: boolean | undefined;
23
+ 'include-subpath-exports'?: boolean | undefined;
23
24
  'max-issues'?: string | undefined;
24
25
  'max-show-issues'?: string | undefined;
25
26
  memory?: boolean | undefined;
@@ -17,6 +17,7 @@ Mode
17
17
  --cache Enable caching
18
18
  --cache-location Change cache location (default: node_modules/.cache/knip)
19
19
  --include-entry-exports Include entry files when reporting unused exports
20
+ --include-subpath-exports Report unused subpaths in package.json#exports
20
21
  --no-gitignore Don't respect .gitignore
21
22
  -p, --production Analyze only production source files (e.g. no test files, devDependencies)
22
23
  -s, --strict Consider only direct dependencies of workspace (not devDependencies, not other workspaces)
@@ -40,7 +41,7 @@ Fix
40
41
  -F, --format Format modified files after --fix using the local formatter
41
42
 
42
43
  Output
43
- --preprocessor Preprocess the results before providing it to the reporter(s), can be repeated
44
+ --preprocessor Preprocess the results before providing them to the reporter(s), can be repeated
44
45
  --preprocessor-options Pass extra options to the preprocessor (as JSON string, see --reporter-options example)
45
46
  --reporter Select reporter (default: symbols), can be repeated (3)
46
47
  --reporter-options Pass extra options to the reporter (as JSON string, see example)
@@ -59,13 +60,13 @@ Troubleshooting
59
60
  --memory-realtime Log memory usage in realtime
60
61
  --performance Measure count and running time of key functions and display stats table
61
62
  --performance-fn [name] Measure only function [name]
62
- -u, --duration Print total running time (zero overhead, no instrumentation)
63
+ -u, --duration Print run time and startup overhead (no instrumentation)
63
64
  --trace Show trace output
64
65
  --trace-dependency [name] Show files that import the named dependency
65
66
  --trace-export [name] Show trace output for named export(s)
66
67
  --trace-file [file] Show trace output for exports in file
67
68
 
68
- (1) Issue types: files, dependencies, unlisted, unresolved, exports, nsExports, types, nsTypes, enumMembers, namespaceMembers, duplicates, catalog, catalogReferences, cycles
69
+ (1) Issue types: files, subpaths, dependencies, unlisted, unresolved, exports, nsExports, types, nsTypes, enumMembers, namespaceMembers, duplicates, catalog, catalogReferences, cycles
69
70
  (2) Fixable issue types: dependencies, exports, types, files, catalog
70
71
  (3) Built-in reporters: symbols (default), compact, codeowners, cycles, json, codeclimate, markdown, disclosure, github-actions, sarif
71
72
 
@@ -111,6 +112,7 @@ export default function parseCLIArgs() {
111
112
  help: { type: 'boolean', short: 'h' },
112
113
  include: { type: 'string', multiple: true },
113
114
  'include-entry-exports': { type: 'boolean' },
115
+ 'include-subpath-exports': { type: 'boolean' },
114
116
  'max-issues': { type: 'string' },
115
117
  'max-show-issues': { type: 'string' },
116
118
  memory: { type: 'boolean' },
@@ -1,134 +1,153 @@
1
- import { IGNORED_GLOBAL_BINARIES, IGNORED_RUNTIME_DEPENDENCIES } from '../constants.js';
1
+ import { IGNORED_GLOBAL_BINARIES } from '../constants.js';
2
2
  import { debugLog } from './debug.js';
3
- import { fromBinary, isBinary, isConfig, isDeferResolve, isDeferResolveEntry, isDependency, toDebugString, } from './input.js';
4
- import { getPackageNameFromSpecifier } from './modules.js';
3
+ import { fromBinary, isConfig, isDeferResolveEntry, toDebugString } from './input.js';
4
+ import { getPackageNameFromFilePath, getPackageNameFromSpecifier } from './modules.js';
5
5
  import { dirname, isAbsolute, isInNodeModules, isInternal, join } from './path.js';
6
- import { _resolveModuleSync, _resolveSync } from './resolve.js';
6
+ import { _resolveModuleSync, _resolveSync, resolvePackageManifestPath } from './resolve.js';
7
+ const CHAR_HASH = 35;
8
+ const CHAR_AT = 64;
9
+ const CHAR_TILDE = 126;
7
10
  const isJoinable = (specifier) => {
8
11
  const char = specifier.charCodeAt(0);
9
- return char !== 35 && char !== 126 && char !== 64 && !isAbsolute(specifier);
12
+ return char !== CHAR_HASH && char !== CHAR_TILDE && char !== CHAR_AT && !isAbsolute(specifier);
10
13
  };
11
- const getWorkspaceFor = (input, chief, workspace) => (input.dir && chief.findWorkspaceByFilePath(`${input.dir}/`)) ||
12
- (input.containingFilePath && chief.findWorkspaceByFilePath(input.containingFilePath)) ||
13
- workspace;
14
- const addExternalRef = (map, containingFilePath, ref) => {
15
- if (!map.has(containingFilePath))
16
- map.set(containingFilePath, new Set());
17
- map.get(containingFilePath).add(ref);
18
- };
19
- export const createInputHandler = (deputy, chief, isGitIgnored, addIssue, externalRefs, options) => (input, workspace) => {
20
- const { specifier, containingFilePath } = input;
21
- if (!containingFilePath)
22
- return;
23
- if (isBinary(input)) {
24
- const binaryName = fromBinary(input);
25
- const inputWorkspace = getWorkspaceFor(input, chief, workspace);
26
- const dependencies = deputy.maybeAddReferencedBinary(inputWorkspace, binaryName);
27
- if (dependencies) {
28
- if (externalRefs) {
29
- for (const dependency of dependencies) {
30
- addExternalRef(externalRefs, containingFilePath, { specifier: dependency, identifier: binaryName });
31
- }
32
- }
14
+ export const createInputHandler = (deputy, chief, isGitIgnored, addIssue, externalRefs, options) => {
15
+ const getWorkspaceFor = (input, containingFilePath, workspace) => (input.dir && chief.findWorkspaceByFilePath(`${input.dir}/`)) ||
16
+ chief.findWorkspaceByFilePath(containingFilePath) ||
17
+ workspace;
18
+ const addExternalRef = (containingFilePath, specifier, identifier) => {
19
+ if (!externalRefs)
33
20
  return;
21
+ const ref = { specifier, identifier };
22
+ const refs = externalRefs.get(containingFilePath);
23
+ if (refs)
24
+ refs.add(ref);
25
+ else
26
+ externalRefs.set(containingFilePath, new Set([ref]));
27
+ };
28
+ const isReportable = (input) => !input.optional && (!options.isProduction || input.production);
29
+ const report = (type, filePath, workspace, symbol) => addIssue({ type, filePath, workspace: workspace.name, symbol, specifier: symbol, fixes: [] });
30
+ const credit = (input, containingFilePath, workspace, packageName) => {
31
+ const reference = {
32
+ specifier: input.specifier,
33
+ isDevOnly: isConfig(input),
34
+ isTypeOnly: input.isTypeOnly,
35
+ isDevelopment: !input.production,
36
+ };
37
+ const isHandled = deputy.maybeAddReferencedExternalDependency(workspace, packageName, reference);
38
+ if (!input.alsoReferenceOwner)
39
+ return isHandled;
40
+ const owner = chief.findWorkspaceByFilePath(containingFilePath);
41
+ if (!owner || owner === workspace)
42
+ return isHandled;
43
+ const isOwnerHandled = deputy.maybeAddReferencedExternalDependency(owner, packageName, reference);
44
+ return isHandled || isOwnerHandled;
45
+ };
46
+ const resolveWorkspacePath = (specifier, containingFilePath) => {
47
+ const filePath = _resolveSync(specifier, dirname(containingFilePath));
48
+ if (filePath && isInternal(filePath) && !isGitIgnored(filePath))
49
+ return filePath;
50
+ };
51
+ const handleFile = (input, containingFilePath, workspace, recordedPackageName) => {
52
+ const { specifier } = input;
53
+ const basePath = input.dir ? join(input.dir, 'file.ts') : containingFilePath;
54
+ const filePath = isJoinable(specifier) ? join(input.dir ?? dirname(containingFilePath), specifier) : specifier;
55
+ const resolvedFilePath = _resolveModuleSync(filePath, basePath) ??
56
+ (filePath === specifier ? undefined : _resolveModuleSync(specifier, basePath));
57
+ if (resolvedFilePath && isInternal(resolvedFilePath)) {
58
+ return isGitIgnored(resolvedFilePath) ? undefined : resolvedFilePath;
34
59
  }
35
- if (input.optional || (inputWorkspace.config.ignoreGlobalBinaries && IGNORED_GLOBAL_BINARIES.has(binaryName)))
36
- return;
37
- addIssue({
38
- type: 'binaries',
39
- filePath: containingFilePath,
40
- workspace: inputWorkspace.name,
41
- symbol: binaryName,
42
- specifier,
43
- fixes: [],
44
- });
45
- return;
46
- }
47
- if (IGNORED_RUNTIME_DEPENDENCIES.has(specifier))
48
- return;
49
- const packageName = getPackageNameFromSpecifier(specifier);
50
- if (packageName &&
51
- (isDependency(input) ||
52
- isDeferResolve(input) ||
53
- (isDeferResolveEntry(input) && isInNodeModules(specifier)) ||
54
- isConfig(input))) {
55
- const isWorkspace = chief.workspacesByPkgName.has(packageName);
56
- const inputWorkspace = getWorkspaceFor(input, chief, workspace);
57
- if (inputWorkspace) {
58
- let isHandled = deputy.maybeAddReferencedExternalDependency(inputWorkspace, packageName, {
59
- specifier,
60
- isDevOnly: isConfig(input),
61
- isTypeOnly: input.isTypeOnly,
62
- });
63
- if (input.alsoReferenceOwner && input.containingFilePath) {
64
- const owningWorkspace = chief.findWorkspaceByFilePath(input.containingFilePath);
65
- if (owningWorkspace && owningWorkspace !== inputWorkspace) {
66
- const isOwnerHandled = deputy.maybeAddReferencedExternalDependency(owningWorkspace, packageName, {
67
- specifier,
68
- isDevOnly: isConfig(input),
69
- isTypeOnly: input.isTypeOnly,
70
- });
71
- isHandled = isHandled || isOwnerHandled;
60
+ const inputWorkspace = getWorkspaceFor(input, containingFilePath, workspace);
61
+ if (resolvedFilePath || !isInternal(filePath)) {
62
+ const packageName = resolvedFilePath
63
+ ? getPackageNameFromFilePath(resolvedFilePath)
64
+ : getPackageNameFromSpecifier(specifier);
65
+ if (packageName) {
66
+ if (packageName !== recordedPackageName && !chief.findWorkspaceByPackageName(packageName, containingFilePath)) {
67
+ addExternalRef(containingFilePath, packageName);
72
68
  }
73
- }
74
- if (externalRefs && !isWorkspace) {
75
- addExternalRef(externalRefs, containingFilePath, { specifier: packageName, identifier: undefined });
76
- }
77
- if (isWorkspace || isDependency(input)) {
78
- if (!isHandled) {
79
- if (!input.optional && ((options.isProduction && input.production) || !options.isProduction)) {
80
- addIssue({
81
- type: 'unlisted',
82
- filePath: containingFilePath,
83
- workspace: inputWorkspace.name,
84
- symbol: packageName,
85
- specifier: packageName,
86
- fixes: [],
87
- });
88
- }
89
- return;
69
+ if (!credit(input, containingFilePath, inputWorkspace, packageName) && isReportable(input)) {
70
+ report('unlisted', containingFilePath, inputWorkspace, packageName);
90
71
  }
91
- const internalPath = _resolveSync(specifier, dirname(containingFilePath));
92
- if (internalPath && isInternal(internalPath) && !isGitIgnored(internalPath))
93
- return internalPath;
94
- }
95
- if (isHandled)
96
72
  return;
73
+ }
97
74
  }
98
- }
99
- if (isDeferResolve(input) && deputy.isProduction && !input.production) {
100
- return;
101
- }
102
- const filePath = isJoinable(specifier) ? join(input.dir ?? dirname(containingFilePath), specifier) : specifier;
103
- const basePath = input.dir ? join(input.dir, 'file.ts') : containingFilePath;
104
- const resolvedFilePath = _resolveModuleSync(filePath, basePath);
105
- if (resolvedFilePath && isInternal(resolvedFilePath)) {
106
- return isGitIgnored(resolvedFilePath) ? undefined : resolvedFilePath;
107
- }
108
- if (input.optional)
109
- return;
110
- if (!isInternal(filePath)) {
111
- addIssue({
112
- type: 'unlisted',
113
- filePath: containingFilePath,
114
- workspace: workspace.name,
115
- symbol: packageName ?? specifier,
116
- specifier: packageName ?? specifier,
117
- fixes: [],
118
- });
119
- }
120
- else if (!isGitIgnored(filePath)) {
121
- if (!isDeferResolveEntry(input) && !isConfig(input)) {
122
- addIssue({
123
- type: 'unresolved',
124
- filePath: containingFilePath,
125
- workspace: workspace.name,
126
- symbol: specifier,
127
- fixes: [],
128
- });
75
+ if ((isInternal(filePath) && isGitIgnored(filePath)) || !isReportable(input))
76
+ return;
77
+ if (isDeferResolveEntry(input) || isConfig(input)) {
78
+ debugLog(inputWorkspace.name, `Unable to resolve ${toDebugString(input, options.cwd)}`);
129
79
  }
130
80
  else {
131
- debugLog(workspace.name, `Unable to resolve ${toDebugString(input, options.cwd)}`);
81
+ report('unresolved', containingFilePath, inputWorkspace, specifier);
82
+ }
83
+ };
84
+ const handleBinary = (input, containingFilePath, workspace) => {
85
+ const binaryName = fromBinary(input);
86
+ const inputWorkspace = getWorkspaceFor(input, containingFilePath, workspace);
87
+ const dependencies = deputy.maybeAddReferencedBinary(inputWorkspace, binaryName, !input.production);
88
+ if (dependencies) {
89
+ for (const dependency of dependencies) {
90
+ addExternalRef(containingFilePath, dependency, binaryName);
91
+ }
92
+ return;
93
+ }
94
+ if (!isReportable(input) ||
95
+ (inputWorkspace.config.ignoreGlobalBinaries && IGNORED_GLOBAL_BINARIES.has(binaryName))) {
96
+ return;
97
+ }
98
+ report('binaries', containingFilePath, inputWorkspace, binaryName);
99
+ };
100
+ const handleDependency = (input, containingFilePath, workspace) => {
101
+ const packageName = getPackageNameFromSpecifier(input.specifier);
102
+ if (!packageName)
103
+ return handleFile(input, containingFilePath, workspace);
104
+ const inputWorkspace = getWorkspaceFor(input, containingFilePath, workspace);
105
+ if (externalRefs && !chief.findWorkspaceByPackageName(packageName, containingFilePath)) {
106
+ addExternalRef(containingFilePath, packageName);
107
+ }
108
+ if (credit(input, containingFilePath, inputWorkspace, packageName)) {
109
+ return resolveWorkspacePath(input.specifier, containingFilePath);
110
+ }
111
+ if (isReportable(input))
112
+ report('unlisted', containingFilePath, inputWorkspace, packageName);
113
+ };
114
+ const handlePackageOrFile = (input, containingFilePath, workspace) => {
115
+ const packageName = getPackageNameFromSpecifier(input.specifier);
116
+ if (!packageName)
117
+ return handleFile(input, containingFilePath, workspace);
118
+ const inputWorkspace = getWorkspaceFor(input, containingFilePath, workspace);
119
+ const isWorkspace = chief.findWorkspaceByPackageName(packageName, containingFilePath) !== undefined;
120
+ if (!isWorkspace)
121
+ addExternalRef(containingFilePath, packageName);
122
+ if (credit(input, containingFilePath, inputWorkspace, packageName)) {
123
+ return isWorkspace ? resolveWorkspacePath(input.specifier, containingFilePath) : undefined;
124
+ }
125
+ if (isWorkspace || resolvePackageManifestPath(packageName, input.dir ?? dirname(containingFilePath))) {
126
+ if (isReportable(input))
127
+ report('unlisted', containingFilePath, inputWorkspace, packageName);
128
+ return;
129
+ }
130
+ return handleFile(input, containingFilePath, workspace, packageName);
131
+ };
132
+ return (input, workspace) => {
133
+ const { specifier, containingFilePath } = input;
134
+ if (!containingFilePath)
135
+ return;
136
+ switch (input.type) {
137
+ case 'binary':
138
+ handleBinary(input, containingFilePath, workspace);
139
+ return;
140
+ case 'dependency':
141
+ return handleDependency(input, containingFilePath, workspace);
142
+ case 'deferResolve':
143
+ case 'config':
144
+ return handlePackageOrFile(input, containingFilePath, workspace);
145
+ case 'deferResolveEntry':
146
+ return isInNodeModules(specifier)
147
+ ? handlePackageOrFile(input, containingFilePath, workspace)
148
+ : handleFile(input, containingFilePath, workspace);
149
+ case 'entry':
150
+ return handleFile(input, containingFilePath, workspace);
132
151
  }
133
- }
152
+ };
134
153
  };