knip 6.44.2 → 6.45.1

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 (226) 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/npm.js +7 -5
  21. package/dist/binaries/resolvers/npx.js +8 -40
  22. package/dist/binaries/util.d.ts +13 -1
  23. package/dist/binaries/util.js +35 -1
  24. package/dist/cli.js +10 -84
  25. package/dist/configuration-hints.d.ts +16 -0
  26. package/dist/configuration-hints.js +64 -0
  27. package/dist/constants.d.ts +2 -1
  28. package/dist/constants.js +2 -0
  29. package/dist/graph/SubpathReferences.d.ts +33 -0
  30. package/dist/graph/SubpathReferences.js +114 -0
  31. package/dist/graph/analyze.d.ts +4 -1
  32. package/dist/graph/analyze.js +16 -5
  33. package/dist/graph/build.d.ts +3 -1
  34. package/dist/graph/build.js +47 -23
  35. package/dist/graph-explorer/operations/resolve-export-origins.js +1 -1
  36. package/dist/plugins/angular/index.js +10 -17
  37. package/dist/plugins/aws-cdk/index.d.ts +3 -0
  38. package/dist/plugins/aws-cdk/index.js +32 -0
  39. package/dist/plugins/aws-cdk/types.d.ts +5 -0
  40. package/dist/plugins/aws-cdk/types.js +1 -0
  41. package/dist/plugins/babel/index.d.ts +1 -1
  42. package/dist/plugins/babel/index.js +19 -5
  43. package/dist/plugins/bun/index.js +8 -7
  44. package/dist/plugins/c8/index.js +17 -4
  45. package/dist/plugins/changesets/index.js +3 -2
  46. package/dist/plugins/commitizen/index.js +6 -3
  47. package/dist/plugins/commitlint/index.js +10 -11
  48. package/dist/plugins/cypress/index.js +4 -2
  49. package/dist/plugins/danger/index.js +1 -1
  50. package/dist/plugins/docusaurus/helpers.js +6 -6
  51. package/dist/plugins/docusaurus/index.js +2 -2
  52. package/dist/plugins/electron-forge/index.js +2 -2
  53. package/dist/plugins/electron-vite/index.js +2 -2
  54. package/dist/plugins/eleventy/index.js +3 -2
  55. package/dist/plugins/eslint/helpers.js +4 -3
  56. package/dist/plugins/eslint/resolveFromAST.js +2 -2
  57. package/dist/plugins/evalite/index.d.ts +3 -0
  58. package/dist/plugins/evalite/index.js +26 -0
  59. package/dist/plugins/flue/index.js +2 -2
  60. package/dist/plugins/github-action/index.js +2 -2
  61. package/dist/plugins/github-actions/index.js +2 -2
  62. package/dist/plugins/graphql-codegen/index.js +13 -9
  63. package/dist/plugins/index.d.ts +5 -0
  64. package/dist/plugins/index.js +10 -0
  65. package/dist/plugins/jest/index.js +5 -5
  66. package/dist/plugins/knex/index.js +2 -2
  67. package/dist/plugins/laravel-vite-plugin/resolveFromAST.js +2 -2
  68. package/dist/plugins/lunaria/index.js +5 -2
  69. package/dist/plugins/markdownlint/index.js +8 -2
  70. package/dist/plugins/marko/index.js +2 -2
  71. package/dist/plugins/marko/taglibs.d.ts +1 -1
  72. package/dist/plugins/marko/taglibs.js +4 -28
  73. package/dist/plugins/mdx/index.d.ts +2 -1
  74. package/dist/plugins/mdx/index.js +7 -4
  75. package/dist/plugins/mdxlint/index.js +18 -15
  76. package/dist/plugins/mikro-orm/index.d.ts +3 -0
  77. package/dist/plugins/mikro-orm/index.js +77 -0
  78. package/dist/plugins/mikro-orm/types.d.ts +21 -0
  79. package/dist/plugins/mikro-orm/types.js +1 -0
  80. package/dist/plugins/msw/index.js +6 -3
  81. package/dist/plugins/msw/types.d.ts +1 -1
  82. package/dist/plugins/netlify/index.js +7 -3
  83. package/dist/plugins/next/index.js +63 -9
  84. package/dist/plugins/next/types.d.ts +5 -0
  85. package/dist/plugins/next-mdx/resolveFromAST.js +15 -10
  86. package/dist/plugins/nitro/index.js +5 -6
  87. package/dist/plugins/node/index.js +4 -4
  88. package/dist/plugins/npm-package-json-lint/index.js +10 -3
  89. package/dist/plugins/nuxt/index.js +18 -10
  90. package/dist/plugins/nuxt/types.d.ts +4 -0
  91. package/dist/plugins/nx/index.js +6 -3
  92. package/dist/plugins/openclaw/index.js +11 -11
  93. package/dist/plugins/oxlint/index.js +2 -2
  94. package/dist/plugins/panda-css/index.js +5 -0
  95. package/dist/plugins/payload/index.js +1 -1
  96. package/dist/plugins/playwright/index.js +3 -3
  97. package/dist/plugins/pm2/index.js +7 -7
  98. package/dist/plugins/prettier/index.js +2 -1
  99. package/dist/plugins/prisma/index.js +2 -2
  100. package/dist/plugins/quasar/resolveFromAST.js +14 -9
  101. package/dist/plugins/react-router/index.js +2 -2
  102. package/dist/plugins/release-it/index.js +6 -2
  103. package/dist/plugins/remix/index.js +3 -3
  104. package/dist/plugins/retry-cli/index.d.ts +3 -0
  105. package/dist/plugins/retry-cli/index.js +14 -0
  106. package/dist/plugins/rollup/index.js +1 -4
  107. package/dist/plugins/rstest/index.js +3 -3
  108. package/dist/plugins/semantic-release/index.js +3 -3
  109. package/dist/plugins/serverless-framework/index.js +10 -8
  110. package/dist/plugins/solidjs-vite-plugin/resolveFromAST.js +2 -2
  111. package/dist/plugins/stencil/resolveFromAST.js +5 -5
  112. package/dist/plugins/stryker/index.js +2 -2
  113. package/dist/plugins/swc/index.js +4 -3
  114. package/dist/plugins/tailwind/index.js +2 -2
  115. package/dist/plugins/tanstack-router/index.js +2 -2
  116. package/dist/plugins/ts-node/index.js +6 -1
  117. package/dist/plugins/turbo/index.js +3 -3
  118. package/dist/plugins/typedoc/index.js +2 -2
  119. package/dist/plugins/typescript/content-mappers.d.ts +1 -1
  120. package/dist/plugins/typescript/index.js +6 -9
  121. package/dist/plugins/varlock/scan.js +1 -1
  122. package/dist/plugins/vite/helpers.d.ts +1 -0
  123. package/dist/plugins/vite/helpers.js +13 -3
  124. package/dist/plugins/vite/index.js +5 -2
  125. package/dist/plugins/vite-plugin-pwa/resolveFromAST.js +2 -2
  126. package/dist/plugins/vitest/index.js +14 -16
  127. package/dist/plugins/vue/index.js +1 -1
  128. package/dist/plugins/web-test-runner/index.d.ts +3 -0
  129. package/dist/plugins/web-test-runner/index.js +86 -0
  130. package/dist/plugins/webdriver-io/index.js +22 -17
  131. package/dist/plugins/webdriver-io/types.d.ts +3 -2
  132. package/dist/plugins/wrangler/index.js +3 -3
  133. package/dist/plugins/yarn/index.js +5 -5
  134. package/dist/plugins.js +2 -5
  135. package/dist/reporters/github-actions.js +3 -7
  136. package/dist/reporters/json.d.ts +1 -0
  137. package/dist/reporters/json.js +1 -0
  138. package/dist/reporters/util/configuration-hints.d.ts +3 -1
  139. package/dist/reporters/util/configuration-hints.js +40 -21
  140. package/dist/reporters/util/util.d.ts +1 -1
  141. package/dist/run-cli.d.ts +2 -0
  142. package/dist/run-cli.js +82 -0
  143. package/dist/run.js +12 -0
  144. package/dist/session/incremental.d.ts +1 -0
  145. package/dist/session/incremental.js +1 -0
  146. package/dist/session/index.d.ts +1 -0
  147. package/dist/session/index.js +1 -0
  148. package/dist/session/watch.js +2 -1
  149. package/dist/types/PluginNames.d.ts +2 -2
  150. package/dist/types/PluginNames.js +5 -0
  151. package/dist/types/args.d.ts +3 -1
  152. package/dist/types/config.d.ts +17 -10
  153. package/dist/types/configuration.d.ts +45 -0
  154. package/dist/types/configuration.js +1 -0
  155. package/dist/types/issues.d.ts +4 -0
  156. package/dist/types/module-graph.d.ts +1 -0
  157. package/dist/types/options.d.ts +1 -0
  158. package/dist/types/package-json.d.ts +3 -8
  159. package/dist/types/workspace.d.ts +1 -0
  160. package/dist/typescript/ast-nodes.js +47 -1
  161. package/dist/typescript/get-imports-and-exports.js +18 -6
  162. package/dist/typescript/resolve-module-names.js +7 -45
  163. package/dist/typescript/visitors/calls.d.ts +1 -1
  164. package/dist/typescript/visitors/calls.js +14 -10
  165. package/dist/typescript/visitors/exports.js +16 -26
  166. package/dist/typescript/visitors/imports.d.ts +2 -1
  167. package/dist/typescript/visitors/imports.js +41 -24
  168. package/dist/typescript/visitors/members.d.ts +1 -1
  169. package/dist/typescript/visitors/members.js +38 -24
  170. package/dist/typescript/visitors/shadow.d.ts +2 -0
  171. package/dist/typescript/visitors/shadow.js +25 -0
  172. package/dist/typescript/visitors/walk.d.ts +20 -12
  173. package/dist/typescript/visitors/walk.js +78 -48
  174. package/dist/util/Performance.d.ts +25 -3
  175. package/dist/util/Performance.js +51 -27
  176. package/dist/util/cli-arguments.d.ts +2 -1
  177. package/dist/util/cli-arguments.js +4 -2
  178. package/dist/util/create-input-handler.js +138 -119
  179. package/dist/util/create-options.d.ts +4 -2071
  180. package/dist/util/create-options.js +6 -4
  181. package/dist/util/create-workspace-graph.js +2 -1
  182. package/dist/util/errors.d.ts +5 -2
  183. package/dist/util/errors.js +11 -18
  184. package/dist/util/fs.js +4 -2
  185. package/dist/util/get-included-issue-types.js +1 -0
  186. package/dist/util/input.d.ts +2 -2
  187. package/dist/util/input.js +2 -2
  188. package/dist/util/is-plugin-name.d.ts +2 -0
  189. package/dist/util/is-plugin-name.js +3 -0
  190. package/dist/util/jiti.d.ts +2 -1
  191. package/dist/util/jiti.js +10 -3
  192. package/dist/util/load-config.d.ts +1 -1
  193. package/dist/util/load-config.js +3 -2
  194. package/dist/util/loader.js +3 -2
  195. package/dist/util/map-workspaces.js +23 -1
  196. package/dist/util/module-graph.js +1 -0
  197. package/dist/util/modules.d.ts +2 -0
  198. package/dist/util/modules.js +78 -0
  199. package/dist/util/package-json.d.ts +12 -5
  200. package/dist/util/package-json.js +54 -26
  201. package/dist/util/parse-and-convert-gitignores.d.ts +2 -2
  202. package/dist/util/parse-and-convert-gitignores.js +5 -5
  203. package/dist/util/parse-configuration.d.ts +2 -0
  204. package/dist/util/parse-configuration.js +66 -0
  205. package/dist/util/plugin.js +8 -12
  206. package/dist/util/regex.js +10 -1
  207. package/dist/util/resolve.d.ts +1 -0
  208. package/dist/util/resolve.js +3 -2
  209. package/dist/util/scripts.d.ts +0 -5
  210. package/dist/util/scripts.js +1 -32
  211. package/dist/util/to-source-path.d.ts +4 -1
  212. package/dist/util/to-source-path.js +2 -2
  213. package/dist/util/unhandled-rejection.d.ts +8 -0
  214. package/dist/util/unhandled-rejection.js +29 -0
  215. package/dist/util/validation.d.ts +11 -0
  216. package/dist/util/validation.js +68 -0
  217. package/dist/util/watch.d.ts +3 -1
  218. package/dist/util/watch.js +5 -2
  219. package/dist/version.d.ts +1 -1
  220. package/dist/version.js +1 -1
  221. package/package.json +6 -7
  222. package/schema.json +34 -0
  223. package/dist/schema/configuration.d.ts +0 -3103
  224. package/dist/schema/configuration.js +0 -94
  225. package/dist/schema/plugins.d.ts +0 -1019
  226. package/dist/schema/plugins.js +0 -215
