Skip to content

Commit ef94f5b

Browse files
committed
Fix typed peer dep + improve some peer/host namings (fixes #239)
1 parent c8b47f1 commit ef94f5b

6 files changed

Lines changed: 38 additions & 37 deletions

File tree

‎src/DependencyDeputy.ts‎

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import {
88
import { isDefinitelyTyped, getDefinitelyTypedFor, getPackageFromDefinitelyTyped } from './util/modules.js';
99
import type { Workspace } from './ConfigurationChief.js';
1010
import 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';
1312
import type { PackageJson } from '@npmcli/package-json';
1413

1514
type 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);

‎src/WorkspaceWorker.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { getKeysByValue } from './util/object.js';
77
import { join, toPosix } from './util/path.js';
88
import type { Configuration, PluginConfiguration, PluginName, WorkspaceConfiguration } from './types/config.js';
99
import type { PackageJsonWithPlugins } from './types/plugins.js';
10-
import type { InstalledBinaries, PeerDependencies } from './types/workspace.js';
10+
import type { InstalledBinaries, HostDependencies } from './types/workspace.js';
1111
import type { Entries } from 'type-fest';
1212

1313
type PluginNames = Entries<typeof plugins>;
@@ -51,7 +51,7 @@ export class WorkspaceWorker {
5151
enabled: Record<PluginName, boolean>;
5252
enabledPlugins: PluginName[] = [];
5353
referencedDependencies: ReferencedDependencies = new Set();
54-
peerDependencies: PeerDependencies = new Map();
54+
hostDependencies: HostDependencies = new Map();
5555
installedBinaries: InstalledBinaries = new Map();
5656

5757
constructor({
@@ -115,7 +115,7 @@ export class WorkspaceWorker {
115115
}
116116

117117
private async initReferencedDependencies() {
118-
const { dependencies, peerDependencies, installedBinaries } = await npm.findDependencies({
118+
const { dependencies, hostDependencies, installedBinaries } = await npm.findDependencies({
119119
manifest: this.manifest,
120120
isProduction: this.isProduction,
121121
isStrict: this.isStrict,
@@ -125,7 +125,7 @@ export class WorkspaceWorker {
125125

126126
const filePath = join(this.dir, 'package.json');
127127
dependencies.forEach(dependency => this.referencedDependencies.add([filePath, dependency]));
128-
this.peerDependencies = peerDependencies;
128+
this.hostDependencies = hostDependencies;
129129
this.installedBinaries = installedBinaries;
130130
}
131131

@@ -313,7 +313,7 @@ export class WorkspaceWorker {
313313
await this.findDependenciesByPlugins();
314314

315315
return {
316-
peerDependencies: this.peerDependencies,
316+
hostDependencies: this.hostDependencies,
317317
installedBinaries: this.installedBinaries,
318318
referencedDependencies: this.referencedDependencies,
319319
enabledPlugins: this.enabledPlugins,

‎src/index.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,9 @@ export const main = async (unresolvedConfiguration: CommandLineOptions) => {
230230
// Get peerDependencies, installed binaries, entry files gathered through all plugins, and hand over
231231
// A bit of an entangled hotchpotch, but it's all related, and efficient in terms of reading package.json once, etc.
232232
const dependencies = await worker.findAllDependencies();
233-
const { referencedDependencies, peerDependencies, installedBinaries, enabledPlugins } = dependencies;
233+
const { referencedDependencies, hostDependencies, installedBinaries, enabledPlugins } = dependencies;
234234

235-
deputy.addPeerDependencies(name, peerDependencies);
235+
deputy.addHostDependencies(name, hostDependencies);
236236
deputy.setInstalledBinaries(name, installedBinaries);
237237
enabledPluginsStore.set(name, enabledPlugins);
238238

‎src/manifest/index.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { _getDependenciesFromScripts } from '../binaries/index.js';
22
import { timerify } from '../util/Performance.js';
33
import { getPackageManifest } from './helpers.js';
4-
import type { InstalledBinaries, PeerDependencies } from '../types/workspace.js';
4+
import type { InstalledBinaries, HostDependencies } from '../types/workspace.js';
55
import type { PackageJson } from '@npmcli/package-json';
66

77
type Options = {
@@ -14,7 +14,7 @@ type Options = {
1414

1515
const findManifestDependencies = async ({ manifest, isProduction, isStrict, dir, cwd }: Options) => {
1616
const scriptFilter = isProduction ? ['start', 'postinstall'] : [];
17-
const peerDependencies: PeerDependencies = new Map();
17+
const hostDependencies: HostDependencies = new Map();
1818

1919
const scripts = Object.entries(manifest.scripts ?? {}).reduce((scripts, [scriptName, script]) => {
2020
if (script && (scriptFilter.length === 0 || scriptFilter.includes(scriptName))) {
@@ -56,18 +56,18 @@ const findManifestDependencies = async ({ manifest, isProduction, isStrict, dir,
5656
// Read and store peer dependencies
5757
const packagePeerDependencies = Object.keys(manifest.peerDependencies ?? {});
5858
packagePeerDependencies.forEach(packagePeerDependency => {
59-
if (peerDependencies.has(packagePeerDependency)) {
60-
peerDependencies.get(packagePeerDependency)?.add(packageName);
59+
if (hostDependencies.has(packagePeerDependency)) {
60+
hostDependencies.get(packagePeerDependency)?.add(packageName);
6161
} else {
62-
peerDependencies.set(packagePeerDependency, new Set([packageName]));
62+
hostDependencies.set(packagePeerDependency, new Set([packageName]));
6363
}
6464
});
6565
}
6666
}
6767

6868
return {
6969
dependencies,
70-
peerDependencies,
70+
hostDependencies,
7171
installedBinaries,
7272
};
7373
};

‎src/types/workspace.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ type WorkspaceManifest = {
1414

1515
export type WorkspaceManifests = Map<string, WorkspaceManifest>;
1616

17-
export type PeerDependencies = Map<string, Set<string>>;
17+
export type HostDependencies = Map<string, Set<string>>;
1818

1919
export type InstalledBinaries = Map<string, Set<string>>;

‎tests/npm-scripts.test.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ test('Referenced dependencies in npm scripts', async () => {
1919
cwd,
2020
};
2121

22-
const { dependencies, peerDependencies, installedBinaries } = await npm.findDependencies(config);
22+
const { dependencies, hostDependencies, installedBinaries } = await npm.findDependencies(config);
2323

2424
assert.deepEqual(dependencies, [
2525
'bin:nodemon',
@@ -37,10 +37,10 @@ test('Referenced dependencies in npm scripts', async () => {
3737
'bin:runnable',
3838
]);
3939

40-
const expectedPeerDependencies = new Map();
41-
expectedPeerDependencies.set('pm2-peer-dep', new Set(['pm2']));
40+
const expectedHostDependencies = new Map();
41+
expectedHostDependencies.set('pm2-peer-dep', new Set(['pm2']));
4242

43-
assert.deepEqual(peerDependencies, expectedPeerDependencies);
43+
assert.deepEqual(hostDependencies, expectedHostDependencies);
4444

4545
assert.deepEqual(
4646
installedBinaries,

0 commit comments

Comments
 (0)