-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiteratePackageFiles.go
40 lines (33 loc) · 1.12 KB
/
iteratePackageFiles.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package parser
import (
"go/ast"
"golang.org/x/tools/go/packages"
)
type packageFilesIterator = func(currFile *ast.File, filePkg *packages.Package, logger LoggerCLI) error
// iteratePackageFiles will iterate over the files inside the parsed packages.
//
// Note that if the focus is set to filepath, it will iterate only over the specified
// file
func (p *GoParser) iteratePackageFiles(callback packageFilesIterator, optionalLogger ...LoggerCLI) error {
packagesIterator := func(pkg *packages.Package, logger LoggerCLI) error {
logger = logger.Trace("Iterating over package files...")
if len(pkg.Syntax) == 0 {
logger.Trace("Skipped (zero Syntax objects)...")
return nil
}
for _, currFile := range pkg.Syntax {
currFilePath := p.fileSet.File(currFile.Pos()).Name()
currLogger := logger.Trace("Analysing *ast.File '%s'...", currFilePath)
if !p.focus.is(focusFilePath, currFilePath) {
currLogger.Trace("Skipped (not the focus)...")
continue
}
e := callback(currFile, pkg, currLogger)
if e != nil {
return e
}
}
return nil
}
return p.iteratePackages(packagesIterator, optionalLogger...)
}