@@ -7,8 +7,9 @@ import { collectAugmentationRefs, getLineAndCol, getStringValue, isStringLiteral
7
7
  import { EMPTY_TAGS } from './jsdoc.js';
8
8
  import { handleCallExpression, handleNewExpression, trackCustomElementRegistry } from './calls.js';
9
9
  import { handleExportAssignment, handleExportDefault, handleExportNamed, handleExpressionStatement, } from './exports.js';
10
- import { getPromiseAllElements, handleImportExpression, handleImportThen, handleVariableDeclarator, } from './imports.js';
10
+ import { getPromiseAllElements, handleImportExpression, handleImportThen, handleVariableDeclarator, markDynamicNamespaceOpaque, } from './imports.js';
11
11
  import { handleJSXMemberExpression, handleMemberExpression } from './members.js';
12
+ import { isShadowedIn } from './shadow.js';
12
13
  let state;
13
14
  const _getFix = (start, end, flags) => state.options.isFixExports ? [start, end, flags ?? FIX_FLAGS.NONE] : undefined;
14
15
  const _getTypeFix = (start, end) => state.options.isFixTypes ? [start, end, FIX_FLAGS.NONE] : undefined;
@@ -197,20 +198,7 @@ const _addRefInExport = (name, exportName) => {
197
198
  state.referencedInExport.set(name, new Set([exportName]));
198
199
  };
199
200
  const _isInNamespace = (node) => state.nsRanges.length > 0 && state.nsRanges.some(([start, end]) => node.start >= start && node.end <= end);
200
- export const isShadowed = (name, pos) => {
201
- if (state.shadowScopes.size === 0)
202
- return false;
203
- const ranges = state.shadowScopes.get(name);
204
- if (!ranges)
205
- return false;
206
- if (state.localImportMap.get(name)?.isDynamicImport)
207
- return false;
208
- for (const range of ranges) {
209
- if (pos >= range[0] && pos <= range[1])
210
- return true;
211
- }
212
- return false;
213
- };
201
+ export const isShadowed = (name, pos) => isShadowedIn(state, name, pos);
214
202
  const _addLocalRef = (name, pos) => {
215
203
  if (isShadowed(name, pos))
216
204
  return;
@@ -261,17 +249,46 @@ const _addParamShadows = (params, body) => {
261
249
  for (const param of items)
262
250
  _collectBindingNames(param, range);
263
251
  };
252
+ const _restoreScopedImports = (end) => {
253
+ const stack = state.scopedImports;
254
+ while (stack.length > 0 && stack[stack.length - 1].end <= end) {
255
+ const { name, prev } = stack.pop();
256
+ if (prev)
257
+ state.localImportMap.set(name, prev);
258
+ else
259
+ state.localImportMap.delete(name);
260
+ }
261
+ };
262
+ const _enterScope = (node) => {
263
+ state.scopeStarts[state.scopeDepth] = node.start;
264
+ state.scopeEnds[state.scopeDepth] = node.end;
265
+ state.scopeDepth++;
266
+ };
267
+ const _exitScope = (node) => {
268
+ state.scopeDepth--;
269
+ _restoreScopedImports(node.end);
270
+ };
264
271
  const coreVisitorObject = {
265
272
  BlockStatement(node) {
266
- state.scopeStarts[state.scopeDepth] = node.start;
267
- state.scopeEnds[state.scopeDepth] = node.end;
268
- state.scopeDepth++;
273
+ _enterScope(node);
269
274
  },
270
275
  'BlockStatement:exit'(node) {
271
- state.scopeDepth--;
276
+ _exitScope(node);
272
277
  if (state.caughtTryBlockEnds[state.caughtTryBlockEnds.length - 1] === node.end)
273
278
  state.caughtTryBlockEnds.pop();
274
279
  },
280
+ ForStatement(node) {
281
+ _enterScope(node);
282
+ },
283
+ 'ForStatement:exit'(node) {
284
+ _exitScope(node);
285
+ },
286
+ SwitchStatement(node) {
287
+ _enterScope(node);
288
+ },
289
+ 'SwitchStatement:exit'(node) {
290
+ _exitScope(node);
291
+ },
275
292
  TSModuleDeclaration(node) {
276
293
  state.nsRanges.push([node.start, node.end]);
277
294
  if (node.kind === 'global' || !isStringLiteral(node.id))
@@ -303,6 +320,8 @@ const coreVisitorObject = {
303
320
  if (node.id?.name) {
304
321
  state.localDeclarationTypes.set(node.id.name, SYMBOL_TYPE.CLASS);
305
322
  state.localDeclarations.set(node.id.name, node);
323
+ if (state.scopeDepth > 0)
324
+ _addShadow(node.id.name);
306
325
  }
307
326
  },
308
327
  'ClassDeclaration:exit'() {
@@ -314,11 +333,13 @@ const coreVisitorObject = {
314
333
  'ClassExpression:exit'() {
315
334
  state.classNameStack.pop();
316
335
  },
317
- StaticBlock() {
336
+ StaticBlock(node) {
318
337
  state.staticBlockDepth++;
338
+ _enterScope(node);
319
339
  },
320
- 'StaticBlock:exit'() {
340
+ 'StaticBlock:exit'(node) {
321
341
  state.staticBlockDepth--;
342
+ _exitScope(node);
322
343
  },
323
344
  FunctionDeclaration(node) {
324
345
  state.functionCaughtTryBlockEnds.push(state.caughtTryBlockEnds);
@@ -347,8 +368,9 @@ const coreVisitorObject = {
347
368
  state.caughtTryBlockEnds = [];
348
369
  _addParamShadows(node.params, node.body);
349
370
  },
350
- 'ArrowFunctionExpression:exit'() {
371
+ 'ArrowFunctionExpression:exit'(node) {
351
372
  state.caughtTryBlockEnds = state.functionCaughtTryBlockEnds.pop();
373
+ _restoreScopedImports(node.end);
352
374
  },
353
375
  CatchClause(node) {
354
376
  if (node.param?.type === 'Identifier' && node.body) {
@@ -357,12 +379,14 @@ const coreVisitorObject = {
357
379
  },
358
380
  VariableDeclaration(node) {
359
381
  state.currentVarDeclStart = node.start;
382
+ for (const decl of node.declarations)
383
+ handleVariableDeclarator(decl, node.kind === 'var', state);
360
384
  if (state.scopeDepth > 0) {
385
+ const i = state.scopeDepth - 1;
361
386
  for (const decl of node.declarations) {
362
- if (decl.id.type === 'Identifier') {
387
+ if (decl.id.type === 'Identifier')
363
388
  state.localDeclarationTypes.set(decl.id.name, SYMBOL_TYPE.VARIABLE);
364
- _addShadow(decl.id.name);
365
- }
389
+ _collectBindingNames(decl.id, [state.scopeStarts[i], state.scopeEnds[i]]);
366
390
  }
367
391
  }
368
392
  else {
@@ -391,7 +415,6 @@ const coreVisitorObject = {
391
415
  handleExpressionStatement(node, state);
392
416
  },
393
417
  VariableDeclarator(node) {
394
- handleVariableDeclarator(node, state);
395
418
  trackCustomElementRegistry(node, state);
396
419
  },
397
420
  ImportExpression(node) {
@@ -419,8 +442,8 @@ const coreVisitorObject = {
419
442
  }
420
443
  },
421
444
  CallExpression(node) {
422
- handleImportThen(node, state);
423
445
  handleCallExpression(node, state);
446
+ handleImportThen(node, state);
424
447
  },
425
448
  NewExpression(node) {
426
449
  handleNewExpression(node, state);
@@ -432,10 +455,7 @@ const coreVisitorObject = {
432
455
  handleJSXMemberExpression(node, state);
433
456
  },
434
457
  ForInStatement(node) {
435
- if (node.left.type === 'VariableDeclaration' && node.body) {
436
- for (const decl of node.left.declarations)
437
- _collectBindingNames(decl.id, [node.body.start, node.body.end]);
438
- }
458
+ _enterScope(node);
439
459
  if (node.right.type === 'Identifier' && !isShadowed(node.right.name, node.right.start)) {
440
460
  const _import = state.localImportMap.get(node.right.name);
441
461
  if (_import?.isNamespace) {
@@ -445,11 +465,11 @@ const coreVisitorObject = {
445
465
  }
446
466
  }
447
467
  },
468
+ 'ForInStatement:exit'(node) {
469
+ _exitScope(node);
470
+ },
448
471
  ForOfStatement(node) {
449
- if (node.left.type === 'VariableDeclaration' && node.body) {
450
- for (const decl of node.left.declarations)
451
- _collectBindingNames(decl.id, [node.body.start, node.body.end]);
452
- }
472
+ _enterScope(node);
453
473
  if (node.right.type === 'Identifier' && !isShadowed(node.right.name, node.right.start)) {
454
474
  const _import = state.localImportMap.get(node.right.name);
455
475
  if (_import?.isNamespace) {
@@ -459,6 +479,9 @@ const coreVisitorObject = {
459
479
  }
460
480
  }
461
481
  },
482
+ 'ForOfStatement:exit'(node) {
483
+ _exitScope(node);
484
+ },
462
485
  ArrayExpression(node) {
463
486
  for (const element of node.elements ?? []) {
464
487
  if (element?.type !== 'Identifier')
@@ -471,6 +494,18 @@ const coreVisitorObject = {
471
494
  }
472
495
  }
473
496
  },
497
+ ReturnStatement(node) {
498
+ if (node.argument?.type === 'Identifier')
499
+ markDynamicNamespaceOpaque(node.argument.name, node.argument.start, state);
500
+ },
501
+ AssignmentExpression(node) {
502
+ if (node.right?.type === 'Identifier')
503
+ markDynamicNamespaceOpaque(node.right.name, node.right.start, state);
504
+ },
505
+ SpreadElement(node) {
506
+ if (node.argument?.type === 'Identifier')
507
+ markDynamicNamespaceOpaque(node.argument.name, node.argument.start, state);
508
+ },
474
509
  TSQualifiedName(node) {
475
510
  let left = node;
476
511
  const parts = [];
@@ -865,6 +900,7 @@ function walkAST(program, sourceText, filePath, hasModuleSyntax, ctx) {
865
900
  scopeStarts: [],
866
901
  scopeEnds: [],
867
902
  shadowScopes: new Map(),
903
+ scopedImports: [],
868
904
  localDeclarations: new Map(),
869
905
  localInterfaces: new Map(),
870
906
  pendingCallRefs: [],
@@ -929,26 +965,20 @@ function walkAST(program, sourceText, filePath, hasModuleSyntax, ctx) {
929
965
  for (const [aliasName, aliasSet] of state.importAliases) {
930
966
  if (!state.accessedAliases.has(aliasName)) {
931
967
  for (const alias of aliasSet) {
932
- const _import = state.localImportMap.get(alias.id);
933
- if (_import?.isNamespace) {
934
- const internalImport = state.internal.get(_import.filePath);
935
- if (internalImport) {
968
+ if (alias.import.isNamespace) {
969
+ const internalImport = state.internal.get(alias.import.filePath);
970
+ if (internalImport)
936
971
  addValue(internalImport.import, OPAQUE, filePath);
937
- }
938
972
  }
939
973
  }
940
974
  }
941
975
  }
942
976
  for (const [containerName, propMap] of state.nsContainers) {
943
- for (const [propKey, nsName] of propMap) {
977
+ for (const [propKey, ref] of propMap) {
944
978
  if (!state.accessedNsContainers.has(`${containerName}.${propKey}`)) {
945
- const _import = state.localImportMap.get(nsName);
946
- if (_import) {
947
- const internalImport = state.internal.get(_import.filePath);
948
- if (internalImport) {
949
- addValue(internalImport.import, OPAQUE, filePath);
950
- }
951
- }
979
+ const internalImport = state.internal.get(ref.import.filePath);
980
+ if (internalImport)
981
+ addValue(internalImport.import, OPAQUE, filePath);
952
982
  }
953
983
  }
954
984
  }
@@ -4,11 +4,11 @@ declare class Performance {
4
4
  readonly isTimerifyFunctions: boolean;
5
5
  readonly isMemoryUsageEnabled: boolean;
6
6
  readonly isDurationEnabled: boolean;
7
- private readonly startTime;
7
+ private startTime;
8
+ private endTime;
8
9
  private readonly memId;
9
10
  private readonly histograms;
10
11
  private readonly memEntries;
11
- private readonly memObserver?;
12
12
  constructor({ isTimerifyFunctions, isMemoryUsageEnabled, isDurationEnabled }: {
13
13
  isDurationEnabled?: boolean | undefined;
14
14
  isMemoryUsageEnabled?: boolean | undefined;
@@ -16,12 +16,34 @@ declare class Performance {
16
16
  });
17
17
  registerHistogram(name: string): RecordableHistogram;
18
18
  addMemoryMark(label: string): void;
19
+ getSnapshot(): {
20
+ durationMs: number;
21
+ processMs: number;
22
+ functions: {
23
+ name: string;
24
+ count: number;
25
+ minMs: number;
26
+ maxMs: number;
27
+ meanMs: number;
28
+ medianMs: number;
29
+ p95Ms: number;
30
+ sumMs: number;
31
+ }[];
32
+ memory: {
33
+ label: string;
34
+ heapUsed: number;
35
+ heapTotal: number;
36
+ rss: number;
37
+ }[];
38
+ };
19
39
  getTimerifiedFunctionsTable(): string;
20
40
  getMemoryUsageTable(): string;
21
41
  getCurrentDurationInMs(): number;
42
+ getProcessDurationInMs(): number;
43
+ startRun(): void;
22
44
  getMemHeapUsage(): number;
23
45
  getCurrentMemUsageInMb(): any;
24
- finalize(): Promise<void>;
46
+ finalize(): void;
25
47
  reset(): void;
26
48
  }
27
49
  export declare const perfObserver: Performance;
@@ -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 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 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)
@@ -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' },