knip 5.1.0 → 5.1.2

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.
@@ -213,6 +213,8 @@ export class ConfigurationChief {
213
213
  const manifest = _require(join(dir, 'package.json'));
214
214
  if (!manifest)
215
215
  throw new LoaderError(`Unable to load package.json for ${dir}`);
216
+ if (dir === this.cwd && !manifest.name)
217
+ manifest.name = ROOT_WORKSPACE_NAME;
216
218
  return { dir, manifest };
217
219
  });
218
220
  }
@@ -24,9 +24,9 @@ export declare const ConfigurationValidator: z.ZodObject<{
24
24
  ignore: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodArray<z.ZodString, "many">]>>;
25
25
  ignoreExportsUsedInFile: z.ZodOptional<z.ZodUnion<[z.ZodBoolean, z.ZodRecord<z.ZodUnion<[z.ZodLiteral<"class">, z.ZodLiteral<"enum">, z.ZodLiteral<"function">, z.ZodLiteral<"interface">, z.ZodLiteral<"member">, z.ZodLiteral<"type">]>, z.ZodBoolean>]>>;
26
26
  includeEntryExports: z.ZodOptional<z.ZodBoolean>;
27
- compilers: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodFunction<z.ZodTuple<[z.ZodString], z.ZodUnknown>, z.ZodString>, z.ZodFunction<z.ZodTuple<[z.ZodString], z.ZodUnknown>, z.ZodPromise<z.ZodString>>]>>>;
28
- syncCompilers: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodFunction<z.ZodTuple<[z.ZodString], z.ZodUnknown>, z.ZodString>>>;
29
- asyncCompilers: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodFunction<z.ZodTuple<[z.ZodString], z.ZodUnknown>, z.ZodPromise<z.ZodString>>>>;
27
+ compilers: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodFunction<z.ZodTuple<[z.ZodString, z.ZodString], z.ZodUnknown>, z.ZodString>, z.ZodFunction<z.ZodTuple<[z.ZodString, z.ZodString], z.ZodUnknown>, z.ZodPromise<z.ZodString>>]>>>;
28
+ syncCompilers: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodFunction<z.ZodTuple<[z.ZodString, z.ZodString], z.ZodUnknown>, z.ZodString>>>;
29
+ asyncCompilers: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodFunction<z.ZodTuple<[z.ZodString, z.ZodString], z.ZodUnknown>, z.ZodPromise<z.ZodString>>>>;
30
30
  include: z.ZodOptional<z.ZodArray<z.ZodUnion<[z.ZodLiteral<"files">, z.ZodLiteral<"dependencies">, z.ZodLiteral<"devDependencies">, z.ZodLiteral<"unlisted">, z.ZodLiteral<"binaries">, z.ZodLiteral<"unresolved">, z.ZodLiteral<"exports">, z.ZodLiteral<"types">, z.ZodLiteral<"nsExports">, z.ZodLiteral<"nsTypes">, z.ZodLiteral<"duplicates">, z.ZodLiteral<"enumMembers">, z.ZodLiteral<"classMembers">]>, "many">>;
31
31
  workspaces: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
32
32
  ignoreBinaries: z.ZodOptional<z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodType<RegExp, z.ZodTypeDef, RegExp>]>, "many">>;
