From e6ea120e4bdf8813c95a91c87ab525bf1b5ec397 Mon Sep 17 00:00:00 2001 From: Jeroen Demeyer Date: Wed, 16 Mar 2022 13:32:30 +0100 Subject: [PATCH] Support Go 1.18 methods with type parameters --- go.mod | 5 ++++- go.sum | 11 +++++++++++ gocov/convert.go | 37 +++++++++++++++++++++++++------------ gocov/convert_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 13 deletions(-) create mode 100644 gocov/convert_test.go diff --git a/go.mod b/go.mod index 1b4daf3..bd98ca7 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/axw/gocov go 1.12 -require golang.org/x/tools v0.0.0-20190617190820-da514acc4774 +require ( + github.com/stretchr/testify v1.7.1 + golang.org/x/tools v0.0.0-20190617190820-da514acc4774 +) diff --git a/go.sum b/go.sum index 967f84c..43c5cf6 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,10 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -5,3 +12,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/tools v0.0.0-20190617190820-da514acc4774 h1:CQVOmarCBFzTx0kbOU0ru54Cvot8SdSrNYjZPhQl+gk= golang.org/x/tools v0.0.0-20190617190820-da514acc4774/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/gocov/convert.go b/gocov/convert.go index a5c2271..60955e9 100644 --- a/gocov/convert.go +++ b/gocov/convert.go @@ -189,6 +189,30 @@ type FuncVisitor struct { funcs []*FuncExtent } +func functionName(f *ast.FuncDecl) string { + name := f.Name.Name + if f.Recv == nil { + return name + } else { + // Function name is prepended with "T." if there is a receiver, where + // T is the type of the receiver, dereferenced if it is a pointer. + return exprName(f.Recv.List[0].Type) + "." + name + } +} + +func exprName(x ast.Expr) string { + switch y := x.(type) { + case *ast.StarExpr: + return exprName(y.X) + case *ast.IndexExpr: + return fmt.Sprintf("%s[%s]", exprName(y.X), exprName(y.Index)) + case *ast.Ident: + return y.Name + default: + return "" + } +} + // Visit implements the ast.Visitor interface. func (v *FuncVisitor) Visit(node ast.Node) ast.Visitor { var body *ast.BlockStmt @@ -198,18 +222,7 @@ func (v *FuncVisitor) Visit(node ast.Node) ast.Visitor { body = n.Body case *ast.FuncDecl: body = n.Body - name = n.Name.Name - // Function name is prepended with "T." if there is a receiver, where - // T is the type of the receiver, dereferenced if it is a pointer. - if n.Recv != nil { - field := n.Recv.List[0] - switch recv := field.Type.(type) { - case *ast.StarExpr: - name = recv.X.(*ast.Ident).Name + "." + name - case *ast.Ident: - name = recv.Name + "." + name - } - } + name = functionName(n) } if body != nil { start := v.fset.Position(node.Pos()) diff --git a/gocov/convert_test.go b/gocov/convert_test.go new file mode 100644 index 0000000..0a96a4b --- /dev/null +++ b/gocov/convert_test.go @@ -0,0 +1,40 @@ +package main + +import ( + "go/ast" + "go/parser" + "go/token" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestExprName(t *testing.T) { + source := ` +package foo + +func Function() {} +func (x Foo) Method() {} +func (x *Foo) PtrMethod() {} +func (x *Foo[T]) GenericMethod() {} +` + + fset := token.NewFileSet() + parsed, err := parser.ParseFile(fset, "", source, 0) + if err != nil { + t.Fatal(err) + } + + function0 := parsed.Decls[0].(*ast.FuncDecl) + assert.Equal(t, "Function", functionName(function0)) + + function1 := parsed.Decls[1].(*ast.FuncDecl) + assert.Equal(t, "Foo.Method", functionName(function1)) + + function2 := parsed.Decls[2].(*ast.FuncDecl) + assert.Equal(t, "Foo.PtrMethod", functionName(function2)) + + function3 := parsed.Decls[3].(*ast.FuncDecl) + assert.Equal(t, "Foo[T].GenericMethod", functionName(function3)) + +}