knip 5.30.0 → 5.30.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.
@@ -37,7 +37,7 @@ export declare class ConfigurationChief {
37
37
  manifest?: PackageJson;
38
38
  ignoredWorkspacePatterns: string[];
39
39
  workspacePackages: Map<string, Package>;
40
- workspacePackagesByName: Map<string, Package>;
40
+ workspacePackagesByPkgName: Map<string, Package>;
41
41
  additionalWorkspaceNames: Set<string>;
42
42
  availableWorkspaceNames: string[];
43
43
  availableWorkspacePkgNames: Set<string>;
@@ -54,7 +54,7 @@ export class ConfigurationChief {
54
54
  manifest;
55
55
  ignoredWorkspacePatterns = [];
56
56
  workspacePackages = new Map();
57
- workspacePackagesByName = new Map();
57
+ workspacePackagesByPkgName = new Map();
58
58
  additionalWorkspaceNames = new Set();
59
59
  availableWorkspaceNames = [];
60
60
  availableWorkspacePkgNames = new Set();
@@ -157,7 +157,7 @@ export class ConfigurationChief {
157
157
  const workspaceNames = compact([...this.getListedWorkspaces(), ...this.additionalWorkspaceNames]);
158
158
  const [byName, byPkgName] = await mapWorkspaces(this.cwd, workspaceNames);
159
159
  this.workspacePackages = byName;
160
- this.workspacePackagesByName = byPkgName;
160
+ this.workspacePackagesByPkgName = byPkgName;
161
161
  this.addRootPackage();
162
162
  this.availableWorkspaceNames = this.getAvailableWorkspaceNames(byName.keys());
163
163
  this.availableWorkspacePkgNames = this.getAvailableWorkspacePkgNames(byPkgName.keys());
@@ -178,7 +178,7 @@ export class ConfigurationChief {
178
178
  manifest: this.manifest,
179
179
  };
180
180
  this.workspacePackages.set('.', rootPackage);
181
- this.workspacePackagesByName.set(pkgName, rootPackage);
181
+ this.workspacePackagesByPkgName.set(pkgName, rootPackage);
182
182
  }
183
183
  }
184
184
  getListedWorkspaces() {
@@ -10,7 +10,7 @@ export const resolve = (_binary, args, options) => {
10
10
  });
11
11
  const packageSpecifier = parsed._[0];
12
12
  const specifier = packageSpecifier ? stripVersionFromSpecifier(packageSpecifier) : '';
13
- const packages = parsed.package ? [parsed.package].flat().map(stripVersionFromSpecifier) : [];
13
+ const packages = parsed.package && !parsed.yes ? [parsed.package].flat().map(stripVersionFromSpecifier) : [];
14
14
  const command = parsed.call ? fromArgs([parsed.call]) : [];
15
15
  const restArgs = argsFrom(args, packageSpecifier);
16
16
  const isBinary = specifier && !packageSpecifier.includes('@') && !isInternal(specifier) && !dependencies.has(specifier);
package/dist/index.js CHANGED
@@ -16,7 +16,8 @@ import { _glob, negate } from './util/glob.js';
16
16
  import { getReferencedDependencyHandler } from './util/handle-dependency.js';
17
17
  import { getHasStrictlyNsReferences, getType } from './util/has-strictly-ns-references.js';
18
18
  import { getIsIdentifierReferencedHandler } from './util/is-identifier-referenced.js';
19
- import { getEntryPathsFromManifest, getPackageNameFromModuleSpecifier } from './util/modules.js';
19
+ import { getPackageNameFromModuleSpecifier } from './util/modules.js';
20
+ import { getEntryPathsFromManifest } from './util/package-json.js';
20
21
  import { dirname, join } from './util/path.js';
21
22
  import { findMatch } from './util/regex.js';
22
23
  import { getShouldIgnoreHandler, getShouldIgnoreTagHandler } from './util/tag.js';
@@ -57,6 +57,7 @@ const resolveDependencies = async (config, options) => {
57
57
  : []).filter(value => !/\$[0-9]/.test(value));
58
58
  const testResultsProcessor = config.testResultsProcessor ? [config.testResultsProcessor] : [];
59
59
  const snapshotResolver = config.snapshotResolver ? [config.snapshotResolver] : [];
60
+ const snapshotSerializers = config.snapshotSerializers ?? [];
60
61
  const testSequencer = config.testSequencer ? [config.testSequencer] : [];
61
62
  const resolve = (specifier) => resolveEntry(options, specifier);
62
63
  const setupFiles = (config.setupFiles ?? []).map(resolve);
@@ -77,6 +78,7 @@ const resolveDependencies = async (config, options) => {
77
78
  ...moduleNameMapper,
78
79
  ...testResultsProcessor,
79
80
  ...snapshotResolver,
81
+ ...snapshotSerializers,
80
82
  ...testSequencer,
81
83
  ...globalSetup,
82
84
  ...globalTeardown,
@@ -3,7 +3,7 @@ import ts from 'typescript';
3
3
  import { ALIAS_TAG, ANONYMOUS, DEFAULT_EXTENSIONS, IMPORT_STAR } from '../constants.js';
4
4
  import { timerify } from '../util/Performance.js';
5
5
  import { addNsValue, addValue, createImports } from '../util/dependency-graph.js';
6
- import { isStartsLikePackageName, sanitizeSpecifier } from '../util/modules.js';
6
+ import { getPackageNameFromFilePath, isStartsLikePackageName, sanitizeSpecifier } from '../util/modules.js';
7
7
  import { extname, isInNodeModules } from '../util/path.js';
8
8
  import { shouldIgnore } from '../util/tag.js';
9
9
  import { getAccessMembers, getDestructuredIds, getJSDocTags, getLineAndCharacterOfPosition, getTypeName, isAccessExpression, isConsiderReferencedNS, isDestructuring, isImportSpecifier, isReferencedInExportedType, } from './ast-helpers.js';
@@ -102,7 +102,9 @@ const getImportsAndExports = (sourceFile, resolveModule, typeChecker, options) =
102
102
  if (module.isExternalLibraryImport) {
103
103
  if (skipTypeOnly && isTypeOnly)
104
104
  return;
105
- const sanitizedSpecifier = sanitizeSpecifier(specifier);
105
+ const sanitizedSpecifier = isInNodeModules(specifier)
106
+ ? getPackageNameFromFilePath(specifier)
107
+ : sanitizeSpecifier(specifier);
106
108
  if (!isStartsLikePackageName(sanitizedSpecifier)) {
107
109
  return;
108
110
  }
@@ -55,7 +55,7 @@ export function createCustomModuleResolver(compilerOptions, customCompilerExtens
55
55
  }
56
56
  function resolveModuleName(name, containingFile) {
57
57
  const sanitizedSpecifier = sanitizeSpecifier(name);
58
- if (isBuiltin(sanitizedSpecifier) || isInNodeModules(name))
58
+ if (isBuiltin(sanitizedSpecifier))
59
59
  return undefined;
60
60
  const resolvedFileName = resolveSync(sanitizedSpecifier, dirname(containingFile));
61
61
  if (resolvedFileName) {
@@ -39,10 +39,17 @@ export const getReferencedDependencyHandler = (collector, deputy, chief) => (spe
39
39
  symbol: specifier,
40
40
  });
41
41
  if (packageName && specifier !== packageName) {
42
- if (chief.workspacePackagesByName.get(packageName)) {
43
- const filePath = _resolveSync(specifier, dirname(containingFilePath));
44
- if (filePath)
45
- return filePath;
42
+ const specifierWorkspace = chief.workspacePackagesByPkgName.get(packageName);
43
+ if (specifierWorkspace) {
44
+ if (specifier.startsWith(packageName)) {
45
+ const dir = specifier.replace(new RegExp(`^${packageName}`), `./${specifierWorkspace.name}`);
46
+ const resolvedFilePath = _resolveSync(dir, chief.cwd);
47
+ if (resolvedFilePath)
48
+ return resolvedFilePath;
49
+ }
50
+ const resolvedFilePath = _resolveSync(specifier, dirname(containingFilePath));
51
+ if (resolvedFilePath)
52
+ return resolvedFilePath;
46
53
  collector.addIssue({
47
54
  type: 'unresolved',
48
55
  filePath: containingFilePath,
@@ -1,14 +1,7 @@
1
- import type { PackageJson } from '../types/package-json.js';
2
1
  export declare const getPackageNameFromModuleSpecifier: (moduleSpecifier: string) => string | undefined;
3
2
  export declare const getPackageNameFromFilePath: (value: string) => string;
4
3
  export declare const isStartsLikePackageName: (specifier: string) => boolean;
5
4
  export declare const isDefinitelyTyped: (packageName: string) => boolean;
6
5
  export declare const getDefinitelyTypedFor: (packageName: string) => string;
7
6
  export declare const getPackageFromDefinitelyTyped: (typedDependency: string) => string;
8
- export declare const getEntryPathsFromManifest: (manifest: PackageJson, sharedGlobOptions: {
9
- cwd: string;
10
- dir: string;
11
- gitignore: boolean;
12
- ignore: string[];
13
- }) => Promise<string[]>;
14
7
  export declare const sanitizeSpecifier: (specifier: string) => string;
@@ -1,7 +1,5 @@
1
1
  import { isBuiltin } from 'node:module';
2
2
  import { DT_SCOPE } from '../constants.js';
3
- import { _glob } from './glob.js';
4
- import { getStringValues } from './object.js';
5
3
  import { isAbsolute, toPosix } from './path.js';
6
4
  export const getPackageNameFromModuleSpecifier = (moduleSpecifier) => {
7
5
  if (!isStartsLikePackageName(moduleSpecifier))
@@ -32,28 +30,6 @@ export const getPackageFromDefinitelyTyped = (typedDependency) => {
32
30
  }
33
31
  return typedDependency;
34
32
  };
35
- export const getEntryPathsFromManifest = (manifest, sharedGlobOptions) => {
36
- const { main, bin, exports, types, typings } = manifest;
37
- const entryPaths = new Set();
38
- if (typeof main === 'string')
39
- entryPaths.add(main);
40
- if (bin) {
41
- if (typeof bin === 'string')
42
- entryPaths.add(bin);
43
- if (typeof bin === 'object')
44
- for (const id of Object.values(bin))
45
- entryPaths.add(id);
46
- }
47
- if (exports) {
48
- for (const item of getStringValues(exports))
49
- entryPaths.add(item);
50
- }
51
- if (typeof types === 'string')
52
- entryPaths.add(types);
53
- if (typeof typings === 'string')
54
- entryPaths.add(typings);
55
- return _glob({ ...sharedGlobOptions, patterns: Array.from(entryPaths) });
56
- };
57
33
  const matchDirectives = /^([?!|-]+)?([^!?:]+).*/;
58
34
  export const sanitizeSpecifier = (specifier) => {
59
35
  if (isBuiltin(specifier))
@@ -7,4 +7,10 @@ interface ExtendedPackageJson extends PackageJson {
7
7
  }
8
8
  export declare const load: (filePath: string) => Promise<ExtendedPackageJson>;
9
9
  export declare const save: (filePath: string, content: ExtendedPackageJson) => Promise<void>;
10
+ export declare const getEntryPathsFromManifest: (manifest: PackageJson, sharedGlobOptions: {
11
+ cwd: string;
12
+ dir: string;
13
+ gitignore: boolean;
14
+ ignore: string[];
15
+ }) => Promise<string[]>;
10
16
  export {};
@@ -1,4 +1,6 @@
1
1
  import { readFile, writeFile } from 'node:fs/promises';
2
+ import { _glob } from './glob.js';
3
+ import { getStringValues } from './object.js';
2
4
  const INDENT = Symbol.for('indent');
3
5
  const NEWLINE = Symbol.for('newline');
4
6
  const DEFAULT_NEWLINE = '\n';
@@ -28,3 +30,25 @@ export const save = async (filePath, content) => {
28
30
  const fileContent = `${JSON.stringify(content, null, space)}\n`.replace(/\n/g, EOL);
29
31
  await writeFile(filePath, fileContent);
30
32
  };
33
+ export const getEntryPathsFromManifest = (manifest, sharedGlobOptions) => {
34
+ const { main, bin, exports, types, typings } = manifest;
35
+ const entryPaths = new Set();
36
+ if (typeof main === 'string')
37
+ entryPaths.add(main);
38
+ if (bin) {
39
+ if (typeof bin === 'string')
40
+ entryPaths.add(bin);
41
+ if (typeof bin === 'object')
42
+ for (const id of Object.values(bin))
43
+ entryPaths.add(id);
44
+ }
45
+ if (exports) {
46
+ for (const item of getStringValues(exports))
47
+ entryPaths.add(item);
48
+ }
49
+ if (typeof types === 'string')
50
+ entryPaths.add(types);
51
+ if (typeof typings === 'string')
52
+ entryPaths.add(typings);
53
+ return _glob({ ...sharedGlobOptions, patterns: Array.from(entryPaths) });
54
+ };
@@ -1,6 +1,6 @@
1
1
  import { basename, dirname } from './path.js';
2
- const getName2 = (parent, base) => (parent.charAt(0) === '@' ? `${parent}/${base}` : base);
3
- const getName = (dir) => (dir ? getName2(basename(dirname(dir)), basename(dir)) : undefined);
2
+ const getPkgName = (parent, base) => (parent.charAt(0) === '@' ? `${parent}/${base}` : base);
3
+ const getName = (dir) => (dir ? getPkgName(basename(dirname(dir)), basename(dir)) : undefined);
4
4
  export function getPackageName(pkg, pathname) {
5
5
  const { name } = pkg;
6
6
  return name || getName(pathname);
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const version = "5.30.0";
1
+ export declare const version = "5.30.2";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = '5.30.0';
1
+ export const version = '5.30.2';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "knip",
3
- "version": "5.30.0",
3
+ "version": "5.30.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": {