knip 5.63.0 → 5.63.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 (103) hide show
  1. package/dist/CacheConsultant.d.ts +3 -9
  2. package/dist/CacheConsultant.js +5 -5
  3. package/dist/ConfigurationChief.d.ts +585 -38
  4. package/dist/ConfigurationChief.js +33 -122
  5. package/dist/ConsoleStreamer.d.ts +2 -3
  6. package/dist/ConsoleStreamer.js +2 -2
  7. package/dist/DependencyDeputy.d.ts +2 -6
  8. package/dist/IssueCollector.d.ts +4 -12
  9. package/dist/IssueCollector.js +9 -9
  10. package/dist/IssueFixer.d.ts +3 -14
  11. package/dist/IssueFixer.js +11 -21
  12. package/dist/PrincipalFactory.d.ts +3 -2
  13. package/dist/PrincipalFactory.js +11 -11
  14. package/dist/ProjectPrincipal.d.ts +4 -3
  15. package/dist/ProjectPrincipal.js +8 -8
  16. package/dist/WorkspaceWorker.d.ts +4 -9
  17. package/dist/WorkspaceWorker.js +8 -12
  18. package/dist/binaries/bash-parser.js +1 -1
  19. package/dist/binaries/fallback.js +1 -1
  20. package/dist/binaries/package-manager/pnpm.js +6 -1
  21. package/dist/cli.js +43 -61
  22. package/dist/compilers/index.d.ts +14 -4
  23. package/dist/graph/analyze.d.ts +3 -8
  24. package/dist/graph/analyze.js +24 -27
  25. package/dist/graph/build.d.ts +3 -17
  26. package/dist/graph/build.js +42 -39
  27. package/dist/index.d.ts +3 -7
  28. package/dist/index.js +22 -62
  29. package/dist/plugins/index.d.ts +5 -0
  30. package/dist/plugins/index.js +2 -0
  31. package/dist/plugins/nuxt/index.js +4 -3
  32. package/dist/plugins/nuxt/types.d.ts +3 -2
  33. package/dist/plugins/pnpm/index.d.ts +7 -0
  34. package/dist/plugins/pnpm/index.js +8 -0
  35. package/dist/plugins/rsbuild/index.js +23 -6
  36. package/dist/plugins/rsbuild/types.d.ts +3 -0
  37. package/dist/plugins.js +3 -2
  38. package/dist/reporters/codeclimate.d.ts +1 -1
  39. package/dist/reporters/codeclimate.js +10 -10
  40. package/dist/reporters/codeowners.d.ts +1 -1
  41. package/dist/reporters/codeowners.js +5 -5
  42. package/dist/reporters/compact.d.ts +1 -1
  43. package/dist/reporters/compact.js +7 -7
  44. package/dist/reporters/disclosure.d.ts +1 -1
  45. package/dist/reporters/disclosure.js +2 -2
  46. package/dist/reporters/index.d.ts +6 -6
  47. package/dist/reporters/json.d.ts +1 -1
  48. package/dist/reporters/json.js +3 -3
  49. package/dist/reporters/markdown.d.ts +1 -1
  50. package/dist/reporters/markdown.js +4 -4
  51. package/dist/reporters/symbols.js +1 -1
  52. package/dist/reporters/util/configuration-hints.d.ts +1 -1
  53. package/dist/reporters/util/configuration-hints.js +6 -7
  54. package/dist/reporters/util/util.d.ts +2 -2
  55. package/dist/reporters/util/util.js +4 -4
  56. package/dist/reporters/watch.d.ts +3 -4
  57. package/dist/reporters/watch.js +5 -5
  58. package/dist/schema/configuration.d.ts +64 -8
  59. package/dist/schema/plugins.d.ts +23 -0
  60. package/dist/schema/plugins.js +1 -0
  61. package/dist/types/PluginNames.d.ts +2 -2
  62. package/dist/types/PluginNames.js +1 -0
  63. package/dist/types/config.d.ts +4 -10
  64. package/dist/types/issues.d.ts +1 -2
  65. package/dist/types/{cli.d.ts → options.d.ts} +2 -2
  66. package/dist/types/package-json.d.ts +1 -0
  67. package/dist/types/project.d.ts +1 -7
  68. package/dist/typescript/get-imports-and-exports.d.ts +2 -2
  69. package/dist/typescript/get-imports-and-exports.js +9 -10
  70. package/dist/util/Performance.js +16 -6
  71. package/dist/util/cli-arguments.d.ts +1 -2
  72. package/dist/util/cli-arguments.js +3 -13
  73. package/dist/util/create-options.d.ts +1219 -0
  74. package/dist/util/create-options.js +112 -0
  75. package/dist/util/debug.js +3 -4
  76. package/dist/util/errors.d.ts +1 -1
  77. package/dist/util/file-entry-cache.js +3 -3
  78. package/dist/util/get-included-issue-types.d.ts +9 -13
  79. package/dist/util/get-included-issue-types.js +10 -16
  80. package/dist/util/get-referenced-inputs.js +1 -1
  81. package/dist/util/input.d.ts +1 -1
  82. package/dist/util/input.js +1 -1
  83. package/dist/util/is-identifier-referenced.d.ts +1 -1
  84. package/dist/util/is-identifier-referenced.js +2 -2
  85. package/dist/util/load-config.d.ts +1 -0
  86. package/dist/util/load-config.js +24 -0
  87. package/dist/util/path.d.ts +4 -4
  88. package/dist/util/path.js +5 -7
  89. package/dist/util/require.js +1 -2
  90. package/dist/util/tag.d.ts +1 -1
  91. package/dist/util/to-source-path.d.ts +1 -1
  92. package/dist/util/to-source-path.js +5 -5
  93. package/dist/util/trace.d.ts +6 -6
  94. package/dist/util/trace.js +18 -22
  95. package/dist/util/watch.d.ts +2 -5
  96. package/dist/util/watch.js +3 -3
  97. package/dist/version.d.ts +1 -1
  98. package/dist/version.js +1 -1
  99. package/package.json +2 -2
  100. package/schema.json +4 -0
  101. package/dist/util/unwrap-function.d.ts +0 -1
  102. package/dist/util/unwrap-function.js +0 -13
  103. /package/dist/types/{cli.js → options.js} +0 -0
