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

Fix gocognit output when a function has generic receiver type #10

Merged
merged 2 commits into from Jul 1, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 0 additions & 12 deletions gocognit.go
Expand Up @@ -49,18 +49,6 @@ func funcName(fn *ast.FuncDecl) string {
return fn.Name.Name
}

// recvString returns a string representation of recv of the
// form "T", "*T", or "BADRECV" (if not a proper receiver type).
func recvString(recv ast.Expr) string {
switch t := recv.(type) {
case *ast.Ident:
return t.Name
case *ast.StarExpr:
return "*" + recvString(t.X)
}
return "BADRECV"
}

// Complexity calculates the cognitive complexity of a function.
func Complexity(fn *ast.FuncDecl) int {
v := complexityVisitor{
Expand Down
30 changes: 30 additions & 0 deletions recv.go
@@ -0,0 +1,30 @@
//go:build go1.18
// +build go1.18

package gocognit

import (
"go/ast"
"strings"
)

// recvString returns a string representation of recv of the
// form "T", "*T", Type[T], Type[T, V], or "BADRECV" (if not a proper receiver type).
func recvString(recv ast.Expr) string {
switch t := recv.(type) {
case *ast.Ident:
return t.Name
case *ast.StarExpr:
return "*" + recvString(t.X)
case *ast.IndexExpr:
return recvString(t.X) + "[" + recvString(t.Index) + "]"
case *ast.IndexListExpr:
targs := make([]string, len(t.Indices))
for i, exp := range t.Indices {
targs[i] = recvString(exp)
}

return recvString(t.X) + "[" + strings.Join(targs, ", ") + "]"
}
return "BADRECV"
}
20 changes: 20 additions & 0 deletions recv_pre118.go
@@ -0,0 +1,20 @@
//go:build !go1.18
// +build !go1.18

package gocognit

import (
"go/ast"
)

// recvString returns a string representation of recv of the
// form "T", "*T", or "BADRECV" (if not a proper receiver type).
func recvString(recv ast.Expr) string {
switch t := recv.(type) {
case *ast.Ident:
return t.Name
case *ast.StarExpr:
return "*" + recvString(t.X)
}
return "BADRECV"
}