knip 5.6.0 → 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
  }
@@ -7,39 +7,31 @@ const enablers = ['typescript'];
7
7
  const isEnabled = ({ dependencies }) => hasDependency(dependencies, enablers);
8
8
  const config = ['tsconfig.json', 'tsconfig.*.json'];
9
9
  const production = [];
10
- const resolveExtensibleConfig = async (configFilePath) => {
10
+ const getExtends = async (configFilePath, internalExtends = new Set()) => {
11
11
  const filePath = configFilePath.replace(/(\.json)?$/, '.json');
12
12
  const localConfig = await loadJSON(filePath);
13
13
  if (!localConfig)
14
- return;
15
- const extends_ = (localConfig.extends = localConfig.extends ? [localConfig.extends].flat() : []);
14
+ return internalExtends;
15
+ const extends_ = localConfig.extends ? [localConfig.extends].flat() : [];
16
16
  for (const extend of extends_) {
17
17
  if (isInternal(extend)) {
18
18
  const presetConfigPath = toAbsolute(extend, dirname(configFilePath));
19
- const presetConfig = await resolveExtensibleConfig(presetConfigPath);
20
- localConfig.extends.push(...(presetConfig?.extends ? [presetConfig.extends].flat() : []));
19
+ await getExtends(presetConfigPath, internalExtends);
21
20
  }
22
21
  }
23
- return localConfig;
22
+ extends_.forEach(extend => internalExtends.add(extend));
23
+ return internalExtends;
24
24
  };
25
25
  export const resolveConfig = async (localConfig, options) => {
26
26
  const { isProduction, configFileDir, configFileName } = options;
27
27
  const configFilePath = join(configFileDir, configFileName);
28
28
  const { compilerOptions } = await loadTSConfig(configFilePath);
29
- const extends_ = (localConfig.extends = localConfig.extends ? [localConfig.extends].flat() : []);
30
- for (const extend of extends_) {
31
- if (isInternal(extend)) {
32
- const presetConfigPath = toAbsolute(extend, dirname(configFilePath));
33
- const presetConfig = await resolveExtensibleConfig(presetConfigPath);
34
- localConfig.extends.push(...(presetConfig?.extends ? [presetConfig.extends].flat() : []));
35
- }
36
- }
29
+ const extend = await getExtends(configFilePath);
37
30
  if (!compilerOptions || !localConfig)
38
31
  return [];
39
32
  const jsx = compilerOptions?.jsxImportSource ? [compilerOptions.jsxImportSource] : [];
40
33
  if (isProduction)
41
34
  return [...jsx];
42
- const extend = localConfig.extends ? [localConfig.extends].flat().filter(extend => !isInternal(extend)) : [];
43
35
  const types = compilerOptions.types ?? [];
44
36
  const plugins = Array.isArray(compilerOptions?.plugins)
45
37
  ? compilerOptions.plugins.map(plugin => (typeof plugin === 'object' && 'name' in plugin ? plugin.name : ''))
@@ -1,6 +1,3 @@
1
1
  import type { ViteConfig } from './types.js';
2
- type BuiltinEnvironment = 'node' | 'jsdom' | 'happy-dom' | 'edge-runtime';
3
- type VitestEnvironment = BuiltinEnvironment | (string & Record<never, never>);
4
- export declare const getEnvPackageName: (env: VitestEnvironment) => any;
5
- export declare const getExternalReporters: (reporters?: ViteConfig['test']['reporters']) => unknown[];
6
- export {};
2
+ export declare const getEnvPackageName: (env: string) => string;
3
+ export declare const getExternalReporters: (reporters?: ViteConfig['test']['reporters']) => string[];
@@ -10,13 +10,13 @@ const envPackageNames = {
10
10
  'edge-runtime': '@edge-runtime/vm',
11
11
  };
12
12
  export const getEnvPackageName = (env) => {
13
- if (env === 'node')
14
- return null;
15
13
  if (env in envPackageNames)
16
14
  return envPackageNames[env];
17
15
  return `vitest-environment-${env}`;
18
16
  };
19
17
  const builtInReporters = ['default', 'verbose', 'dot', 'json', 'tap', 'tap-flat', 'junit', 'hanging-process'];
20
18
  export const getExternalReporters = (reporters) => reporters
21
- ? [reporters].flat().filter(reporter => typeof reporter === 'string' && !builtInReporters.includes(reporter))
19
+ ? [reporters]
20
+ .flat()
21
+ .filter((reporter) => typeof reporter === 'string' && !builtInReporters.includes(reporter))
22
22
  : [];
@@ -27,7 +27,7 @@ const findConfigDependencies = (localConfig, options) => {
27
27
  const testConfig = localConfig.test;
28
28
  if (!testConfig)
29
29
  return [];
30
- const environments = testConfig.environment ? [getEnvPackageName(testConfig.environment)] : [];
30
+ const environments = testConfig.environment && testConfig.environment !== 'node' ? [getEnvPackageName(testConfig.environment)] : [];
31
31
  const reporters = getExternalReporters(testConfig.reporters);
32
32
  const hasCoverageEnabled = (testConfig.coverage && testConfig.coverage.enabled !== false) || hasScriptWithCoverage(manifest.scripts);
33
33
  const coverage = hasCoverageEnabled ? [`@vitest/coverage-${testConfig.coverage?.provider ?? 'v8'}`] : [];
@@ -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.0";
1
+ export declare const version = "5.7.0";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = '5.6.0';
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.0",
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",