knip 5.6.1 → 5.7.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.
@@ -1,7 +1,7 @@
1
1
  import ts from 'typescript';
2
2
  import { ProjectPrincipal } from './ProjectPrincipal.js';
3
3
  import { debugLog } from './util/debug.js';
4
- import { toAbsolute } from './util/path.js';
4
+ import { toAbsolute, toRelative } from './util/path.js';
5
5
  const mapToAbsolutePaths = (paths, cwd) => Object.keys(paths).reduce((result, key) => {
6
6
  result[key] = paths[key].map(entry => toAbsolute(entry, cwd));
7
7
  return result;
@@ -66,7 +66,7 @@ export class PrincipalFactory {
66
66
  deletePrincipal(principal) {
67
67
  const p = Array.from(this.principals).find(p => p.principal === principal);
68
68
  if (p) {
69
- debugLog('*', `Deleting principal at ${[...p.cwds]} for ${[...p.pkgNames]}`);
69
+ debugLog('*', `Deleting principal at ${[...p.cwds].map(cwd => toRelative(cwd) || '.')} (${[...p.pkgNames]})`);
70
70
  this.principals.delete(p);
71
71
  }
72
72
  }
@@ -25,7 +25,7 @@ export interface BoundSourceFile extends ts.SourceFile {
25
25
  pragmas?: Map<string, PragmaMap | PragmaMap[]>;
26
26
  }
27
27
  export interface ProgramMaybe53 extends ts.Program {
28
- getResolvedModule?: (f: ts.SourceFile, moduleName: string, mode: ts.ResolutionMode) => ts.ResolvedModuleWithFailedLookupLocations | undefined;
28
+ getResolvedModule?: (sourceFile: ts.SourceFile, moduleName: string, mode: ts.ResolutionMode) => ts.ResolvedModuleWithFailedLookupLocations | undefined;
29
29
  }
30
30
  export type GetResolvedModule = (name: string) => ts.ResolvedModuleWithFailedLookupLocations | undefined;
31
31
  export {};
@@ -1,8 +1,10 @@
1
1
  import { existsSync } from 'node:fs';
2
2
  import { isBuiltin } from 'node:module';
3
+ import resolve from 'resolve';
3
4
  import ts from 'typescript';
5
+ import { DEFAULT_EXTENSIONS } from '../constants.js';
4
6
  import { sanitizeSpecifier } from '../util/modules.js';
5
- import { dirname, extname, isAbsolute, isInNodeModules, isInternal, join } from '../util/path.js';
7
+ import { dirname, extname, isAbsolute, isInNodeModules, isInternal, join, toPosix } from '../util/path.js';
6
8
  import { isDeclarationFileExtension } from './ast-helpers.js';
7
9
  import { ensureRealFilePath, isVirtualFilePath } from './utils.js';
8
10
  const resolutionCache = new Map();
@@ -18,6 +20,7 @@ const fileExists = (name, containingFile) => {
18
20
  }
19
21
  };
20
22
  export function createCustomModuleResolver(customSys, compilerOptions, virtualFileExtensions) {
23
+ const extensions = [...DEFAULT_EXTENSIONS, ...virtualFileExtensions];
21
24
  function resolveModuleNames(moduleNames, containingFile) {
22
25
  return moduleNames.map(moduleName => {
23
26
  const key = moduleName.startsWith('.')
@@ -34,6 +37,24 @@ export function createCustomModuleResolver(customSys, compilerOptions, virtualFi
34
37
  const sanitizedSpecifier = sanitizeSpecifier(name);
35
38
  if (isBuiltin(sanitizedSpecifier) || isInNodeModules(name))
36
39
  return undefined;
40
+ try {
41
+ const resolved = resolve.sync(sanitizedSpecifier, {
42
+ basedir: dirname(containingFile),
43
+ extensions,
44
+ preserveSymlinks: false,
45
+ });
46
+ const resolvedFileName = toPosix(resolved);
47
+ const ext = extname(resolved);
48
+ const extension = virtualFileExtensions.includes(ext) ? ts.Extension.Js : ext;
49
+ return {
50
+ resolvedFileName,
51
+ extension,
52
+ isExternalLibraryImport: isInNodeModules(resolvedFileName),
53
+ resolvedUsingTsExtension: false,
54
+ };
55
+ }
56
+ catch (err) {
57
+ }
37
58
  const tsResolvedModule = ts.resolveModuleName(sanitizedSpecifier, containingFile, compilerOptions, ts.sys).resolvedModule;
38
59
  if (tsResolvedModule &&
39
60
  isDeclarationFileExtension(tsResolvedModule.extension) &&
@@ -36,7 +36,7 @@ function parseGitignoreFile(filePath) {
36
36
  return file
37
37
  .split(/\r?\n/)
38
38
  .filter(line => line && !line.startsWith('#'))
39
- .map(pattern => convertGitignoreToMicromatch(pattern.replace(/#.*/, '').trim()));
39
+ .map(pattern => convertGitignoreToMicromatch(pattern.replace(/(?<!\\)#.*/, '').trim()));
40
40
  }
41
41
  async function parseFindGitignores(options) {
42
42
  const ignores = ['.git', ...GLOBAL_IGNORE_PATTERNS];
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const version = "5.6.1";
1
+ export declare const version = "5.7.0";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = '5.6.1';
1
+ export const version = '5.7.0';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "knip",
3
- "version": "5.6.1",
3
+ "version": "5.7.0",
4
4
  "description": "Find unused files, dependencies and exports in your TypeScript and JavaScript projects",
5
5
  "homepage": "https://knip.dev",
6
6
  "repository": {
@@ -74,6 +74,7 @@
74
74
  "picocolors": "1.0.0",
75
75
  "picomatch": "4.0.1",
76
76
  "pretty-ms": "9.0.0",
77
+ "resolve": "1.22.8",
77
78
  "smol-toml": "1.1.4",
78
79
  "strip-json-comments": "5.0.1",
79
80
  "summary": "2.1.0",
@@ -96,6 +97,7 @@
96
97
  "@types/minimist": "^1.2.5",
97
98
  "@types/npmcli__map-workspaces": "^3.0.4",
98
99
  "@types/npmcli__package-json": "^4.0.4",
100
+ "@types/resolve": "1.20.6",
99
101
  "@types/webpack": "^5.28.5",
100
102
  "c8": "9.1.0",
101
103
  "eslint": "^8.57.0",