Skip to content

Commit

Permalink
parser: put func params in Symbol.Fields
Browse files Browse the repository at this point in the history
  • Loading branch information
tbruyelle committed Aug 9, 2024
1 parent 25e5d03 commit a4fe1fc
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
13 changes: 9 additions & 4 deletions internal/gno/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,13 @@ func function(n *ast.FuncDecl, source string) *Symbol {
} else {
kind = "func"
}
if n.Type.Results != nil && len(n.Type.Results.List) == 1 {
// Store retType only if there's only one
retType = n.Type.Results.List[0].Type.(*ast.Ident).Name
}
retType, params := typeFromNode(n.Type, source)
return &Symbol{
Name: n.Name.Name,
Doc: n.Doc.Text(),
Signature: strings.Split(source[n.Pos()-1:n.End()-1], " {")[0],
Kind: kind,
Fields: params,
Recv: recv,
Type: retType,
}
Expand Down Expand Up @@ -311,6 +309,13 @@ func typeFromNode(x ast.Node, source string) (string, []Symbol) {
return typeFromNode(x.X, source)
case *ast.StructType: // inline struct
return "", symbolsFromFieldList(x.Fields, source)
case *ast.FuncType:
var retType string
if x.Results != nil && len(x.Results.List) == 1 {
// Store retType only if there's only one
retType = x.Results.List[0].Type.(*ast.Ident).Name
}
return retType, symbolsFromFieldList(x.Params, source)
case *ast.InterfaceType:
return "", symbolsFromFieldList(x.Methods, source)
}
Expand Down
21 changes: 21 additions & 0 deletions internal/gno/testdata/parsePackages-func.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@ func i() x {
"Name": "f",
"Signature": "func f(x int, y interface{ Foo() int }) bool",
"Kind": "func",
"Fields": [
{
"Name": "x",
"Signature": "x int",
"Kind": "field",
"Type": "int"
},
{
"Name": "y",
"Signature": "y interface{ Foo() int }",
"Kind": "field",
"Fields": [
{
"Name": "Foo",
"Signature": "Foo() int",
"Kind": "method",
"Type": "int"
}
]
}
],
"Type": "bool"
},
{
Expand Down
8 changes: 8 additions & 0 deletions internal/gno/testdata/parsePackages-interface.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ var x interface{ Baz(int) bool }
"Name": "Baz",
"Signature": "Baz(int) bool",
"Kind": "method",
"Fields": [
{
"Name": "int",
"Signature": "int",
"Kind": "field",
"Type": "int"
}
],
"Type": "bool"
}
]
Expand Down
20 changes: 18 additions & 2 deletions internal/gno/testdata/parsePackages-variable.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type MyType struct {
d int
}

func (MyType) F(int) bool { return false }
func (MyType) F(i int) bool { return false }

func (o *otherType) f(int) bool { return false }

Expand Down Expand Up @@ -70,9 +70,17 @@ func Hello() {
},
{
"Name": "F",
"Signature": "func (MyType) F(int) bool",
"Signature": "func (MyType) F(i int) bool",
"Kind": "method",
"Recv": "MyType",
"Fields": [
{
"Name": "i",
"Signature": "i int",
"Kind": "field",
"Type": "int"
}
],
"Type": "bool"
}
]
Expand All @@ -93,6 +101,14 @@ func Hello() {
"Signature": "func (o *otherType) f(int) bool",
"Kind": "method",
"Recv": "otherType",
"Fields": [
{
"Name": "int",
"Signature": "int",
"Kind": "field",
"Type": "int"
}
],
"Type": "bool"
}
]
Expand Down

0 comments on commit a4fe1fc

Please sign in to comment.