@@ -120,7 +120,7 @@ func (p *GoParser) parseVar(ctx *fileContext, vspec *ast.ValueSpec, isConst bool
120120 v = p .newVar (ctx .module .Name , ctx .pkgPath , name .Name , isConst )
121121 v .FileLine = ctx .FileLine (vspec )
122122
123- // always collect value's dependencies
123+ // collect func value dependencies, in case of var a = func() {...}
124124 if val != nil && ! isConst {
125125 collects := collectInfos {}
126126 ast .Inspect (* val , func (n ast.Node ) bool {
@@ -159,38 +159,13 @@ func (p *GoParser) parseVar(ctx *fileContext, vspec *ast.ValueSpec, isConst bool
159159 if isConst && v .Type == nil {
160160 v .Type = lastType
161161 }
162- var varType string
163- if v .Type != nil {
164- if v .Type .PkgPath == ctx .pkgPath {
165- varType = v .Type .Name
166- } else {
167- varType = v .Type .CallName ()
168- }
169- if v .IsPointer {
170- varType = "*" + varType
171- }
172- }
173162
174163 if ! isConst {
175- v .Content = fmt . Sprintf ( "var %s %s" , name . Name , varType )
164+ v .Content = "var " + string ( ctx . GetRawContent ( vspec ) )
176165 } else {
177- if varType != "" {
178- v .Content = fmt .Sprintf ("const %s %s" , name .Name , varType )
179- } else {
180- v .Content = fmt .Sprintf ("const %s" , name .Name )
181- }
166+ v .Content = "const " + string (ctx .GetRawContent (vspec ))
182167 }
183168
184- var comment string
185- if ctx .collectComment && doc != nil {
186- comment += string (ctx .GetRawContent (doc )) + "\n "
187- }
188- if ctx .collectComment && vspec .Doc != nil {
189- comment += string (ctx .GetRawContent (vspec .Doc )) + "\n "
190- v .FileLine .StartOffset = ctx .fset .Position (vspec .Pos ()).Offset
191- }
192- v .Content = comment + v .Content
193-
194169 var finalVal string
195170 if val != nil {
196171 // refer codes
@@ -229,11 +204,20 @@ func (p *GoParser) parseVar(ctx *fileContext, vspec *ast.ValueSpec, isConst bool
229204 lastValue = & tmp
230205 finalVal = strconv .FormatFloat (tmp , 'f' , - 1 , 64 )
231206 }
232-
233- if finalVal != "" {
207+ if finalVal != "" && ! strings .Contains (v .Content , " = " ) {
234208 v .Content += " = " + finalVal
235209 }
236210
211+ var comment string
212+ if ctx .collectComment && doc != nil {
213+ comment += string (ctx .GetRawContent (doc )) + "\n "
214+ }
215+ if ctx .collectComment && vspec .Doc != nil {
216+ comment += string (ctx .GetRawContent (vspec .Doc )) + "\n "
217+ v .FileLine .StartOffset = ctx .fset .Position (vspec .Pos ()).Offset
218+ }
219+ v .Content = comment + v .Content
220+
237221 typ = v .Type
238222 }
239223 return typ , v , lastValue
@@ -441,22 +425,23 @@ func (p *GoParser) parseASTNode(ctx *fileContext, node ast.Node, collect *collec
441425func (p * GoParser ) parseFunc (ctx * fileContext , funcDecl * ast.FuncDecl ) (* Function , bool ) {
442426 // method receiver
443427 var receiver * Receiver
444- isMethod := funcDecl .Recv != nil
445- if strings .HasSuffix (ctx .filePath , "cmds/life_stat/main.go" ) && funcDecl .Name .Name == "init" {
446-
447- }
428+ var tparams []Dependency
429+ isMethod := funcDecl .Recv != nil && len (funcDecl .Recv .List ) > 0
448430 if isMethod {
449- // TODO: reserve the pointer message?
450- ti := ctx .GetTypeInfo (funcDecl .Recv .List [0 ].Type )
451- // name := "self"
452- // if len(funcDecl.Recv.List[0].Names) > 0 {
453- // name = funcDecl.Recv.List[0].Names[0].Name
454- // }
431+ rt := funcDecl .Recv .List [0 ].Type
432+ ti := ctx .GetTypeInfo (rt )
455433 receiver = & Receiver {
456434 Type : ti .Id ,
457435 IsPointer : ti .IsPointer ,
458436 // Name: name,
459437 }
438+ // collect receiver's type params
439+ for _ , d := range ti .Deps {
440+ tparams = append (tparams , Dependency {
441+ Identity : d ,
442+ FileLine : ctx .FileLine (rt ), // FIXME: location is not accurate, try parse Index AST to get it.
443+ })
444+ }
460445 }
461446
462447 fname := funcDecl .Name .Name
@@ -474,6 +459,10 @@ func (p *GoParser) parseFunc(ctx *fileContext, funcDecl *ast.FuncDecl) (*Functio
474459 if funcDecl .Type .Results != nil {
475460 ctx .collectFields (funcDecl .Type .Results .List , & results )
476461 }
462+ // collect type params
463+ if funcDecl .Type .TypeParams != nil {
464+ ctx .collectFields (funcDecl .Type .TypeParams .List , & tparams )
465+ }
477466
478467 // collect signature
479468 sig := ctx .GetRawContent (funcDecl .Type )
@@ -510,6 +499,9 @@ set_func:
510499 f .Results = results
511500 f .GlobalVars = collects .globalVars
512501 f .Types = collects .tys
502+ for _ , t := range tparams {
503+ f .Types = InsertDependency (f .Types , t )
504+ }
513505 f .Signature = string (sig )
514506 return f , false
515507}
@@ -534,6 +526,10 @@ func (p *GoParser) parseType(ctx *fileContext, typDecl *ast.TypeSpec, doc *ast.C
534526 }
535527 }
536528
529+ if typDecl .TypeParams != nil {
530+ ctx .collectFields (typDecl .TypeParams .List , & st .SubStruct )
531+ }
532+
537533 st .FileLine = ctx .FileLine (typDecl )
538534 st .Content = string (ctx .GetRawContent (typDecl ))
539535 if ctx .collectComment && doc != nil {
0 commit comments