diff --git a/v3/internal/parser/README.md b/v3/internal/parser/README.md index 33adb9edc..491dc20d7 100644 --- a/v3/internal/parser/README.md +++ b/v3/internal/parser/README.md @@ -13,6 +13,8 @@ This package contains the static analyser used for parsing Wails projects so tha - [ ] Assignment - [x] Struct Literal Pointer - [ ] Function + - [ ] Same package + - [ ] Different package - [ ] Function - [x] Parsing of bound methods diff --git a/v3/internal/parser/parser.go b/v3/internal/parser/parser.go index 8c78aa052..ff10e1508 100644 --- a/v3/internal/parser/parser.go +++ b/v3/internal/parser/parser.go @@ -239,6 +239,10 @@ func (p *Project) findApplicationNewCalls(pkgs map[string]*ParsedPackage) (err e return result } + // Check if the assignment is the result of a function call + if _, ok := assign.Rhs[0].(*ast.CallExpr); ok { + println("TODO: Parsing call expression") + } } } } diff --git a/v3/internal/parser/parser_test.go b/v3/internal/parser/parser_test.go index c9f8ff6b8..2e893b566 100644 --- a/v3/internal/parser/parser_test.go +++ b/v3/internal/parser/parser_test.go @@ -1154,6 +1154,157 @@ func TestParseDirectory(t *testing.T) { }, }, }, + { + name: "should find a bound services using a variable from function call", + dir: "testdata/variable_single_from_function", + wantErr: false, + wantBoundMethods: map[string]map[string][]*BoundMethod{ + "main": { + "GreetService": { + { + Name: "Greet", + DocComment: "Greet someone\n", + Inputs: []*Parameter{ + { + Name: "name", + Type: &ParameterType{ + Name: "string", + }, + }, + }, + Outputs: []*Parameter{ + { + Name: "", + Type: &ParameterType{ + Name: "string", + }, + }, + }, + }, + }, + }, + }, + }, + { + name: "should find a bound services using a variable from function call in another package", + dir: "testdata/variable_single_from_other_function", + wantErr: false, + wantBoundMethods: map[string]map[string][]*BoundMethod{ + "main": { + "GreetService": { + { + Name: "Greet", + DocComment: "Greet does XYZ\n", + Inputs: []*Parameter{ + { + Name: "name", + Type: &ParameterType{ + Name: "string", + }, + }, + }, + Outputs: []*Parameter{ + { + Name: "", + Type: &ParameterType{ + Name: "string", + }, + }, + }, + }, + { + Name: "NewPerson", + DocComment: "NewPerson creates a new person\n", + Inputs: []*Parameter{ + { + Name: "name", + Type: &ParameterType{ + Name: "string", + }, + }, + }, + Outputs: []*Parameter{ + { + Name: "", + Type: &ParameterType{ + Name: "Person", + IsPointer: true, + IsStruct: true, + Package: "main", + }, + }, + }, + }, + }, + }, + "github.com/wailsapp/wails/v3/internal/parser/testdata/variable_single_from_other_function/services": { + "OtherService": { + { + Name: "Yay", + Outputs: []*Parameter{ + { + Type: &ParameterType{ + Name: "Address", + IsStruct: true, + IsPointer: true, + Package: "github.com/wailsapp/wails/v3/internal/parser/testdata/variable_single_from_other_function/services", + }, + }, + }, + }, + }, + }, + }, + wantModels: map[string]map[string]*StructDef{ + "main": { + "Person": { + Name: "Person", + Fields: []*Field{ + { + Name: "Name", + Type: &ParameterType{ + Name: "string", + }, + }, + { + Name: "Address", + Type: &ParameterType{ + Name: "Address", + IsStruct: true, + IsPointer: true, + Package: "github.com/wailsapp/wails/v3/internal/parser/testdata/variable_single_from_other_function/services", + }, + }, + }, + }, + }, + "github.com/wailsapp/wails/v3/internal/parser/testdata/variable_single_from_other_function/services": { + "Address": { + Name: "Address", + Fields: []*Field{ + { + Name: "Street", + Type: &ParameterType{ + Name: "string", + }, + }, + { + Name: "State", + Type: &ParameterType{ + Name: "string", + }, + }, + { + Name: "Country", + Type: &ParameterType{ + Name: "string", + }, + }, + }, + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/v3/internal/parser/testdata/variable_single_from_function/main.go b/v3/internal/parser/testdata/variable_single_from_function/main.go new file mode 100644 index 000000000..0d7121ca9 --- /dev/null +++ b/v3/internal/parser/testdata/variable_single_from_function/main.go @@ -0,0 +1,40 @@ +package main + +import ( + _ "embed" + "github.com/wailsapp/wails/v3/pkg/application" + "log" +) + +// GreetService is great +type GreetService struct { + SomeVariable int + lowerCase string +} + +// Greet someone +func (*GreetService) Greet(name string) string { + return "Hello " + name +} + +func NewGreetService() *GreetService { + return &GreetService{} +} + +func main() { + greetService := NewGreetService() + app := application.New(application.Options{ + Bind: []interface{}{ + greetService, + }, + }) + + app.NewWebviewWindow() + + err := app.Run() + + if err != nil { + log.Fatal(err) + } + +} diff --git a/v3/internal/parser/testdata/variable_single_from_other_function/main.go b/v3/internal/parser/testdata/variable_single_from_other_function/main.go new file mode 100644 index 000000000..9d10a301e --- /dev/null +++ b/v3/internal/parser/testdata/variable_single_from_other_function/main.go @@ -0,0 +1,50 @@ +package main + +import ( + _ "embed" + "github.com/wailsapp/wails/v3/internal/parser/testdata/variable_single_from_other_function/services" + "log" + + "github.com/wailsapp/wails/v3/pkg/application" +) + +// GreetService is great +type GreetService struct { + SomeVariable int + lowerCase string + target *Person +} + +type Person struct { + Name string + Address *services.Address +} + +// Greet does XYZ +func (*GreetService) Greet(name string) string { + return "Hello " + name +} + +// NewPerson creates a new person +func (*GreetService) NewPerson(name string) *Person { + return &Person{Name: name} +} + +func main() { + otherService := services.NewOtherService() + app := application.New(application.Options{ + Bind: []interface{}{ + &GreetService{}, + otherService, + }, + }) + + app.NewWebviewWindow() + + err := app.Run() + + if err != nil { + log.Fatal(err) + } + +} diff --git a/v3/internal/parser/testdata/variable_single_from_other_function/services/other.go b/v3/internal/parser/testdata/variable_single_from_other_function/services/other.go new file mode 100644 index 000000000..2daa9df17 --- /dev/null +++ b/v3/internal/parser/testdata/variable_single_from_other_function/services/other.go @@ -0,0 +1,26 @@ +package services + +// OtherService is a struct +// that does things +type OtherService struct { + t int +} + +type Address struct { + Street string + State string + Country string +} + +// Yay does this and that +func (o *OtherService) Yay() *Address { + return &Address{ + Street: "123 Pitt Street", + State: "New South Wales", + Country: "Australia", + } +} + +func NewOtherService() *OtherService { + return &OtherService{} +}