-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenumlinter.go
52 lines (42 loc) · 1.09 KB
/
enumlinter.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
package main
import (
"go/ast"
"github.com/THE108/enumlinter/checker"
lg "github.com/THE108/enumlinter/logger"
ph "github.com/THE108/enumlinter/position_helper"
"golang.org/x/tools/go/loader"
)
func run(pkgpaths []string, typeToCheck string, logger lg.ILogger) error {
prog, err := loadProgFromArgs(pkgpaths)
if err != nil {
return err
}
posHelper := ph.NewPositionHelper(prog.Fset)
for _, pkgInfo := range prog.InitialPackages() {
switchChecker := &checker.SwitchChecker{
Pkg: pkgInfo,
PosHelper: posHelper,
Logger: logger,
}
assignmentChecker := &checker.AssignmentChecker{
Pkg: pkgInfo,
PosHelper: posHelper,
Logger: logger,
TypeToCheck: typeToCheck,
}
for _, astfile := range pkgInfo.Files {
ast.Inspect(astfile, func(node ast.Node) bool {
switchChecker.Run(node)
return assignmentChecker.Run(node)
})
}
}
return nil
}
func loadProgFromArgs(pkgpaths []string) (*loader.Program, error) {
conf := &loader.Config{}
if _, err := conf.FromArgs(pkgpaths, true); err != nil {
return nil, err
}
return conf.Load()
}