Skip to content

Commit e7122a1

Browse files
committed
Don't flag undeclared sibling workspace imports as unlisted (#1742)
1 parent d654ec7 commit e7122a1

13 files changed

Lines changed: 82 additions & 9 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "@fixtures/workspace-tspaths-undeclared",
3+
"private": true,
4+
"workspaces": ["projects/*"]
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "demo",
3+
"private": true
4+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { apple } from '@scope/fruit';
2+
export const x = apple;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"include": ["src/**/*.ts"]
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "@scope/fruit",
3+
"private": true
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const apple = 'apple';
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": "./",
4+
"paths": {
5+
"@scope/fruit": ["projects/lib-fruit/src/index"],
6+
"@scope/fruit/*": ["projects/lib-fruit/src/*"]
7+
}
8+
}
9+
}

packages/knip/src/DependencyDeputy.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export class DependencyDeputy {
4343
isStrict;
4444
isReportDependencies;
4545
_manifests: WorkspaceManifests = new Map();
46+
workspacePkgNames: Set<string> = new Set();
4647
referencedDependencies: Map<string, Set<string>>;
4748
referencedBinaries: Map<string, Set<string>>;
4849
hostDependencies: Map<string, HostDependencies>;
@@ -140,6 +141,10 @@ export class DependencyDeputy {
140141
return this._manifests.get(workspaceName);
141142
}
142143

144+
public setWorkspacePkgNames(pkgNames: Iterable<string>) {
145+
this.workspacePkgNames = new Set(pkgNames);
146+
}
147+
143148
getProductionDependencies(workspaceName: string): DependencyArray {
144149
const manifest = this._manifests.get(workspaceName);
145150
if (!manifest) return [];
@@ -236,6 +241,11 @@ export class DependencyDeputy {
236241

237242
if (this._manifests.get(workspace.name)?.engines[packageName]) return true;
238243

244+
if (!this.isStrict && this.workspacePkgNames.has(packageName)) {
245+
this.addReferencedDependency(workspace.name, packageName);
246+
return true;
247+
}
248+
239249
if (!this.isStrict) {
240250
for (const name of workspaceNames) {
241251
const hosts = this.getHostDependenciesFor(name, packageName);

packages/knip/src/graph/build.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ export async function build({
9898
counselor.addWorkspace(manifest);
9999
}
100100

101+
deputy.setWorkspacePkgNames(chief.availableWorkspacePkgNames);
102+
101103
collector.addIgnorePatterns(chief.config.ignore.map(id => ({ pattern: prependDir(options.cwd, id), id })));
102104
collector.addIgnoreFilesPatterns(chief.config.ignoreFiles.map(id => ({ pattern: prependDir(options.cwd, id), id })));
103105

packages/knip/test/module-resolution-non-std-absolute.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@ test('Resolve non-standard absolute specifiers', async () => {
1111
const options = await createOptions({ cwd });
1212
const { issues, counters } = await main(options);
1313

14-
assert(issues.unlisted['x-self/index.ts']['x-other']);
14+
assert(!issues.unlisted['x-self/index.ts']?.['x-other']);
1515

1616
assert.deepEqual(counters, {
1717
...baseCounters,
18-
unlisted: 1,
1918
processed: 1,
2019
total: 1,
2120
});
2221
});
22+
23+
test('Resolve non-standard absolute specifiers (strict flags sibling workspace)', async () => {
24+
const options = await createOptions({ cwd, isStrict: true, isProduction: false });
25+
const { issues } = await main(options);
26+
27+
assert(issues.unlisted['x-self/index.ts']['x-other']);
28+
});

0 commit comments

Comments
 (0)