1
0
mirror of https://github.com/mgechev/revive.git synced 2025-07-05 00:28:53 +02:00

fix: ast_utils.FuncSignatureIs panics with types names other than ast.Ident (#1174)

This commit is contained in:
chavacava
2024-12-07 15:40:39 +01:00
committed by GitHub
parent 06192a5599
commit 24a9325b5a
2 changed files with 23 additions and 2 deletions

View File

@ -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.
@ -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
@ -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))
}
}