@@ -2045,9 +2045,9 @@ export declare const ConfigurationValidator: z.ZodObject<{
2045
2045
  ignore?: string | string[] | undefined;
2046
2046
  ignoreExportsUsedInFile?: boolean | Partial<Record<"function" | "type" | "interface" | "enum" | "class" | "member", boolean>> | undefined;
2047
2047
  includeEntryExports?: boolean | undefined;
2048
- compilers?: Record<string, ((args_0: string, ...args_1: unknown[]) => string) | ((args_0: string, ...args_1: unknown[]) => Promise<string>)> | undefined;
2049
- syncCompilers?: Record<string, (args_0: string, ...args_1: unknown[]) => string> | undefined;
2050
- asyncCompilers?: Record<string, (args_0: string, ...args_1: unknown[]) => Promise<string>> | undefined;
2048
+ compilers?: Record<string, ((args_0: string, args_1: string, ...args_2: unknown[]) => string) | ((args_0: string, args_1: string, ...args_2: unknown[]) => Promise<string>)> | undefined;
2049
+ syncCompilers?: Record<string, (args_0: string, args_1: string, ...args_2: unknown[]) => string> | undefined;
2050
+ asyncCompilers?: Record<string, (args_0: string, args_1: string, ...args_2: unknown[]) => Promise<string>> | undefined;
2051
2051
  include?: ("files" | "dependencies" | "devDependencies" | "unlisted" | "binaries" | "unresolved" | "exports" | "types" | "nsExports" | "nsTypes" | "duplicates" | "enumMembers" | "classMembers")[] | undefined;
2052
2052
  workspaces?: Record<string, {
2053
2053
  ignoreBinaries?: (string | RegExp)[] | undefined;
@@ -2620,9 +2620,9 @@ export declare const ConfigurationValidator: z.ZodObject<{
2620
2620
  ignore?: string | string[] | undefined;
2621
2621
  ignoreExportsUsedInFile?: boolean | Partial<Record<"function" | "type" | "interface" | "enum" | "class" | "member", boolean>> | undefined;
2622
2622
  includeEntryExports?: boolean | undefined;
2623
- compilers?: Record<string, ((args_0: string, ...args_1: unknown[]) => string) | ((args_0: string, ...args_1: unknown[]) => Promise<string>)> | undefined;
2624
- syncCompilers?: Record<string, (args_0: string, ...args_1: unknown[]) => string> | undefined;
2625
- asyncCompilers?: Record<string, (args_0: string, ...args_1: unknown[]) => Promise<string>> | undefined;
2623
+ compilers?: Record<string, ((args_0: string, args_1: string, ...args_2: unknown[]) => string) | ((args_0: string, args_1: string, ...args_2: unknown[]) => Promise<string>)> | undefined;
2624
+ syncCompilers?: Record<string, (args_0: string, args_1: string, ...args_2: unknown[]) => string> | undefined;
2625
+ asyncCompilers?: Record<string, (args_0: string, args_1: string, ...args_2: unknown[]) => Promise<string>> | undefined;
2626
2626
  include?: ("files" | "dependencies" | "devDependencies" | "unlisted" | "binaries" | "unresolved" | "exports" | "types" | "nsExports" | "nsTypes" | "duplicates" | "enumMembers" | "classMembers")[] | undefined;
2627
2627
  workspaces?: Record<string, {
2628
2628
  ignoreBinaries?: (string | RegExp)[] | undefined;
@@ -1,8 +1,8 @@
1
1
  import { z } from 'zod';
2
2
  const globSchema = z.union([z.string(), z.array(z.string())]);
3
3
  const pathsSchema = z.record(z.string(), z.array(z.string()));
4
- const syncCompilerSchema = z.function().args(z.string()).returns(z.string());
5
- const asyncCompilerSchema = z.function().args(z.string()).returns(z.promise(z.string()));
4
+ const syncCompilerSchema = z.function().args(z.string(), z.string()).returns(z.string());
5
+ const asyncCompilerSchema = z.function().args(z.string(), z.string()).returns(z.promise(z.string()));
6
6
  const compilerSchema = z.union([syncCompilerSchema, asyncCompilerSchema]);
7
7
  const compilersSchema = z.record(z.string(), compilerSchema);
8
8
  const stringOrRegexSchema = z.array(z.union([z.string(), z.instanceof(RegExp)]));
@@ -17,6 +17,7 @@ export type PrincipalOptions = {
17
17
  pkgName: string;
18
18
  isGitIgnored: (path: string) => boolean;
19
19
  isIsolateWorkspaces: boolean;
20
+ isSkipLibs: boolean;
20
21
  };
21
22
  export declare class PrincipalFactory {
22
23
  principals: Principals;
@@ -24,6 +24,7 @@ export declare class ProjectPrincipal {
24
24
  extensions: Set<string>;
25
25
  syncCompilers: SyncCompilers;
26
26
  asyncCompilers: AsyncCompilers;
27
+ isSkipLibs: boolean;
27
28
  backend: {
28
29
  fileManager: SourceFileManager;
29
30
  compilerHost: ts.CompilerHost;
@@ -33,7 +34,7 @@ export declare class ProjectPrincipal {
33
34
  languageServiceHost: ts.LanguageServiceHost;
34
35
  };
35
36
  findReferences?: ts.LanguageService['findReferences'];
36
- constructor({ compilerOptions, cwd, compilers, isGitIgnored }: PrincipalOptions);
37
+ constructor({ compilerOptions, cwd, compilers, isGitIgnored, isSkipLibs }: PrincipalOptions);
37
38
  init(): void;
38
39
  addPaths(paths: ts.CompilerOptions['paths']): void;
39
40
  addCompilers(compilers: [SyncCompilers, AsyncCompilers]): void;
@@ -39,9 +39,10 @@ export class ProjectPrincipal {
39
39
  extensions;
40
40
  syncCompilers;
41
41
  asyncCompilers;
42
+ isSkipLibs;
42
43
  backend;
43
44
  findReferences;
44
- constructor({ compilerOptions, cwd, compilers, isGitIgnored }) {
45
+ constructor({ compilerOptions, cwd, compilers, isGitIgnored, isSkipLibs }) {
45
46
  this.cwd = cwd;
46
47
  this.isGitIgnored = isGitIgnored;
47
48
  this.compilerOptions = {
@@ -54,6 +55,7 @@ export class ProjectPrincipal {
54
55
  this.extensions = new Set([...DEFAULT_EXTENSIONS, ...getCompilerExtensions(compilers)]);
55
56
  this.syncCompilers = syncCompilers;
56
57
  this.asyncCompilers = asyncCompilers;
58
+ this.isSkipLibs = isSkipLibs;
57
59
  }
58
60
  init() {
59
61
  this.addCompilers([dummyCompilers, new Map()]);
@@ -62,6 +64,7 @@ export class ProjectPrincipal {
62
64
  compilerOptions: this.compilerOptions,
63
65
  entryPaths: this.entryPaths,
64
66
  compilers: [this.syncCompilers, this.asyncCompilers],
67
+ isSkipLibs: this.isSkipLibs,
65
68
  });
66
69
  this.backend = {
67
70
  fileManager,
@@ -15,7 +15,7 @@ export declare const partitionCompilers: (rawLocalConfig: RawConfiguration) => {
15
15
  ignore?: string | string[] | undefined;
16
16
  ignoreExportsUsedInFile?: boolean | Partial<Record<"function" | "type" | "interface" | "enum" | "class" | "member", boolean>> | undefined;
17
17
  includeEntryExports?: boolean | undefined;
18
- compilers?: Record<string, ((args_0: string, ...args_1: unknown[]) => string) | ((args_0: string, ...args_1: unknown[]) => Promise<string>)> | undefined;
18
+ compilers?: Record<string, ((args_0: string, args_1: string, ...args_2: unknown[]) => string) | ((args_0: string, args_1: string, ...args_2: unknown[]) => Promise<string>)> | undefined;
19
19
  include?: ("files" | "dependencies" | "devDependencies" | "unlisted" | "binaries" | "unresolved" | "exports" | "types" | "nsExports" | "nsTypes" | "duplicates" | "enumMembers" | "classMembers")[] | undefined;
20
20
  workspaces?: Record<string, {
21
21
  ignoreBinaries?: (string | RegExp)[] | undefined;
package/dist/constants.js CHANGED
@@ -44,6 +44,7 @@ export const IGNORED_GLOBAL_BINARIES = new Set([
44
44
  'sh',
45
45
  'sudo',
46
46
  'test',
47
+ 'touch',
47
48
  'true',
48
49
  'yarn',
49
50
  'xargs',
package/dist/index.js CHANGED
@@ -112,6 +112,7 @@ export const main = async (unresolvedConfiguration) => {
112
112
  pkgName,
113
113
  isGitIgnored,
114
114
  isIsolateWorkspaces,
115
+ isSkipLibs: !isReportClassMembers,
115
116
  });
116
117
  const worker = new WorkspaceWorker({
117
118
  name,
@@ -1,13 +1,19 @@
1
1
  import ts from 'typescript';
2
2
  import type { SyncCompilers, AsyncCompilers } from '../compilers/types.js';
3
+ interface SourceFileManagerOptions {
4
+ isSkipLibs: boolean;
5
+ compilers: [SyncCompilers, AsyncCompilers];
6
+ }
3
7
  export declare class SourceFileManager {
8
+ isSkipLibs: boolean;
4
9
  sourceFileCache: Map<string, ts.SourceFile | undefined>;
5
10
  snapshotCache: Map<string, ts.IScriptSnapshot | undefined>;
6
- syncCompilers?: SyncCompilers;
7
- asyncCompilers?: AsyncCompilers;
8
- installCompilers(compilers: [SyncCompilers, AsyncCompilers]): void;
11
+ syncCompilers: SyncCompilers;
12
+ asyncCompilers: AsyncCompilers;
13
+ constructor({ compilers, isSkipLibs }: SourceFileManagerOptions);
9
14
  createSourceFile(filePath: string, contents: string): ts.SourceFile;
10
15
  getSourceFile(filePath: string): ts.SourceFile | undefined;
11
16
  getSnapshot(filePath: string): ts.IScriptSnapshot | undefined;
12
17
  compileAndAddSourceFile(filePath: string): Promise<void>;
13
18
  }
19
+ export {};
@@ -1,12 +1,14 @@
1
1
  import ts from 'typescript';
2
2
  import { debugLog } from '../util/debug.js';
3
- import { extname, isInternal } from '../util/path.js';
3
+ import { extname, isInNodeModules, isInternal } from '../util/path.js';
4
4
  export class SourceFileManager {
5
+ isSkipLibs;
5
6
  sourceFileCache = new Map();
6
7
  snapshotCache = new Map();
7
8
  syncCompilers;
8
9
  asyncCompilers;
9
- installCompilers(compilers) {
10
+ constructor({ compilers, isSkipLibs }) {
11
+ this.isSkipLibs = isSkipLibs;
10
12
  this.syncCompilers = compilers[0];
11
13
  this.asyncCompilers = compilers[1];
12
14
  }
@@ -17,6 +19,8 @@ export class SourceFileManager {
17
19
  return sourceFile;
18
20
  }
19
21
  getSourceFile(filePath) {
22
+ if (this.isSkipLibs && isInNodeModules(filePath))
23
+ return undefined;
20
24
  if (this.sourceFileCache.has(filePath))
21
25
  return this.sourceFileCache.get(filePath);
22
26
  const contents = ts.sys.readFile(filePath);
@@ -6,8 +6,9 @@ type CreateHostsOptions = {
6
6
  compilerOptions: ts.CompilerOptions;
7
7
  entryPaths: Set<string>;
8
8
  compilers: [SyncCompilers, AsyncCompilers];
9
+ isSkipLibs: boolean;
9
10
  };
10
- export declare const createHosts: ({ cwd, compilerOptions, entryPaths, compilers }: CreateHostsOptions) => {
11
+ export declare const createHosts: ({ cwd, compilerOptions, entryPaths, compilers, isSkipLibs }: CreateHostsOptions) => {
11
12
  fileManager: SourceFileManager;
12
13
  compilerHost: ts.CompilerHost;
13
14
  resolveModuleNames: (moduleNames: string[], containingFile: string) => (ts.ResolvedModuleFull | undefined)[];
@@ -6,9 +6,8 @@ import { createCustomModuleResolver } from './resolveModuleNames.js';
6
6
  import { SourceFileManager } from './SourceFileManager.js';
7
7
  import { createCustomSys } from './sys.js';
8
8
  const libLocation = path.dirname(ts.getDefaultLibFilePath({}));
9
- const fileManager = new SourceFileManager();
10
- export const createHosts = ({ cwd, compilerOptions, entryPaths, compilers }) => {
11
- fileManager.installCompilers(compilers);
9
+ export const createHosts = ({ cwd, compilerOptions, entryPaths, compilers, isSkipLibs }) => {
10
+ const fileManager = new SourceFileManager({ compilers, isSkipLibs });
12
11
  const virtualFileExtensions = getCompilerExtensions(compilers);
13
12
  const sys = createCustomSys(cwd, virtualFileExtensions);
14
13
  const resolveModuleNames = createCustomModuleResolver(sys, compilerOptions, virtualFileExtensions);
@@ -2,7 +2,7 @@ import { existsSync } from 'node:fs';
2
2
  import { isBuiltin } from 'node:module';
3
3
  import ts from 'typescript';
4
4
  import { sanitizeSpecifier } from '../util/modules.js';
5
- import { basename, dirname, extname, format, isAbsolute, isInternal, join } from '../util/path.js';
5
+ import { basename, dirname, extname, format, isAbsolute, isInNodeModules, isInternal, join } from '../util/path.js';
6
6
  import { isDeclarationFileExtension } from './ast-helpers.js';
7
7
  import { ensureRealFilePath, isVirtualFilePath } from './utils.js';
8
8
  const resolutionCache = new Map();
@@ -17,22 +17,22 @@ const fileExists = (name, containingFile) => {
17
17
  };
18
18
  }
19
19
  };
20
- const DECLARATION_EXTENSIONS_MAP = {
20
+ const DTS_EXTENSIONS_MAP = {
21
21
  [ts.Extension.Dts]: ts.Extension.Js,
22
22
  [ts.Extension.Dmts]: ts.Extension.Mjs,
23
23
  [ts.Extension.Dcts]: ts.Extension.Cjs,
24
24
  };
25
- const jsMatchingDeclarationFileExists = (resolveDtsFileName, declarationFileExtension) => {
26
- const mappedExtension = DECLARATION_EXTENSIONS_MAP[declarationFileExtension];
25
+ const jsMatchingDeclarationFileExists = (resolveDtsFileName, dtsExtension) => {
26
+ const extension = DTS_EXTENSIONS_MAP[dtsExtension];
27
27
  const resolvedFileName = format({
28
- ext: mappedExtension,
28
+ ext: extension,
29
29
  dir: dirname(resolveDtsFileName),
30
- name: basename(resolveDtsFileName, declarationFileExtension),
30
+ name: basename(resolveDtsFileName, dtsExtension),
31
31
  });
32
32
  if (existsSync(resolvedFileName)) {
33
33
  return {
34
34
  resolvedFileName,
35
- extension: mappedExtension,
35
+ extension,
36
36
  isExternalLibraryImport: false,
37
37
  resolvedUsingTsExtension: false,
38
38
  };
@@ -55,6 +55,8 @@ export function createCustomModuleResolver(customSys, compilerOptions, virtualFi
55
55
  const sanitizedSpecifier = sanitizeSpecifier(name);
56
56
  if (isBuiltin(sanitizedSpecifier))
57
57
  return undefined;
58
+ if (isInNodeModules(name))
59
+ return undefined;
58
60
  const tsResolvedModule = ts.resolveModuleName(sanitizedSpecifier, containingFile, compilerOptions, ts.sys).resolvedModule;
59
61
  if (!tsResolvedModule) {
60
62
  const extension = extname(sanitizedSpecifier);
@@ -65,7 +67,7 @@ export function createCustomModuleResolver(customSys, compilerOptions, virtualFi
65
67
  }
66
68
  }
67
69
  if (tsResolvedModule &&
68
- isDeclarationFileExtension(tsResolvedModule?.extension) &&
70
+ isDeclarationFileExtension(tsResolvedModule.extension) &&
69
71
  isInternal(tsResolvedModule.resolvedFileName)) {
70
72
  {
71
73
  const module = jsMatchingDeclarationFileExists(tsResolvedModule.resolvedFileName, tsResolvedModule.extension);
@@ -83,7 +85,7 @@ export function createCustomModuleResolver(customSys, compilerOptions, virtualFi
83
85
  if (tsResolvedModule && !isVirtualFilePath(tsResolvedModule.resolvedFileName, virtualFileExtensions)) {
84
86
  return tsResolvedModule;
85
87
  }
86
- const customResolvedModule = ts.resolveModuleName(name, containingFile, compilerOptions, customSys).resolvedModule;
88
+ const customResolvedModule = ts.resolveModuleName(sanitizedSpecifier, containingFile, compilerOptions, customSys).resolvedModule;
87
89
  if (!customResolvedModule || !isVirtualFilePath(customResolvedModule.resolvedFileName, virtualFileExtensions)) {
88
90
  return customResolvedModule;
89
91
  }
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const version = "5.1.0";
1
+ export declare const version = "5.1.2";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = '5.1.0';
1
+ export const version = '5.1.2';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "knip",
3
- "version": "5.1.0",
3
+ "version": "5.1.2",
4
4
  "description": "Find unused files, dependencies and exports in your TypeScript and JavaScript projects",
5
5
  "homepage": "https://knip.dev",
6
6
  "repository": {