Skip to content

Commit

Permalink
Simplify recvString output
Browse files Browse the repository at this point in the history
Refactor recvString to return only type's name without it's type
parameters when the type is generic.

In this way type's string representation will be consistent with
function's string representation(gocognit print only function's name
even if it's generic one).
  • Loading branch information
ivanruski committed Jul 3, 2022
1 parent 150dee4 commit 87c0fdc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
12 changes: 3 additions & 9 deletions recv.go
Expand Up @@ -5,26 +5,20 @@ 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).
// 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)
case *ast.IndexExpr:
return recvString(t.X) + "[" + recvString(t.Index) + "]"
return recvString(t.X)
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 recvString(t.X)
}
return "BADRECV"
}
6 changes: 3 additions & 3 deletions testdata/src/c/c.go
Expand Up @@ -3,7 +3,7 @@ package testdata
type Node[T any] struct {
}

func (n *Node[T]) String() string { // want "cognitive complexity 1 of func \\(\\*Node\\[T\\]\\)\\.String is high \\(> 0\\)"
func (n *Node[T]) String() string { // want "cognitive complexity 1 of func \\(\\*Node\\)\\.String is high \\(> 0\\)"
if n != nil { // +1
return "Node"
}
Expand All @@ -16,7 +16,7 @@ type Pair[K any, V any] struct {
Value V
}

func (p *Pair[K, V]) String() string { // want "cognitive complexity 1 of func \\(\\*Pair\\[K, V\\]\\)\\.String is high \\(> 0\\)"
func (p *Pair[K, V]) String() string { // want "cognitive complexity 1 of func \\(\\*Pair\\)\\.String is high \\(> 0\\)"
if p != nil { // +1
return "Pair"
}
Expand All @@ -27,7 +27,7 @@ func (p *Pair[K, V]) String() string { // want "cognitive complexity 1 of func \
type Triple[K any, V any, T any] struct {
}

func (t *Triple[K, V, T]) String() string { // want "cognitive complexity 1 of func \\(\\*Triple\\[K, V, T\\]\\)\\.String is high \\(> 0\\)"
func (t *Triple[K, V, T]) String() string { // want "cognitive complexity 1 of func \\(\\*Triple\\)\\.String is high \\(> 0\\)"
if t != nil { // +1 `
return "Triple"
}
Expand Down

0 comments on commit 87c0fdc

Please sign in to comment.