-
Notifications
You must be signed in to change notification settings - Fork 2
/
enumcover.go
157 lines (142 loc) · 3.96 KB
/
enumcover.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package enumcover
import (
"fmt"
"go/ast"
"go/types"
"regexp"
"strconv"
"strings"
"sync"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
)
const Doc = `check that code blocks cover all consts of a given type`
var Analyzer = &analysis.Analyzer{
Doc: Doc,
Name: "enumcover",
Run: enumcoverCheck,
Requires: []*analysis.Analyzer{inspect.Analyzer},
}
var commentRegex = regexp.MustCompile(`enumcover:([\w\.]+)`)
func enumcoverCheck(pass *analysis.Pass) (interface{}, error) {
for _, file := range pass.Files {
commentMap := ast.NewCommentMap(pass.Fset, file, file.Comments)
ast.Inspect(file, func(n ast.Node) bool {
if n == nil {
return true
}
for _, comments := range commentMap[n] {
for _, comment := range comments.List {
matches := commentRegex.FindAllStringSubmatch(comment.Text, 1)
if len(matches) == 1 && len(matches[0]) == 2 {
typeName := fullTypeName(pass, file, n, strings.TrimSpace(matches[0][1]))
checkConsts(pass, n, typeName)
}
}
}
return true
})
}
return nil, nil
}
func fullTypeName(pass *analysis.Pass, file *ast.File, n ast.Node, typeName string) string {
selectorParts := strings.Split(typeName, ".")
if len(selectorParts) == 2 {
for _, fimport := range file.Imports {
var pkgName string
if fimport.Name != nil {
if fimport.Name.Name == "." {
// TODO: handle dot imports
reportNodef(pass, n, "Dot imports are unhandled!")
}
pkgName = fimport.Name.Name
} else {
components := strings.Split(unquote(fimport.Path.Value), "/")
pkgName = components[len(components)-1]
}
if selectorParts[0] == pkgName {
typeName = unquote(fimport.Path.Value) + "." + selectorParts[1]
}
}
} else {
typeName = pass.Pkg.Path() + "." + typeName
}
return typeName
}
func checkConsts(pass *analysis.Pass, n ast.Node, typeName string) {
allConsts := buildAllConstMap(pass, typeName)
namesForType := map[string]bool{}
ast.Inspect(n, func(n ast.Node) bool {
if expr, ok := n.(ast.Expr); ok {
t := pass.TypesInfo.TypeOf(expr)
if t != nil && t.String() == typeName {
switch n := n.(type) {
case *ast.BasicLit:
namesForType[unquote(n.Value)] = true
case *ast.Ident:
namedConst := allConsts[n.Name]
namesForType[namedConst.val] = true
}
}
}
return true
})
if len(allConsts) == 0 {
reportNodef(pass, n, "No consts found for type %v", typeName)
}
for _, want := range allConsts {
if !namesForType[want.val] {
reportNodef(pass, n, "Unhandled const: %v", want)
}
}
}
func reportNodef(pass *analysis.Pass, node ast.Node, format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
pass.Report(analysis.Diagnostic{Pos: node.Pos(), End: node.End(), Message: msg})
}
func unquote(str string) string {
if unquoted, err := strconv.Unquote(str); err == nil {
return unquoted
}
return str
}
type constVal struct {
name string
val string
}
func (c constVal) String() string {
return fmt.Sprintf("%s (%s)", c.name, c.val)
}
var allPkgs sync.Map
func initializeAllPkgs(pass *analysis.Pass) {
var visit func(pkg *types.Package)
visit = func(pkg *types.Package) {
if _, ok := allPkgs.Load(pkg); ok {
return
}
allPkgs.Store(pkg, struct{}{})
for _, imp := range pkg.Imports() {
visit(imp)
}
}
visit(pass.Pkg)
}
// TODO: do this by storing analysis.Facts about all the consts in each package?
func buildAllConstMap(pass *analysis.Pass, targetType string) map[string]constVal {
initializeAllPkgs(pass)
constMap := map[string]constVal{}
allPkgs.Range(func(pkgKey, _ interface{}) bool {
pkg := pkgKey.(*types.Package)
for _, name := range pkg.Scope().Names() {
if namedConst, ok := pkg.Scope().Lookup(name).(*types.Const); ok {
val := unquote(namedConst.Val().ExactString())
typeName := namedConst.Type().String()
if typeName == targetType {
constMap[namedConst.Name()] = constVal{name: namedConst.Name(), val: val}
}
}
}
return true
})
return constMap
}