Skip to content

Commit fc95526

Browse files
committedFeb 12, 2025·
Fix unused @types DT dependencies (resolve #942)
1 parent 41a2bcc commit fc95526

File tree

10 files changed

+104
-4
lines changed

10 files changed

+104
-4
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { meta } from '@eslint/js';
2+
import { Ajv } from 'ajv';
3+
4+
console.log(meta);
5+
console.log(Ajv);

‎packages/knip/fixtures/dependencies-types/node_modules/@eslint/js/package.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/knip/fixtures/dependencies-types/node_modules/@types/ajv/package.json

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/knip/fixtures/dependencies-types/node_modules/@types/eslint/package.json

+24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/knip/fixtures/dependencies-types/node_modules/@types/eslint__js/package.json

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/knip/fixtures/dependencies-types/node_modules/ajv/package.json

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "@fixtures/dependencies-types",
3+
"dependencies": {
4+
"@eslint/js": "^9.20.0",
5+
"ajv": "^8.17.1"
6+
},
7+
"devDependencies": {
8+
"@types/ajv": "^0.0.5",
9+
"@types/eslint__js": "^8.42.3",
10+
"@types/node": "^20.14.8"
11+
}
12+
}

‎packages/knip/src/DependencyDeputy.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export class DependencyDeputy {
162162
}
163163

164164
getHasTypesIncluded(workspaceName: string) {
165-
return this.installedBinaries.get(workspaceName);
165+
return this.hasTypesIncluded.get(workspaceName);
166166
}
167167

168168
addReferencedDependency(workspaceName: string, packageName: string) {
@@ -214,7 +214,8 @@ export class DependencyDeputy {
214214

215215
if (closestWorkspaceName || closestWorkspaceNameForTypes) {
216216
if (closestWorkspaceName) this.addReferencedDependency(closestWorkspaceName, packageName);
217-
if (closestWorkspaceNameForTypes) this.addReferencedDependency(closestWorkspaceNameForTypes, typesPackageName);
217+
if (closestWorkspaceNameForTypes && !this.hasTypesIncluded.get(closestWorkspaceNameForTypes)?.has(packageName))
218+
this.addReferencedDependency(closestWorkspaceNameForTypes, typesPackageName);
218219
return true;
219220
}
220221
this.addReferencedDependency(workspace.name, packageName);
@@ -277,7 +278,7 @@ export class DependencyDeputy {
277278
if (IGNORE_DEFINITELY_TYPED.has(typedPackageName)) return true;
278279

279280
// The `pkg` dependency already has types included, i.e. this `@types/pkg` is obsolete
280-
if (hasTypesIncluded?.has(typedDependency)) return false;
281+
if (hasTypesIncluded?.has(typedPackageName)) return false;
281282

282283
// Ignore typed dependencies that have a host dependency that's referenced
283284
// Example: `next` (host) has `react-dom` and/or `@types/react-dom` (peer), peers can be ignored if host `next` is referenced
@@ -287,7 +288,7 @@ export class DependencyDeputy {
287288
];
288289
if (hostDependencies.length) return !!hostDependencies.find(host => isReferencedDependency(host.name, true));
289290

290-
if (!referencedDependencies) return false;
291+
if (!referencedDependencies?.has(dependency)) return false;
291292

292293
return referencedDependencies.has(typedPackageName);
293294
}

‎packages/knip/test/definitely-typed.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ test('Find unused DT @types', async () => {
1414
});
1515

1616
assert(issues.devDependencies['package.json']['@types/unused']);
17+
assert(issues.devDependencies['package.json']['@types/mocha']);
1718

1819
assert.deepEqual(counters, {
1920
...baseCounters,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { test } from 'bun:test';
2+
import assert from 'node:assert/strict';
3+
import { main } from '../src/index.js';
4+
import { resolve } from '../src/util/path.js';
5+
import baseArguments from './helpers/baseArguments.js';
6+
import baseCounters from './helpers/baseCounters.js';
7+
8+
const cwd = resolve('fixtures/dependencies-types');
9+
10+
test('Find unused @types dependencies', async () => {
11+
const { issues, counters } = await main({
12+
...baseArguments,
13+
cwd,
14+
});
15+
16+
assert(issues.devDependencies['package.json']['@types/ajv']);
17+
assert(issues.devDependencies['package.json']['@types/eslint__js']);
18+
19+
assert.deepEqual(counters, {
20+
...baseCounters,
21+
devDependencies: 2,
22+
processed: 1,
23+
total: 1,
24+
});
25+
});

0 commit comments

Comments
 (0)
Please sign in to comment.