knip 6.16.0 → 6.16.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.
@@ -19,6 +19,7 @@ export declare class ProjectPrincipal {
19
19
  syncCompilers: SyncCompilers;
20
20
  asyncCompilers: AsyncCompilers;
21
21
  private paths;
22
+ private rootDirs;
22
23
  private extensions;
23
24
  cache: CacheConsultant<FileNode>;
24
25
  toSourceFilePath: ToSourceFilePath;
@@ -31,6 +32,7 @@ export declare class ProjectPrincipal {
31
32
  constructor(options: MainOptions, toSourceFilePath: ToSourceFilePath, findWorkspaceManifestImports?: WorkspaceManifestHandler);
32
33
  addCompilers(compilers: [SyncCompilers, AsyncCompilers]): void;
33
34
  addPaths(paths: Paths, basePath: string, scope: string): void;
35
+ addRootDirs(rootDirs: string[] | undefined, scope: string): void;
34
36
  init(): void;
35
37
  readFile(filePath: string): string;
36
38
  private hasAcceptedExtension;
@@ -29,6 +29,7 @@ export class ProjectPrincipal {
29
29
  syncCompilers = new Map();
30
30
  asyncCompilers = new Map();
31
31
  paths = new Map();
32
+ rootDirs = new Map();
32
33
  extensions = new Set(DEFAULT_EXTENSIONS);
33
34
  cache;
34
35
  toSourceFilePath;
@@ -72,6 +73,12 @@ export class ProjectPrincipal {
72
73
  }
73
74
  this.paths.set(scope, scoped);
74
75
  }
76
+ addRootDirs(rootDirs, scope) {
77
+ if (!rootDirs?.length)
78
+ return;
79
+ const scoped = this.rootDirs.get(scope) ?? [];
80
+ this.rootDirs.set(scope, compact([...scoped, ...rootDirs]));
81
+ }
75
82
  init() {
76
83
  this.extensions = new Set([
77
84
  ...DEFAULT_EXTENSIONS,
@@ -79,7 +86,8 @@ export class ProjectPrincipal {
79
86
  ]);
80
87
  const customCompilerExtensions = getCompilerExtensions([this.syncCompilers, this.asyncCompilers]);
81
88
  const scopedPaths = this.paths.size > 0 ? Array.from(this.paths, ([scope, paths]) => ({ scope, paths })) : undefined;
82
- this.resolveModule = createCustomModuleResolver({ scopedPaths }, customCompilerExtensions, this.toSourceFilePath, this.findWorkspaceManifestImports);
89
+ const scopedRootDirs = this.rootDirs.size > 0 ? Array.from(this.rootDirs, ([scope, rootDirs]) => ({ scope, rootDirs })) : undefined;
90
+ this.resolveModule = createCustomModuleResolver({ scopedPaths, scopedRootDirs }, customCompilerExtensions, this.toSourceFilePath, this.findWorkspaceManifestImports);
83
91
  }
84
92
  readFile(filePath) {
85
93
  return this.fileManager.readFile(filePath);
@@ -129,6 +129,7 @@ export async function build({ chief, collector, counselor, deputy, principal, is
129
129
  for (const dep of getManifestImportDependencies(manifest))
130
130
  deputy.addReferencedDependency(name, dep);
131
131
  principal.addPaths(config.paths, dir, dir);
132
+ principal.addRootDirs(compilerOptions.rootDirs, dir);
132
133
  const inputsFromPlugins = await worker.runPlugins();
133
134
  for (const id of inputsFromPlugins)
134
135
  inputs.add(Object.assign(id, { skipExportsAnalysis: !id.allowIncludeExports }));
@@ -22,6 +22,7 @@ export interface CompilerOptions {
22
22
  name: string;
23
23
  } | string>;
24
24
  rootDir?: string;
25
+ rootDirs?: string[];
25
26
  skipDefaultLibCheck?: boolean;
26
27
  skipLibCheck?: boolean;
27
28
  sourceMap?: boolean;
@@ -4,8 +4,13 @@ type ScopedPaths = Array<{
4
4
  scope: string;
5
5
  paths: Record<string, string[]>;
6
6
  }>;
7
+ type ScopedRootDirs = Array<{
8
+ scope: string;
9
+ rootDirs: string[];
10
+ }>;
7
11
  export declare function clearModuleResolutionCaches(): void;
8
12
  export declare function createCustomModuleResolver(compilerOptions: {
9
13
  scopedPaths?: ScopedPaths;
14
+ scopedRootDirs?: ScopedRootDirs;
10
15
  }, customCompilerExtensions: string[], toSourceFilePath: ToSourceFilePath, findWorkspaceManifestImports?: WorkspaceManifestHandler): ResolveModule;
11
16
  export {};
@@ -47,12 +47,22 @@ function compilePathMappings(scopedPaths) {
47
47
  mappings.sort((a, b) => b.scope.length - a.scope.length);
48
48
  return mappings;
49
49
  }
50
+ function compileRootDirs(scopedRootDirs) {
51
+ if (!scopedRootDirs)
52
+ return undefined;
53
+ const scoped = scopedRootDirs.filter(({ rootDirs }) => rootDirs.length > 1);
54
+ if (scoped.length === 0)
55
+ return undefined;
56
+ scoped.sort((a, b) => b.scope.length - a.scope.length);
57
+ return scoped;
58
+ }
50
59
  export function createCustomModuleResolver(compilerOptions, customCompilerExtensions, toSourceFilePath, findWorkspaceManifestImports) {
51
60
  const customCompilerExtensionsSet = new Set(customCompilerExtensions);
52
61
  const hasCustomExts = customCompilerExtensionsSet.size > 0;
53
62
  const extensions = [...DEFAULT_EXTENSIONS, ...customCompilerExtensions, ...DTS_EXTENSIONS];
54
63
  const resolveSync = hasCustomExts ? _createSyncModuleResolver(extensions) : _resolveModuleSync;
55
64
  const pathMappings = compilePathMappings(compilerOptions.scopedPaths);
65
+ const rootDirMappings = compileRootDirs(compilerOptions.scopedRootDirs);
56
66
  function toSourcePath(resolvedFileName) {
57
67
  if (!hasCustomExts || !customCompilerExtensionsSet.has(extname(resolvedFileName))) {
58
68
  return toSourceFilePath(resolvedFileName) || resolvedFileName;
@@ -107,6 +117,25 @@ export function createCustomModuleResolver(compilerOptions, customCompilerExtens
107
117
  }
108
118
  }
109
119
  }
120
+ if (rootDirMappings && !isAbsolute(specifier)) {
121
+ const dir = dirname(containingFile);
122
+ for (const { scope, rootDirs } of rootDirMappings) {
123
+ if (dir !== scope && !dir.startsWith(`${scope}/`))
124
+ continue;
125
+ for (const rootDir of rootDirs) {
126
+ if (dir !== rootDir && !dir.startsWith(`${rootDir}/`))
127
+ continue;
128
+ const relPath = dir === rootDir ? '' : dir.slice(rootDir.length + 1);
129
+ for (const targetRoot of rootDirs) {
130
+ if (targetRoot === rootDir)
131
+ continue;
132
+ const resolved = resolveSync(join(targetRoot, relPath, specifier), containingFile);
133
+ if (resolved)
134
+ return toResult(resolved);
135
+ }
136
+ }
137
+ }
138
+ }
110
139
  if (specifier.startsWith('#') && findWorkspaceManifestImports) {
111
140
  const ws = findWorkspaceManifestImports(containingFile);
112
141
  if (ws) {
@@ -94,6 +94,8 @@ export const loadTSConfig = async (tsConfigFilePath) => {
94
94
  compilerOptions.outDir = absDir(compilerOptions.outDir, dir);
95
95
  if (compilerOptions.rootDir)
96
96
  compilerOptions.rootDir = absDir(compilerOptions.rootDir, dir);
97
+ if (compilerOptions.rootDirs)
98
+ compilerOptions.rootDirs = compilerOptions.rootDirs.map(d => absDir(d, dir));
97
99
  if (compilerOptions.paths) {
98
100
  compilerOptions.pathsBasePath ??= dir;
99
101
  }
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const version = "6.16.0";
1
+ export declare const version = "6.16.1";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = '6.16.0';
1
+ export const version = '6.16.1';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "knip",
3
- "version": "6.16.0",
3
+ "version": "6.16.1",
4
4
  "description": "Find and fix unused dependencies, exports and files in your TypeScript and JavaScript projects",
5
5
  "keywords": [
6
6
  "analysis",