Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gomod): add line locations #266

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 23 additions & 5 deletions pkg/dart/pub/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pub

import (
"fmt"

"golang.org/x/xerrors"
"gopkg.in/yaml.v3"

Expand All @@ -22,7 +23,12 @@ func NewParser() types.Parser {
}

type lock struct {
Packages map[string]Dep `yaml:"packages"`
Packages map[string]DepWithMetadata `yaml:"packages"`
}

type DepWithMetadata struct {
Dep Dep
Line int
}

type Dep struct {
Expand All @@ -43,10 +49,11 @@ func (Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, er
// It will be confusing if we exclude direct dev dependencies and include transitive dev dependencies.
// We decided to keep all dev dependencies until Pub will add support for "transitive main" and "transitive dev".
lib := types.Library{
ID: pkgID(name, dep.Version),
Name: name,
Version: dep.Version,
Indirect: dep.Dependency == transitiveDep,
ID: pkgID(name, dep.Dep.Version),
Name: name,
Version: dep.Dep.Version,
Indirect: dep.Dep.Dependency == transitiveDep,
Locations: []types.Location{{StartLine: dep.Line, EndLine: dep.Line}},
}
libs = append(libs, lib)
}
Expand All @@ -57,3 +64,14 @@ func (Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, er
func pkgID(name, version string) string {
return fmt.Sprintf(idFormat, name, version)
}

func (dep *DepWithMetadata) UnmarshalYAML(value *yaml.Node) error {
err := value.Decode(&dep.Dep)
if err != nil {
return err
}

dep.Line = value.Line - 1

return nil
}
23 changes: 13 additions & 10 deletions pkg/dart/pub/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,23 @@ func TestParser_Parse(t *testing.T) {
inputFile: "testdata/happy.lock",
want: []types.Library{
{
ID: "[email protected]",
Name: "crypto",
Version: "3.0.2",
ID: "[email protected]",
Name: "crypto",
Version: "3.0.2",
Locations: []types.Location{{StartLine: 4, EndLine: 4}},
},
{
ID: "[email protected]",
Name: "flutter_test",
Version: "0.0.0",
ID: "[email protected]",
Name: "flutter_test",
Version: "0.0.0",
Locations: []types.Location{{StartLine: 11, EndLine: 11}},
},
{
ID: "[email protected]",
Name: "uuid",
Version: "3.0.6",
Indirect: true,
ID: "[email protected]",
Name: "uuid",
Version: "3.0.6",
Indirect: true,
Locations: []types.Location{{StartLine: 16, EndLine: 16}},
},
},
wantErr: assert.NoError,
Expand Down
7 changes: 5 additions & 2 deletions pkg/frameworks/wordpress/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ func Parse(r io.Reader) (lib types.Library, err error) {
var version string
isComment := false
scanner := bufio.NewScanner(r)
var lineNumber int // It is used to save dependency location
for scanner.Scan() {
lineNumber++
line := scanner.Text()

// Remove comment
Expand Down Expand Up @@ -72,7 +74,8 @@ func Parse(r io.Reader) (lib types.Library, err error) {
}

return types.Library{
Name: "wordpress",
Version: version,
Name: "wordpress",
Version: version,
Locations: []types.Location{{StartLine: lineNumber, EndLine: lineNumber}},
}, nil
}
5 changes: 3 additions & 2 deletions pkg/frameworks/wordpress/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ func TestParseWordPress(t *testing.T) {
{
file: "testdata/version.php",
want: types.Library{
Name: "wordpress",
Version: "4.9.4-alpha",
Name: "wordpress",
Version: "4.9.4-alpha",
Locations: []types.Location{{StartLine: 22, EndLine: 22}},
},
},
{
Expand Down
6 changes: 6 additions & 0 deletions pkg/golang/mod/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ func (p *Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency,
Version: require.Mod.Version[1:],
Indirect: require.Indirect,
ExternalReferences: p.GetExternalRefs(require.Mod.Path),
Locations: []types.Location{
{
StartLine: require.Syntax.Start.Line,
EndLine: require.Syntax.End.Line,
},
},
}
}

Expand Down
114 changes: 114 additions & 0 deletions pkg/golang/mod/parse_testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,24 @@ var (
URL: "https://github.com/aquasecurity/go-dep-parser",
},
},
Locations: []types.Location{
{
StartLine: 5,
EndLine: 5,
},
},
},
{
ID: "golang.org/x/[email protected]",
Name: "golang.org/x/xerrors",
Version: "0.0.0-20200804184101-5ec99f83aff1",
Indirect: true,
Locations: []types.Location{
{
StartLine: 8,
EndLine: 8,
},
},
},
{
ID: "gopkg.in/[email protected]",
Expand All @@ -34,6 +46,12 @@ var (
URL: "https://github.com/go-yaml/yaml",
},
},
Locations: []types.Location{
{
StartLine: 9,
EndLine: 9,
},
},
},
}

Expand All @@ -56,6 +74,12 @@ var (
Name: "golang.org/x/xerrors",
Version: "0.0.0-20200804184101-5ec99f83aff1",
Indirect: true,
Locations: []types.Location{
{
StartLine: 7,
EndLine: 7,
},
},
},
}

Expand All @@ -72,12 +96,24 @@ var (
URL: "https://github.com/aquasecurity/go-dep-parser",
},
},
Locations: []types.Location{
{
StartLine: 5,
EndLine: 5,
},
},
},
{
ID: "golang.org/x/[email protected]",
Name: "golang.org/x/xerrors",
Version: "0.0.0-20200804184101-5ec99f83aff1",
Indirect: true,
Locations: []types.Location{
{
StartLine: 7,
EndLine: 7,
},
},
},
}

