Skip to content

Commit

Permalink
Deal with packages that confusingly include package.json#types but …
Browse files Browse the repository at this point in the history
…also recommend to install DT pkg
  • Loading branch information
webpro committed Sep 22, 2024
1 parent c333a90 commit dd10163
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
8 changes: 4 additions & 4 deletions packages/knip/src/DependencyDeputy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,12 @@ export class DependencyDeputy {

const [scope, typedDependency] = dependency.split('/');
if (scope === DT_SCOPE) {
// The `pkg` dependency already has types included, i.e. this `@types/pkg` is obsolete
if (hasTypesIncluded?.has(typedDependency)) return false;

const typedPackageName = getPackageFromDefinitelyTyped(typedDependency);
// Ignore `@types/*` packages that don't have a related dependency (e.g. `@types/node`)
if (IGNORE_DEFINITELY_TYPED.includes(typedPackageName)) return true;
if (IGNORE_DEFINITELY_TYPED.has(typedPackageName)) return true;

// The `pkg` dependency already has types included, i.e. this `@types/pkg` is obsolete
if (hasTypesIncluded?.has(typedDependency)) return false;

// Ignore typed dependencies that have a host dependency that's referenced
// Example: `next` (host) has `react-dom` and/or `@types/react-dom` (peer), peers can be ignored if host `next` is referenced
Expand Down
9 changes: 7 additions & 2 deletions packages/knip/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,13 @@ export const FOREIGN_FILE_EXTENSIONS = new Set([
'.yml',
]);

// The `@types/node` dependency does not require the `node` dependency
export const IGNORE_DEFINITELY_TYPED = ['node', 'bun'];
export const IGNORE_DEFINITELY_TYPED = new Set([
// The `@types/node` dependency does not require the `node` dependency
'node',
'bun',
// Packages that confusingly include `package.json#types` but also recommend to install DT pkg
'jest',
]);

export const ISSUE_TYPES: IssueType[] = [
'files',
Expand Down

0 comments on commit dd10163

Please sign in to comment.