Skip to content

Commit 7389961

Browse files
feat(nodejs): respect peer dependencies for dependency tree (#7989)
Signed-off-by: knqyf263 <[email protected]> Co-authored-by: DmitriyLewen <[email protected]>
1 parent 1feb81c commit 7389961

File tree

4 files changed

+199
-2
lines changed

4 files changed

+199
-2
lines changed

pkg/dependency/parser/nodejs/npm/parse.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type Package struct {
4343
Dependencies map[string]string `json:"dependencies"`
4444
OptionalDependencies map[string]string `json:"optionalDependencies"`
4545
DevDependencies map[string]string `json:"devDependencies"`
46+
PeerDependencies map[string]string `json:"peerDependencies"`
4647
Resolved string `json:"resolved"`
4748
Dev bool `json:"dev"`
4849
Link bool `json:"link"`
@@ -91,7 +92,7 @@ func (p *Parser) parseV2(packages map[string]Package) ([]ftypes.Package, []ftype
9192
p.resolveLinks(packages)
9293

9394
directDeps := make(map[string]struct{})
94-
for name, version := range lo.Assign(packages[""].Dependencies, packages[""].OptionalDependencies, packages[""].DevDependencies) {
95+
for name, version := range lo.Assign(packages[""].Dependencies, packages[""].OptionalDependencies, packages[""].DevDependencies, packages[""].PeerDependencies) {
9596
pkgPath := joinPaths(nodeModulesDir, name)
9697
if _, ok := packages[pkgPath]; !ok {
9798
p.logger.Debug("Unable to find the direct dependency",
@@ -165,7 +166,7 @@ func (p *Parser) parseV2(packages map[string]Package) ([]ftypes.Package, []ftype
165166
// └─┬ [email protected]
166167
// ├─┬ [email protected] - optional dependency
167168
// │ └── [email protected].
168-
dependencies := lo.Assign(pkg.Dependencies, pkg.OptionalDependencies)
169+
dependencies := lo.Assign(pkg.Dependencies, pkg.OptionalDependencies, pkg.PeerDependencies)
169170
dependsOn := make([]string, 0, len(dependencies))
170171
for depName, depVersion := range dependencies {
171172
depID, err := findDependsOn(pkgPath, depName, packages)

pkg/dependency/parser/nodejs/npm/parse_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ func TestParse(t *testing.T) {
4141
want: npmV3WithWorkspacePkgs,
4242
wantDeps: npmV3WithWorkspaceDeps,
4343
},
44+
{
45+
name: "lock version v3 with peer dependencies",
46+
file: "testdata/package-lock_v3_with_peer.json",
47+
want: npmV3WithPeerDependenciesPkgs,
48+
wantDeps: npmV3WithPeerDependenciesDeps,
49+
},
4450
{
4551
name: "lock file v3 contains same dev and non-dev dependencies",
4652
file: "testdata/package-lock_v3_with-same-dev-and-non-dev.json",

pkg/dependency/parser/nodejs/npm/parse_testcase.go

+114
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,120 @@ var (
14421442
DependsOn: []string{"[email protected]"},
14431443
},
14441444
}
1445+
// docker run --name node --rm -it node@sha256:51dd437f31812df71108b81385e2945071ec813d5815fa3403855669c8f3432b sh
1446+
// mkdir node_v3_with_peer && cd node_v3_with_peer
1447+
// npm init --force
1448+
// npm install --save [email protected]
1449+
// npm install --save-peer [email protected]
1450+
// npm update
1451+
//
1452+
// Delete unnecessary packages from package-lock.json
1453+
// Packages are filled manually
1454+
npmV3WithPeerDependenciesPkgs = []ftypes.Package{
1455+
{
1456+
1457+
Name: "lodash",
1458+
Version: "4.17.21",
1459+
Relationship: ftypes.RelationshipDirect,
1460+
ExternalReferences: []ftypes.ExternalRef{
1461+
{
1462+
Type: ftypes.RefOther,
1463+
URL: "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
1464+
},
1465+
},
1466+
Locations: []ftypes.Location{
1467+
{
1468+
StartLine: 30,
1469+
EndLine: 36,
1470+
},
1471+
},
1472+
},
1473+
{
1474+
1475+
Name: "winston-mail",
1476+
Version: "2.0.0",
1477+
Relationship: ftypes.RelationshipDirect,
1478+
ExternalReferences: []ftypes.ExternalRef{
1479+
{
1480+
Type: ftypes.RefOther,
1481+
URL: "https://registry.npmjs.org/winston-mail/-/winston-mail-2.0.0.tgz",
1482+
},
1483+
},
1484+
Locations: []ftypes.Location{
1485+
{
1486+
StartLine: 60,
1487+
EndLine: 74,
1488+
},
1489+
},
1490+
},
1491+
{
1492+
1493+
Name: "mustache",
1494+
Version: "2.3.2",
1495+
Relationship: ftypes.RelationshipIndirect,
1496+
ExternalReferences: []ftypes.ExternalRef{
1497+
{
1498+
Type: ftypes.RefOther,
1499+
URL: "https://registry.npmjs.org/mustache/-/mustache-2.3.2.tgz",
1500+
},
1501+
},
1502+
Locations: []ftypes.Location{
1503+
{
1504+
StartLine: 18,
1505+
EndLine: 29,
1506+
},
1507+
},
1508+
},
1509+
{
1510+
1511+
Name: "triple-beam",
1512+
Version: "1.4.1",
1513+
Relationship: ftypes.RelationshipIndirect,
1514+
ExternalReferences: []ftypes.ExternalRef{
1515+
{
1516+
Type: ftypes.RefOther,
1517+
URL: "https://registry.npmjs.org/triple-beam/-/triple-beam-1.4.1.tgz",
1518+
},
1519+
},
1520+
Locations: []ftypes.Location{
1521+
{
1522+
StartLine: 37,
1523+
EndLine: 46,
1524+
},
1525+
},
1526+
},
1527+
{
1528+
1529+
Name: "winston",
1530+
Version: "3.17.0",
1531+
Relationship: ftypes.RelationshipIndirect,
1532+
ExternalReferences: []ftypes.ExternalRef{
1533+
{
1534+
Type: ftypes.RefOther,
1535+
URL: "https://registry.npmjs.org/winston/-/winston-3.17.0.tgz",
1536+
},
1537+
},
1538+
Locations: []ftypes.Location{
1539+
{
1540+
StartLine: 47,
1541+
EndLine: 59,
1542+
},
1543+
},
1544+
},
1545+
}
1546+
npmV3WithPeerDependenciesDeps = []ftypes.Dependency{
1547+
{
1548+
1549+
DependsOn: []string{
1550+
1551+
1552+
},
1553+
},
1554+
{
1555+
1556+
DependsOn: []string{"[email protected]"},
1557+
},
1558+
}
14451559

14461560
// docker run --name node --rm -it node@sha256:51dd437f31812df71108b81385e2945071ec813d5815fa3403855669c8f3432b sh
14471561
// mkdir node_v3_without_direct_deps && cd node_v3_without_direct_deps
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"name": "node_v3_with_peer",
3+
"version": "1.0.0",
4+
"lockfileVersion": 3,
5+
"requires": true,
6+
"packages": {
7+
"": {
8+
"name": "node_v3_with_peer",
9+
"version": "1.0.0",
10+
"license": "ISC",
11+
"dependencies": {
12+
"winston-mail": "^2.0.0"
13+
},
14+
"peerDependencies": {
15+
"lodash": "^4.17.21"
16+
}
17+
},
18+
"node_modules/mustache": {
19+
"version": "2.3.2",
20+
"resolved": "https://registry.npmjs.org/mustache/-/mustache-2.3.2.tgz",
21+
"integrity": "sha512-KpMNwdQsYz3O/SBS1qJ/o3sqUJ5wSb8gb0pul8CO0S56b9Y2ALm8zCfsjPXsqGFfoNBkDwZuZIAjhsZI03gYVQ==",
22+
"license": "MIT",
23+
"bin": {
24+
"mustache": "bin/mustache"
25+
},
26+
"engines": {
27+
"npm": ">=1.4.0"
28+
}
29+
},
30+
"node_modules/lodash": {
31+
"version": "4.17.21",
32+
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
33+
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==",
34+
"license": "MIT",
35+
"peer": true
36+
},
37+
"node_modules/triple-beam": {
38+
"version": "1.4.1",
39+
"resolved": "https://registry.npmjs.org/triple-beam/-/triple-beam-1.4.1.tgz",
40+
"integrity": "sha512-aZbgViZrg1QNcG+LULa7nhZpJTZSLm/mXnHXnbAbjmN5aSa0y7V+wvv6+4WaBtpISJzThKy+PIPxc1Nq1EJ9mg==",
41+
"license": "MIT",
42+
"peer": true,
43+
"engines": {
44+
"node": ">= 14.0.0"
45+
}
46+
},
47+
"node_modules/winston": {
48+
"version": "3.17.0",
49+
"resolved": "https://registry.npmjs.org/winston/-/winston-3.17.0.tgz",
50+
"integrity": "sha512-DLiFIXYC5fMPxaRg832S6F5mJYvePtmO5G9v9IgUFPhXm9/GkXarH/TUrBAVzhTCzAj9anE/+GjrgXp/54nOgw==",
51+
"license": "MIT",
52+
"peer": true,
53+
"dependencies": {
54+
"triple-beam": "^1.3.0"
55+
},
56+
"engines": {
57+
"node": ">= 12.0.0"
58+
}
59+
},
60+
"node_modules/winston-mail": {
61+
"version": "2.0.0",
62+
"resolved": "https://registry.npmjs.org/winston-mail/-/winston-mail-2.0.0.tgz",
63+
"integrity": "sha512-Wp+mKiieoV6FAZJNyNMS62Zsf5FBSxe17j0f4fpFYeA+rfW8nEZ2eBGGl7+vq+dr3dEpefV5D+ZI3d9jaqdRfw==",
64+
"license": "MIT",
65+
"dependencies": {
66+
"mustache": "^2.2.1"
67+
},
68+
"engines": {
69+
"node": ">= 0.6.0"
70+
},
71+
"peerDependencies": {
72+
"winston": ">=0.5.0"
73+
}
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)