Expand All @@ -100,6 +136,12 @@ var (
Name: "golang.org/x/xerrors",
Version: "0.0.0-20200804184101-5ec99f83aff1",
Indirect: true,
Locations: []types.Location{
{
StartLine: 7,
EndLine: 7,
},
},
},
}

Expand All @@ -116,12 +158,24 @@ var (
URL: "https://github.com/aquasecurity/go-dep-parser",
},
},
Locations: []types.Location{
{
StartLine: 5,
EndLine: 5,
},
},
},
{
ID: "golang.org/x/[email protected]",
Name: "golang.org/x/xerrors",
Version: "0.0.0-20200804184101-5ec99f83aff1",
Indirect: true,
Locations: []types.Location{
{
StartLine: 8,
EndLine: 8,
},
},
},
{
ID: "gopkg.in/[email protected]",
Expand All @@ -134,6 +188,12 @@ var (
URL: "https://github.com/go-yaml/yaml",
},
},
Locations: []types.Location{
{
StartLine: 9,
EndLine: 9,
},
},
},
}

Expand All @@ -150,6 +210,12 @@ var (
URL: "https://github.com/aquasecurity/go-dep-parser",
},
},
Locations: []types.Location{
{
StartLine: 5,
EndLine: 5,
},
},
},
{
ID: "gopkg.in/[email protected]",
Expand All @@ -162,6 +228,12 @@ var (
URL: "https://github.com/go-yaml/yaml",
},
},
Locations: []types.Location{
{
StartLine: 9,
EndLine: 9,
},
},
},
}

Expand All @@ -178,6 +250,12 @@ var (
URL: "https://github.com/aquasecurity/go-dep-parser",
},
},
Locations: []types.Location{
{
StartLine: 5,
EndLine: 5,
},
},
},
{
ID: "gopkg.in/[email protected]",
Expand All @@ -190,6 +268,12 @@ var (
URL: "https://github.com/go-yaml/yaml",
},
},
Locations: []types.Location{
{
StartLine: 9,
EndLine: 9,
},
},
},
}

Expand All @@ -206,12 +290,24 @@ var (
URL: "https://github.com/aquasecurity/go-dep-parser",
},
},
Locations: []types.Location{
{
StartLine: 5,
EndLine: 5,
},
},
},
{
ID: "golang.org/x/[email protected]",
Name: "golang.org/x/xerrors",
Version: "0.0.0-20200804184101-5ec99f83aff1",
Indirect: true,
Locations: []types.Location{
{
StartLine: 8,
EndLine: 8,
},
},
},
{
ID: "gopkg.in/[email protected]",
Expand All @@ -224,6 +320,12 @@ var (
URL: "https://github.com/go-yaml/yaml",
},
},
Locations: []types.Location{
{
StartLine: 9,
EndLine: 9,
},
},
},
}

Expand All @@ -240,6 +342,12 @@ var (
URL: "https://github.com/aquasecurity/go-dep-parser",
},
},
Locations: []types.Location{
{
StartLine: 5,
EndLine: 5,
},
},
},
}

Expand All @@ -256,6 +364,12 @@ var (
URL: "https://github.com/aquasecurity/go-dep-parser",
},
},
Locations: []types.Location{
{
StartLine: 3,
EndLine: 3,
},
},
},
}
)
Loading