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

Support Go 1.18 methods with type parameters #91

Merged
merged 1 commit into from
Apr 26, 2022
Merged
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
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
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=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
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=
37 changes: 25 additions & 12 deletions gocov/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
Expand Down
40 changes: 40 additions & 0 deletions gocov/convert_test.go
Original file line number Diff line number Diff line change
@@ -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))

}