@@ -1,4 +1,4 @@
1
- export interface CommandLineOptions {
1
+ export interface Options {
2
2
  cacheLocation: string;
3
3
  cwd: string;
4
4
  excludedIssueTypes: string[];
@@ -20,7 +20,7 @@ export interface CommandLineOptions {
20
20
  isShowProgress: boolean;
21
21
  isStrict: boolean;
22
22
  isWatch: boolean;
23
- tags: Tags;
23
+ tags: string[];
24
24
  tsConfigFile: string | undefined;
25
25
  workspace: string | undefined;
26
26
  }
@@ -45,6 +45,7 @@ export type PackageJson = {
45
45
  browser?: string;
46
46
  types?: string;
47
47
  typings?: string;
48
+ packageManager?: string;
48
49
  } & Plugins;
49
50
  export type WorkspacePackage = {
50
51
  dir: string;
@@ -3,16 +3,10 @@ import type { AsyncCompilers, SyncCompilers } from '../compilers/types.js';
3
3
  import type { ToSourceFilePath } from '../util/to-source-path.js';
4
4
  export type Paths = ts.CompilerOptions['paths'];
5
5
  export type PrincipalOptions = {
6
- cwd: string;
6
+ dir: string;
7
7
  isFile: boolean;
8
8
  compilerOptions: ts.CompilerOptions;
9
9
  compilers: [SyncCompilers, AsyncCompilers];
10
10
  pkgName: string;
11
- isIsolateWorkspaces: boolean;
12
- isSkipLibs: boolean;
13
- isWatch: boolean;
14
11
  toSourceFilePath: ToSourceFilePath;
15
- isCache: boolean;
16
- cacheLocation: string;
17
- isProduction: boolean;
18
12
  };
@@ -1,5 +1,5 @@
1
1
  import ts from 'typescript';
2
- import type { GetImportsAndExportsOptions } from '../types/config.js';
2
+ import type { GetImportsAndExportsOptions, IgnoreExportsUsedInFile } from '../types/config.js';
3
3
  import type { FileNode } from '../types/module-graph.js';
4
4
  import type { BoundSourceFile } from './SourceFile.js';
5
- export declare const _getImportsAndExports: (sourceFile: BoundSourceFile, resolveModule: (specifier: string) => ts.ResolvedModuleFull | undefined, typeChecker: ts.TypeChecker, options: GetImportsAndExportsOptions) => FileNode;
5
+ export declare const _getImportsAndExports: (sourceFile: BoundSourceFile, resolveModule: (specifier: string) => ts.ResolvedModuleFull | undefined, typeChecker: ts.TypeChecker, options: GetImportsAndExportsOptions, ignoreExportsUsedInFile: IgnoreExportsUsedInFile, skipExports: boolean) => FileNode;
@@ -33,8 +33,7 @@ const createMember = (node, member, pos) => {
33
33
  jsDocTags: getJSDocTags(member.node),
34
34
  };
35
35
  };
36
- const getImportsAndExports = (sourceFile, resolveModule, typeChecker, options) => {
37
- const { skipTypeOnly, tags, ignoreExportsUsedInFile } = options;
36
+ const getImportsAndExports = (sourceFile, resolveModule, typeChecker, options, ignoreExportsUsedInFile, skipExports) => {
38
37
  const internal = new Map();
39
38
  const external = new Set();
40
39
  const unresolved = new Set();
@@ -112,23 +111,23 @@ const getImportsAndExports = (sourceFile, resolveModule, typeChecker, options) =
112
111
  importedInternalSymbols.set(symbol, filePath);
113
112
  }
114
113
  };
115
- const addImport = (options, node) => {
116
- const { specifier, isTypeOnly, pos, identifier = ANONYMOUS, isReExport = false } = options;
114
+ const addImport = (opts, node) => {
115
+ const { specifier, isTypeOnly, pos, identifier = ANONYMOUS, isReExport = false } = opts;
117
116
  if (isBuiltin(specifier))
118
117
  return;
119
118
  const module = resolveModule(specifier);
120
119
  if (module) {
121
120
  const filePath = module.resolvedFileName;
122
121
  if (filePath) {
123
- if (options.resolve && !isInNodeModules(filePath)) {
122
+ if (opts.resolve && !isInNodeModules(filePath)) {
124
123
  resolved.add(filePath);
125
124
  return;
126
125
  }
127
126
  if (!module.isExternalLibraryImport || !isInNodeModules(filePath)) {
128
- addInternalImport({ ...options, identifier, filePath, isReExport });
127
+ addInternalImport({ ...opts, identifier, filePath, isReExport });
129
128
  }
130
129
  if (module.isExternalLibraryImport) {
131
- if (skipTypeOnly && isTypeOnly)
130
+ if (options.skipTypeOnly && isTypeOnly)
132
131
  return;
133
132
  const sanitizedSpecifier = sanitizeSpecifier(isInNodeModules(specifier) || isInNodeModules(filePath) ? getPackageNameFromFilePath(specifier) : specifier);
134
133
  if (!isStartsLikePackageName(sanitizedSpecifier)) {
@@ -139,9 +138,9 @@ const getImportsAndExports = (sourceFile, resolveModule, typeChecker, options) =
139
138
  }
140
139
  }
141
140
  else {
142
- if (skipTypeOnly && isTypeOnly)
141
+ if (options.skipTypeOnly && isTypeOnly)
143
142
  return;
144
- if (shouldIgnore(getJSDocTags(node), tags))
143
+ if (shouldIgnore(getJSDocTags(node), options.tags))
145
144
  return;
146
145
  if (specifier.startsWith(PROTOCOL_VIRTUAL))
147
146
  return;
@@ -155,7 +154,7 @@ const getImportsAndExports = (sourceFile, resolveModule, typeChecker, options) =
155
154
  }
156
155
  };
157
156
  const addExport = ({ node, symbol, identifier, type, pos, members = [], fix }) => {
158
- if (options.skipExports)
157
+ if (skipExports)
159
158
  return;
160
159
  if (symbol) {
161
160
  const importedSymbolFilePath = importedInternalSymbols.get(symbol);
@@ -1,12 +1,22 @@
1
1
  import os from 'node:os';
2
2
  import { PerformanceObserver, performance } from 'node:perf_hooks';
3
3
  import { memoryUsage } from 'node:process';
4
- import parsedArgValues from './cli-arguments.js';
4
+ import { parseArgs } from 'node:util';
5
5
  import { getStats } from './math.js';
6
6
  import { Table } from './table.js';
7
- const { performance: enableTimerify = false, 'performance-fn': timerifyOnlyFnName, memory: enableMemoryUsage = false, 'memory-realtime': memoryRealtime = false, } = parsedArgValues;
8
- const isTimerifyFunctions = enableTimerify || !!timerifyOnlyFnName;
9
- const isMemoryUsageEnabled = enableMemoryUsage || memoryRealtime;
7
+ const { values } = parseArgs({
8
+ strict: false,
9
+ options: {
10
+ performance: { type: 'boolean' },
11
+ 'performance-fn': { type: 'string' },
12
+ memory: { type: 'boolean' },
13
+ 'memory-realtime': { type: 'boolean' },
14
+ },
15
+ });
16
+ const timerifyOnlyFnName = values['performance-fn'];
17
+ const isMemoryRealtime = !!values['memory-realtime'];
18
+ const isTimerifyFunctions = !!values.performance || !!timerifyOnlyFnName;
19
+ const isMemoryUsageEnabled = !!values.memory || isMemoryRealtime;
10
20
  export const timerify = (fn, name = fn.name) => {
11
21
  if (!isTimerifyFunctions)
12
22
  return fn;
@@ -57,7 +67,7 @@ class Performance {
57
67
  }
58
68
  });
59
69
  this.memObserver.observe({ type: 'mark' });
60
- if (memoryRealtime)
70
+ if (isMemoryRealtime)
61
71
  logHead();
62
72
  this.addMemoryMark(0);
63
73
  }
@@ -108,7 +118,7 @@ class Performance {
108
118
  const id = `${this.memId}:${index}`;
109
119
  const detail = getMemInfo();
110
120
  performance.mark(id, { detail });
111
- if (memoryRealtime && detail)
121
+ if (isMemoryRealtime && detail)
112
122
  log(detail);
113
123
  }
114
124
  getMemoryUsageTable() {
@@ -1,5 +1,5 @@
1
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 -c, --config [file] Configuration file path (default: [.]knip.json[c], knip.(js|ts), knip.config.(js|ts) or package.json#knip)\n -t, --tsConfig [file] TypeScript configuration path (default: tsconfig.json)\n --production Analyze only production source files (e.g. no test files, devDependencies)\n --strict Consider only direct dependencies of workspace (not devDependencies, not other workspaces)\n -W, --workspace [dir] Analyze a single workspace (default: analyze all configured workspaces)\n --directory [dir] Run process from a different directory (default: cwd)\n --cache Enable caching\n --cache-location Change cache location (default: node_modules/.cache/knip)\n --watch Watch mode\n --no-gitignore Don't use .gitignore\n --include Report only provided issue type(s), can be comma-separated or repeated (1)\n --exclude Exclude provided issue type(s) from report, can be comma-separated or repeated (1)\n --dependencies Shortcut for --include dependencies,unlisted,binaries,unresolved\n --exports Shortcut for --include exports,nsExports,classMembers,types,nsTypes,enumMembers,duplicates\n --files Shortcut for --include files\n --fix Fix issues\n --fix-type Fix only issues of type, can be comma-separated or repeated (2)\n --format Format modified files after --fix using the local formatter\n --allow-remove-files Allow Knip to remove files (with --fix)\n --include-libs Include type definitions from external dependencies (default: false)\n --include-entry-exports Include entry files when reporting unused exports\n --isolate-workspaces Isolate workspaces into separate programs\n -n, --no-progress Don't show dynamic progress updates (automatically enabled in CI environments)\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: symbols, compact, codeowners, json, codeclimate, markdown, disclosure, can be repeated (default: symbols)\n --reporter-options Pass extra options to the reporter (as JSON string, see example)\n --tags Include or exclude tagged exports\n --no-config-hints Suppress configuration hints\n --treat-config-hints-as-errors Exit with non-zero code (1) if there are any configuration hints\n --no-exit-code Always exit with code zero (0)\n --max-issues Maximum number of issues before non-zero exit code (default: 0)\n -d, --debug Show debug output\n --trace Show trace output\n --trace-export [name] Show trace output for named export(s)\n --trace-file [file] Show trace output for exports in file\n --performance Measure count and running time of key functions and display stats table\n --performance-fn [name] Measure only function [name]\n --memory Measure memory usage and display data table\n --memory-realtime Log memory usage in realtime\n -h, --help Print this help text\n -V, --version Print version\n\n(1) Issue types: files, dependencies, unlisted, unresolved, exports, nsExports, classMembers, types, nsTypes, enumMembers, duplicates\n(2) Fixable issue types: dependencies, exports, types\n\nExamples:\n\n$ knip\n$ knip --production\n$ knip --workspace packages/client --include files,dependencies\n$ knip -c ./config/knip.json --reporter compact\n$ knip --reporter codeowners --reporter-options '{\"path\":\".github/CODEOWNERS\"}'\n$ knip --tags=-lintignore\n\nWebsite: https://knip.dev";
2
- declare const _default: {
2
+ export default function (): {
3
3
  cache?: boolean | undefined;
4
4
  'cache-location'?: string | undefined;
5
5
  config?: string | undefined;
@@ -45,4 +45,3 @@ declare const _default: {
45
45
  watch?: boolean | undefined;
46
46
  workspace?: string | undefined;
47
47
  };
48
- export default _default;
@@ -60,9 +60,8 @@ $ knip --reporter codeowners --reporter-options '{"path":".github/CODEOWNERS"}'
60
60
  $ knip --tags=-lintignore
61
61
 
62
62
  Website: https://knip.dev`;
63
- let parsedArgs;
64
- try {
65
- parsedArgs = parseArgs({
63
+ export default function () {
64
+ return parseArgs({
66
65
  options: {
67
66
  cache: { type: 'boolean' },
68
67
  'cache-location': { type: 'string' },
@@ -109,14 +108,5 @@ try {
109
108
  watch: { type: 'boolean' },
110
109
  workspace: { type: 'string', short: 'W' },
111
110
  },
112
- });
111
+ }).values;
113
112
  }
114
- catch (error) {
115
- if (error instanceof Error) {
116
- console.error(error.message);
117
- console.log(`\n${helpText}`);
118
- process.exit(1);
119
- }
120
- throw error;
121
- }
122
- export default parsedArgs.values;