Skip to content

Commit 53839e0

Browse files
committed
Remove module resolving from plugins
1 parent 98c1ff7 commit 53839e0

File tree

4 files changed

+12
-35
lines changed

4 files changed

+12
-35
lines changed
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import parseArgs from 'minimist';
22
import type { Resolver } from '../../types/config.js';
3-
import { tryResolveFilePath } from '../util.js';
3+
import { toEntry } from '../../util/dependencies.js';
4+
import { isFile } from '../../util/fs.js';
5+
import { isAbsolute, join } from '../../util/path.js';
46

57
const commands = ['add', 'create', 'init', 'install', 'link', 'pm', 'remove', 'run', 'test', 'update', 'upgrade'];
68

@@ -10,7 +12,7 @@ export const resolve: Resolver = (_binary, args, { manifestScriptNames, cwd, fro
1012
if (command === 'run' && manifestScriptNames.has(script)) return [];
1113
if (manifestScriptNames.has(command) || commands.includes(command)) return [];
1214
const filePath = command === 'run' ? script : command;
13-
const specifier = tryResolveFilePath(cwd, filePath);
14-
if (specifier) return [specifier];
15+
const absFilePath = isAbsolute(filePath) ? filePath : join(cwd, filePath);
16+
if (isFile(absFilePath)) return [toEntry(absFilePath)];
1517
return fromArgs(args);
1618
};

‎packages/knip/src/binaries/util.ts‎

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,3 @@
1-
import { toBinary, toDependency, toEntry } from '../util/dependencies.js';
2-
import { getPackageNameFromFilePath, getPackageNameFromModuleSpecifier } from '../util/modules.js';
3-
import { isAbsolute, isInNodeModules, join } from '../util/path.js';
4-
import { _resolveSync } from '../util/resolve.js';
5-
6-
export const tryResolveFilePath = (cwd: string, specifier: string, acceptModuleSpecifier?: boolean) => {
7-
if (specifier) {
8-
const filePath = isAbsolute(specifier) ? specifier : join(cwd, specifier);
9-
if (!isInNodeModules(filePath)) {
10-
const resolvedFilePath = _resolveSync(filePath, cwd);
11-
if (resolvedFilePath) {
12-
return toEntry(resolvedFilePath);
13-
}
14-
if (acceptModuleSpecifier) {
15-
const p = getPackageNameFromModuleSpecifier(specifier);
16-
if (p) return toDependency(p);
17-
}
18-
} else if (specifier.includes('node_modules/.bin')) {
19-
return toBinary(trimBinary(specifier));
20-
} else {
21-
return toDependency(getPackageNameFromFilePath(specifier));
22-
}
23-
}
24-
};
25-
261
export const stripVersionFromSpecifier = (specifier: string) => specifier.replace(/(\S+)@.*/, '$1');
272

283
const stripNodeModulesFromPath = (command: string) => command.replace(/^(\.\/)?node_modules\//, '');

‎packages/knip/src/util/handle-referenced-dependency.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@ import type { DependencyDeputy } from '../DependencyDeputy.js';
33
import type { IssueCollector } from '../IssueCollector.js';
44
import { IGNORED_RUNTIME_DEPENDENCIES } from '../constants.js';
55
import { type Dependency, fromBinary, isBinary, isConfigPattern, isDependency } from './dependencies.js';
6-
import { getPackageNameFromFilePath, getPackageNameFromModuleSpecifier } from './modules.js';
7-
import { dirname, isAbsolute, isInNodeModules, isInternal, join } from './path.js';
6+
import { getPackageNameFromSpecifier } from './modules.js';
7+
import { dirname, isAbsolute, isInternal, join } from './path.js';
88
import { _resolveSync } from './resolve.js';
99

10-
const getPackageName = (specifier: string) =>
11-
isInNodeModules(specifier) ? getPackageNameFromFilePath(specifier) : getPackageNameFromModuleSpecifier(specifier);
12-
1310
/**
1411
* Resolve internal file paths + collect issues
1512
*
@@ -41,7 +38,7 @@ export const getReferencedDependencyHandler =
4138
return;
4239
}
4340

44-
const packageName = getPackageName(specifier);
41+
const packageName = getPackageNameFromSpecifier(specifier);
4542

4643
if (packageName) {
4744
// Attempt fast path first for external dependencies and internal workspaces

‎packages/knip/src/util/modules.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { isBuiltin } from 'node:module';
22
import { DT_SCOPE } from '../constants.js';
3-
import { isAbsolute, toPosix } from './path.js';
3+
import { isAbsolute, isInNodeModules, toPosix } from './path.js';
44

55
export const getPackageNameFromModuleSpecifier = (moduleSpecifier: string) => {
66
if (!isStartsLikePackageName(moduleSpecifier)) return;
@@ -15,6 +15,9 @@ export const getPackageNameFromFilePath = (value: string) => {
1515
return value;
1616
};
1717

18+
export const getPackageNameFromSpecifier = (specifier: string) =>
19+
isInNodeModules(specifier) ? getPackageNameFromFilePath(specifier) : getPackageNameFromModuleSpecifier(specifier);
20+
1821
export const isStartsLikePackageName = (specifier: string) => /^(@[a-z0-9._]|[a-z0-9])/.test(specifier);
1922

2023
export const isDefinitelyTyped = (packageName: string) => packageName.startsWith(`${DT_SCOPE}/`);

0 commit comments

Comments
 (0)