Skip to content

Commit eeb0b4e

Browse files
authored
fix:(go) parser missing complicated method calls (#28)
* fix:(go) parser missing complicated method calls * test
1 parent 82df42c commit eeb0b4e

File tree

4 files changed

+289
-259
lines changed

4 files changed

+289
-259
lines changed

lang/golang/parser/ctx.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ type typeInfo struct {
252252
IsPointer bool
253253
IsStdOrBuiltin bool
254254
Deps []Identity
255+
Ty types.Type
255256
}
256257

257258
// FIXME: for complex type like map[XX]YY , we only extract first-meet type here
@@ -284,7 +285,7 @@ func (ctx *fileContext) mockType(typ ast.Expr) typeInfo {
284285
if err != nil {
285286
goto fallback
286287
}
287-
return typeInfo{NewIdentity(mod, PkgPath(impt), ty.Sel.Name), false, false, nil}
288+
return typeInfo{NewIdentity(mod, PkgPath(impt), ty.Sel.Name), false, false, nil, nil}
288289
case *ast.SelectorExpr:
289290
// recurse
290291
ti := ctx.mockType(xx)
@@ -295,7 +296,7 @@ func (ctx *fileContext) mockType(typ ast.Expr) typeInfo {
295296
}
296297

297298
fallback:
298-
return typeInfo{NewIdentity("UNLOADED", ctx.pkgPath, string(ctx.GetRawContent(typ))), false, true, nil}
299+
return typeInfo{NewIdentity("UNLOADED", ctx.pkgPath, string(ctx.GetRawContent(typ))), false, true, nil, nil}
299300
}
300301

301302
func (ctx *fileContext) collectFields(fields []*ast.Field, m *[]Dependency) {
@@ -453,6 +454,7 @@ func (p *GoParser) collectTypes(ctx *fileContext, typ ast.Expr, st *Type, inline
453454
func (ctx *fileContext) getTypeinfo(typ types.Type) (ti typeInfo) {
454455
tobjs, isPointer := getNamedTypes(typ)
455456
ti.IsPointer = isPointer
457+
ti.Ty = typ
456458
if len(tobjs) > 0 {
457459
tobj := tobjs[0]
458460
if tp := tobj.Pkg(); tp != nil {

lang/golang/parser/file.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,19 @@ func (p *GoParser) parseSelector(ctx *fileContext, expr *ast.SelectorExpr, infos
263263
// recurse call
264264
cont = p.parseSelector(ctx, sel, infos)
265265
} else {
266-
// descent to the next level
266+
// try to get type info of field first
267+
if ti := ctx.GetTypeInfo(expr); ti.Ty != nil {
268+
if _, ok := ti.Ty.(*types.Signature); ok {
269+
// collect method call
270+
// method call
271+
rev := ctx.GetTypeInfo(expr.X)
272+
if !rev.IsStdOrBuiltin {
273+
id := NewIdentity(rev.Id.ModPath, rev.Id.PkgPath, rev.Id.Name+"."+expr.Sel.Name)
274+
dep := NewDependency(id, ctx.FileLine(expr.Sel))
275+
*infos.methodCalls = InsertDependency(*infos.methodCalls, dep)
276+
}
277+
}
278+
}
267279
return true
268280
}
269281

0 commit comments

Comments
 (0)