Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/dependency/parser/nodejs/npm/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@ func (p *Parser) pkgNameFromPath(pkgPath string) string {
// node_modules/function1
// node_modules/nested_func/node_modules/debug
if index := strings.LastIndex(pkgPath, nodeModulesDir); index != -1 {
if index+len(nodeModulesDir) == len(pkgPath) {
return ""
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to add a warning here since the package-lock.json file is invalid in this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you could add warning message to warn the lock file is malformed

return pkgPath[index+len(nodeModulesDir)+1:]
}
p.logger.Warn("Package path doesn't have `node_modules` prefix", log.String("pkg_path", pkgPath))
Expand Down
36 changes: 36 additions & 0 deletions pkg/dependency/parser/nodejs/npm/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,39 @@ func TestParse(t *testing.T) {
})
}
}

func TestPkgNameFromPath(t *testing.T) {
tests := []struct {
path string
expected string
}{
{
path: "node_modules/package-name",
expected: "package-name",
},
{
path: "node_modules/package-name/sub-package",
expected: "package-name/sub-package",
},
{
path: "node_modules/package-name/node_modules/sub-sub-package",
expected: "sub-sub-package",
},
{
path: "node_modules",
expected: "",
},
{
path: "node_modules/",
expected: "",
},
}

parser := &Parser{}
for _, test := range tests {
t.Run(test.path, func(t *testing.T) {
path := parser.pkgNameFromPath(test.path)
assert.Equal(t, path, test.expected)
})
}
}