Skip to content

Commit da7c3e7

Browse files
authored
fix:(uniast) add method's receiver to Node.Dependencies (cloudwego#20)
* fix:(go) add receiver into `Node.Dependencies` * fix: not collect anonymous sub struct * fix:(uniast) dedup `Type.Implements`
1 parent 637434e commit da7c3e7

8 files changed

Lines changed: 47 additions & 18 deletions

File tree

lang/golang/parser/file.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,17 @@ func (p *GoParser) parseStruct(ctx *fileContext, struName string, name *ast.Iden
471471
fieldname = fieldDecl.Names[0].Name
472472
}
473473
if stru, ok := fieldDecl.Type.(*ast.StructType); ok {
474-
// anonymous struct. parse and collect it
474+
// anonymous struct. parse it
475475
as, _ := p.parseStruct(ctx, "_"+fieldname, nil, stru)
476-
dep := NewDependency(as.Identity, ctx.FileLine(fieldDecl.Type))
477-
st.SubStruct = append(st.SubStruct, dep)
476+
// move out substructs of the anonymous struct
477+
for _, dep := range as.SubStruct {
478+
st.SubStruct = InsertDependency(st.SubStruct, dep)
479+
}
480+
for _, dep := range as.InlineStruct {
481+
st.SubStruct = InsertDependency(st.InlineStruct, dep)
482+
}
483+
// remove the anonymous struct from the repo
484+
delete(p.repo.GetPackage(as.ModPath, as.PkgPath).Types, as.Name)
478485
} else {
479486
p.collectTypes(ctx, fieldDecl.Type, st, inlined)
480487
}

lang/golang/parser/pkg.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ func (p *GoParser) associateImplements() {
9393
for iface, iid := range p.interfaces {
9494
if types.Implements(typ, iface) {
9595
tobj := p.getRepo().GetType(tid)
96-
tobj.Implements = append(tobj.Implements, iid)
96+
tobj.Implements = Append(tobj.Implements, iid)
9797
}
9898
// 另外检查 typ 的指针类型是否实现了 iface
9999
if types.Implements(types.NewPointer(typ), iface) {
100100
tobj := p.getRepo().GetType(tid)
101-
tobj.Implements = append(tobj.Implements, iid)
101+
tobj.Implements = Append(tobj.Implements, iid)
102102
}
103103
}
104104
}

lang/golang/parser/pkg_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ func Test_goParser_ParseDirs(t *testing.T) {
114114
if err != nil {
115115
t.Fatal(err)
116116
}
117-
_ = out
118-
println(string(out))
117+
if err := os.WriteFile("golang.json", out, 0644); err != nil {
118+
t.Fatal(err)
119+
}
119120
})
120121
}
121122
}

lang/uniast/node.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func calOffset(ref, dep FileLine) int {
9090

9191
func (r *Repository) AddRelation(node *Node, dep Identity, depFl FileLine) {
9292
line := calOffset(node.FileLine(), depFl)
93-
node.Dependencies = append(node.Dependencies, Relation{
93+
node.Dependencies = InsertRelation(node.Dependencies, Relation{
9494
Identity: dep,
9595
Kind: DEPENDENCY,
9696
Line: line,
@@ -104,7 +104,7 @@ func (r *Repository) AddRelation(node *Node, dep Identity, depFl FileLine) {
104104
}
105105
r.Graph[key] = nd
106106
}
107-
nd.References = append(nd.References, Relation{
107+
nd.References = InsertRelation(nd.References, Relation{
108108
Identity: node.Identity,
109109
Kind: REFERENCE,
110110
Line: line,
@@ -145,6 +145,9 @@ func (r *Repository) BuildGraph() error {
145145
for _, dep := range f.Types {
146146
r.AddRelation(n, dep.Identity, dep.FileLine)
147147
}
148+
if f.Receiver != nil {
149+
r.AddRelation(n, f.Receiver.Type, n.FileLine())
150+
}
148151
for _, dep := range f.GlobalVars {
149152
r.AddRelation(n, dep.Identity, dep.FileLine)
150153
}

lang/uniast/utils.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ import (
2121
"os"
2222
)
2323

24+
func Append[T comparable](ids []T, id T) []T {
25+
for _, i := range ids {
26+
if i == id {
27+
return ids
28+
}
29+
}
30+
return append(ids, id)
31+
}
32+
2433
func InsertDependency(ids []Dependency, id Dependency) []Dependency {
2534
for _, i := range ids {
2635
if i.Identity == id.Identity {
@@ -39,6 +48,15 @@ func InserImport(ids []Import, id Import) []Import {
3948
return append(ids, id)
4049
}
4150

51+
func InsertRelation(ids []Relation, id Relation) []Relation {
52+
for _, i := range ids {
53+
if i.Identity == id.Identity {
54+
return ids
55+
}
56+
}
57+
return append(ids, id)
58+
}
59+
4260
func LoadRepo(path string) (*Repository, error) {
4361
bs, err := os.ReadFile(path)
4462
if err != nil {

testdata/golang/go.mod

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ module a.b/c
22

33
go 1.20
44

5-
require (
6-
github.com/bytedance/sonic v1.10.2
7-
github.com/pkg/errors v0.9.1
8-
)
5+
require github.com/bytedance/sonic v1.10.2
96

107
require (
118
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect

testdata/golang/go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
1414
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
1515
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
1616
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
17-
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
18-
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1917
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2018
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2119
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

testdata/golang/pkg/entity/entity.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Copyright 2025 CloudWeGo Authors
2-
//
2+
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
6-
//
6+
//
77
// https://www.apache.org/licenses/LICENSE-2.0
8-
//
8+
//
99
// Unless required by applicable law or agreed to in writing, software
1010
// distributed under the License is distributed on an "AS IS" BASIS,
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -25,13 +25,18 @@ type MyStruct struct {
2525
b string
2626
c MyStructC
2727
MyStructD
28+
Embed struct {
29+
*MyStruct
30+
}
2831
}
2932

3033
type InterfaceB interface {
3134
String() string
3235
}
3336

3437
func (a MyStruct) String() string {
38+
_ = a.Embed.MyStruct
39+
_ = a.MyStructD
3540
return "base struct"
3641
}
3742

0 commit comments

Comments
 (0)