Skip to content

Commit

Permalink
Fix comment collection
Browse files Browse the repository at this point in the history
Signed-off-by: weiyang <weiyang.ones@gmail.com>
  • Loading branch information
wy-z committed Nov 26, 2018
1 parent 0dc6e6a commit 639a65e
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions tspec/tspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,14 @@ func (t *Parser) Options(opts ...ParserOptions) ParserOptions {
}

// ParseDir parses the dir and cache it
func (t *Parser) ParseDir(dirPath string, pkgName string) (pkg *ast.Package, err error) {
if tmpPkg, ok := t.dirPkgMap[dirPath]; ok {
func (t *Parser) ParseDir(dirPath string, pkgName string, useCacheOpts ...bool) (pkg *ast.Package, cached bool, err error) {
useCache := true
if len(useCacheOpts) > 0 {
useCache = useCacheOpts[0]
}
if tmpPkg, ok := t.dirPkgMap[dirPath]; ok && useCache {
pkg = tmpPkg
cached = true
return
}

Expand Down Expand Up @@ -105,11 +110,23 @@ func (t *Parser) Import(pkgPath string) (pkg *ast.Package, err error) {
return
}

pkg, err = t.ParseDir(importPkg.Dir, importPkg.Name)
pkg, cached, err := t.ParseDir(importPkg.Dir, importPkg.Name)
if err != nil {
err = errors.WithStack(err)
return
}
if !cached {
// collects comments
// doc.New takes ownership of the AST pkg and may edit or overwrite it.
docPkg, _, e := t.ParseDir(importPkg.Dir, importPkg.Name, false)
if e != nil {
err = errors.WithStack(e)
return
}
for _, typ := range doc.New(docPkg, "", 0).Types {
t.docTypeMap[typ.Name] = typ
}
}
return
}

Expand All @@ -130,10 +147,6 @@ func (t *Parser) ParsePkg(pkg *ast.Package) (objs map[string]*ast.Object, err er
}
}
t.pkgObjsMap[pkg] = objs

for _, typ := range doc.New(pkg, "", 0).Types {
t.docTypeMap[typ.Name] = typ
}
return
}

Expand Down

0 comments on commit 639a65e

Please sign in to comment.