88import { isDefinitelyTyped , getDefinitelyTypedFor , getPackageFromDefinitelyTyped } from './util/modules.js' ;
99import type { Workspace } from './ConfigurationChief.js' ;
1010import type { ConfigurationHints , Issue } from './types/issues.js' ;
11- import type { WorkspaceManifests } from './types/workspace.js' ;
12- import type { PeerDependencies , InstalledBinaries } from './types/workspace.js' ;
11+ import type { WorkspaceManifests , HostDependencies , InstalledBinaries } from './types/workspace.js' ;
1312import type { PackageJson } from '@npmcli/package-json' ;
1413
1514type Options = {
@@ -27,7 +26,7 @@ export class DependencyDeputy {
2726 _manifests : WorkspaceManifests = new Map ( ) ;
2827 referencedDependencies : Map < string , Set < string > > ;
2928 referencedBinaries : Map < string , Set < string > > ;
30- peerDependencies : Map < string , PeerDependencies > ;
29+ hostDependencies : Map < string , HostDependencies > ;
3130 installedBinaries : Map < string , InstalledBinaries > ;
3231 ignoreBinaries : string [ ] = [ ] ;
3332 ignoreDependencies : string [ ] = [ ] ;
@@ -36,7 +35,7 @@ export class DependencyDeputy {
3635 this . isStrict = isStrict ;
3736 this . referencedDependencies = new Map ( ) ;
3837 this . referencedBinaries = new Map ( ) ;
39- this . peerDependencies = new Map ( ) ;
38+ this . hostDependencies = new Map ( ) ;
4039 this . installedBinaries = new Map ( ) ;
4140 }
4241
@@ -127,12 +126,12 @@ export class DependencyDeputy {
127126 this . referencedBinaries . get ( workspaceName ) ?. add ( binaryName ) ;
128127 }
129128
130- addPeerDependencies ( workspaceName : string , peerDependencies : Map < string , Set < string > > ) {
131- this . peerDependencies . set ( workspaceName , peerDependencies ) ;
129+ addHostDependencies ( workspaceName : string , hostDependencies : HostDependencies ) {
130+ this . hostDependencies . set ( workspaceName , hostDependencies ) ;
132131 }
133132
134- getPeerDependenciesOf ( workspaceName : string , dependency : string ) {
135- return Array . from ( this . peerDependencies . get ( workspaceName ) ?. get ( dependency ) ?? [ ] ) ;
133+ getHostDependenciesFor ( workspaceName : string , dependency : string ) {
134+ return Array . from ( this . hostDependencies . get ( workspaceName ) ?. get ( dependency ) ?? [ ] ) ;
136135 }
137136
138137 getPeerDependencies ( workspaceName : string ) {
@@ -249,23 +248,25 @@ export class DependencyDeputy {
249248 // Ignore `@types/*` packages that don't have a related dependency (e.g. `@types/node`)
250249 if ( IGNORE_DEFINITELY_TYPED . includes ( typedPackageName ) ) return true ;
251250
252- // Ignore typed dependencies that have a peer dependency that's referenced
253- // Example: `next` has `react-dom` as peer dependency, so when `@types/react-dom` is listed it can be ignored
254- const peerDependencies = this . getPeerDependenciesOf ( workspaceName , typedPackageName ) ;
255- if ( peerDependencies . length ) {
256- return ! ! peerDependencies . find ( peerDependency => isReferencedDependency ( peerDependency , true ) ) ;
257- }
251+ // Ignore typed dependencies that have a host dependency that's referenced
252+ // Example: `next` (host) has `react-dom` and/or `@types/react-dom` (peer), peers can be ignored if host `next` is referenced
253+ const hostDependencies = [
254+ ...this . getHostDependenciesFor ( workspaceName , dependency ) ,
255+ ...this . getHostDependenciesFor ( workspaceName , typedPackageName ) ,
256+ ] ;
257+ if ( hostDependencies . length ) return ! ! hostDependencies . find ( host => isReferencedDependency ( host , true ) ) ;
258258
259259 if ( ! referencedDependencies ) return false ;
260260
261261 return referencedDependencies . has ( typedPackageName ) ;
262262 }
263263
264- // A dependency may not be referenced, but it may be a peerDependency of another.
265- // If that "host" dependency is also not referenced we'll report this dependency as unused.
266- const peerDependenciesOf = this . getPeerDependenciesOf ( workspaceName , dependency ) ;
267- peerDependenciesOf . forEach ( dep => ( ! peerDepRecs [ dep ] ? ( peerDepRecs [ dep ] = 1 ) : peerDepRecs [ dep ] ++ ) ) ;
268- return peerDependenciesOf . some ( peerDependency => isReferencedDependency ( peerDependency , true ) ) ;
264+ // A dependency may not be referenced, but it may be a peer dep of another.
265+ // If that host is also not referenced we'll report this dependency as unused.
266+ const hostDependencies = this . getHostDependenciesFor ( workspaceName , dependency ) ;
267+
268+ hostDependencies . forEach ( dep => ( ! peerDepRecs [ dep ] ? ( peerDepRecs [ dep ] = 1 ) : peerDepRecs [ dep ] ++ ) ) ;
269+ return hostDependencies . some ( peerDependency => isReferencedDependency ( peerDependency , true ) ) ;
269270 } ;
270271
271272 const isNotReferencedDependency = ( dependency : string ) : boolean => ! isReferencedDependency ( dependency ) ;
0 commit comments