Skip to content

Commit

Permalink
fix(godocfx): filter out other modules, sort pkgs (#2894)
Browse files Browse the repository at this point in the history
The sort keeps the TOC in a stable order.
  • Loading branch information
tbpg authored Sep 21, 2020
1 parent 07df7d8 commit 868db45
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions internal/godocfx/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/martian/v3 v3.0.0 h1:pMen7vLs8nvgEYhywH3KDWJIJTeEr2ULsVWHWYHQyBs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/pprof v0.0.0-20200905233945-acf8798be1f7 h1:k+KkMRk8mGOu1xG38StS7dQ+Z6oW1i9n3dgrAVU9Q/E=
github.com/google/pprof v0.0.0-20200905233945-acf8798be1f7/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/googleapis/gax-go/v2 v2.0.5 h1:sjZBwGj9Jlw33ImPtvFviGYvseOtDM7hkSKB7+Tv3SM=
Expand Down
31 changes: 27 additions & 4 deletions internal/godocfx/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func parse(glob string) (map[string]*page, tableOfContents, *packages.Module, er

// First, collect all of the files grouped by package, including test
// packages.
pkgFiles := map[string][]string{}
allPkgFiles := map[string][]string{}
for _, pkg := range pkgs {
id := pkg.ID
// See https://pkg.go.dev/golang.org/x/tools/go/packages#Config.
Expand All @@ -144,17 +144,35 @@ func parse(glob string) (map[string]*page, tableOfContents, *packages.Module, er
for _, f := range pkg.Syntax {
name := pkg.Fset.File(f.Pos()).Name()
if strings.HasSuffix(name, ".go") {
pkgFiles[id] = append(pkgFiles[id], name)
allPkgFiles[id] = append(allPkgFiles[id], name)
}
}
}

// Test files don't have Module set. Filter out packages in skipped modules.
pkgFiles := map[string][]string{}
pkgNames := []string{}
for pkgPath, files := range allPkgFiles {
skip := false
for skipped := range skippedModules {
if strings.HasPrefix(pkgPath, skipped) {
skip = true
break
}
}
if !skip {
pkgFiles[pkgPath] = files
pkgNames = append(pkgNames, pkgPath)
}
}
sort.Strings(pkgNames)

// Once the files are grouped by package, process each package
// independently.
for pkgPath, files := range pkgFiles {
for _, pkgPath := range pkgNames {
parsedFiles := []*ast.File{}
fset := token.NewFileSet()
for _, f := range files {
for _, f := range pkgFiles[pkgPath] {
pf, err := parser.ParseFile(fset, f, nil, parser.ParseComments)
if err != nil {
return nil, nil, nil, fmt.Errorf("ParseFile: %v", err)
Expand All @@ -168,6 +186,11 @@ func parse(glob string) (map[string]*page, tableOfContents, *packages.Module, er
return nil, nil, nil, fmt.Errorf("doc.NewFromFiles: %v", err)
}

// Extra filter in case the file filtering didn't catch everything.
if !strings.HasPrefix(docPkg.ImportPath, module.Path) {
continue
}

toc = append(toc, &tocItem{
UID: docPkg.ImportPath,
Name: docPkg.ImportPath,
Expand Down

0 comments on commit 868db45

Please sign in to comment.