Skip to content

Commit

Permalink
fix: ast_utils.FuncSignatureIs panics with types names other than ast…
Browse files Browse the repository at this point in the history
….Ident (#1174)
  • Loading branch information
chavacava authored Dec 7, 2024
1 parent 06192a5 commit 24a9325
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
20 changes: 18 additions & 2 deletions internal/astutils/ast_utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Package astutils provides utility functions for working with AST nodes
package astutils

import "go/ast"
import (
"fmt"
"go/ast"
)

// FuncSignatureIs returns true if the given func decl satisfies a signature characterized
// by the given name, parameters types and return types; false otherwise.
Expand Down Expand Up @@ -46,7 +49,7 @@ func getTypeNames(fields *ast.FieldList) []string {
}

for _, field := range fields.List {
typeName := field.Type.(*ast.Ident).Name
typeName := getFieldTypeName(field.Type)
if field.Names == nil { // unnamed field
result = append(result, typeName)
continue
Expand All @@ -59,3 +62,16 @@ func getTypeNames(fields *ast.FieldList) []string {

return result
}

func getFieldTypeName(typ ast.Expr) string {
switch f := typ.(type) {
case *ast.Ident:
return f.Name
case *ast.SelectorExpr:
return f.Sel.Name + "." + getFieldTypeName(f.X)
case *ast.StarExpr:
return "*" + getFieldTypeName(f.X)
default:
panic(fmt.Sprintf("not supported type %T", typ))
}
}
5 changes: 5 additions & 0 deletions testdata/golint/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ type Vv []int

func (vv Vv) Len() (result int) { return len(w) } // MATCH /exported method Vv.Len should have comment or be unexported/
func (vv Vv) Less(i int, j int) (result bool) { return w[i] < w[j] } // MATCH /exported method Vv.Less should have comment or be unexported/

// X is ...
type X []int

func (x X) Less(i *pkg.tip) (result bool) { return len(x) } // MATCH /exported method X.Less should have comment or be unexported/

0 comments on commit 24a9325

Please sign in to comment.