-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyze.go
59 lines (55 loc) · 1.51 KB
/
analyze.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package allfields
import (
"fmt"
"go/token"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/packages"
)
type AnalyzeConfig struct {
PackagesPattern string // e.g. "./..."
ReportErr func(errMessage string)
Tests bool // if true, tests will be analyzed too
}
func Analyze(config AnalyzeConfig) {
if config.ReportErr == nil {
panic("config.ReportErr is nil, must be set")
}
if config.PackagesPattern == "" {
config.ReportErr("AnalyzeConfig.PackagesPattern is empty, must be set")
return
}
pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedName | packages.NeedFiles |
packages.NeedSyntax | packages.NeedTypes | packages.NeedTypesInfo,
Tests: config.Tests,
}, config.PackagesPattern)
if err != nil {
config.ReportErr("load packages: " + err.Error())
return
}
reportedPositions := make(map[token.Pos]bool) // needed because test packages are separate packages
for _, pkg := range pkgs {
if len(pkg.Errors) > 0 {
for _, err := range pkg.Errors {
config.ReportErr(err.Error())
}
continue
}
_, err := Analyzer.Run(&analysis.Pass{
Files: pkg.Syntax,
TypesInfo: pkg.TypesInfo,
Pkg: pkg.Types,
Report: func(d analysis.Diagnostic) {
if reportedPositions[d.Pos] {
return
}
reportedPositions[d.Pos] = true
pos := pkg.Fset.Position(d.Pos)
config.ReportErr(pos.String() + ": " + d.Message)
},
})
if err != nil {
config.ReportErr(fmt.Sprintf("run analyzer for package %q: %v", pkg.PkgPath, err.Error()))
}
}
}