-
Notifications
You must be signed in to change notification settings - Fork 1
/
typecover.go
230 lines (206 loc) · 5.85 KB
/
typecover.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package typecover
import (
"fmt"
"go/ast"
"go/types"
"regexp"
"strconv"
"strings"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
)
const Doc = `typecover checks that a code block is assigning to all exported fields of a struct or calling all exported methods of an interface.`
var Analyzer = &analysis.Analyzer{
Doc: Doc,
Name: "typecover",
Run: run,
Requires: []*analysis.Analyzer{inspect.Analyzer},
}
var commentRegex = regexp.MustCompile(`typecover:([\w.]+)`)
func run(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 {
for _, comments := range commentMap[n] {
for _, comment := range comments.List {
var exclude []string
matches := commentRegex.FindAllStringSubmatch(comment.Text, 1)
if len(matches) == 1 && len(matches[0]) == 2 {
// Check if there are any member to exclude
ss := strings.Split(comment.Text, "-exclude")
if len(ss) > 1 {
exclude = strings.Split(strings.TrimLeft(ss[1], "= "), ",")
}
if len(ss) > 2 {
reportNodef(pass, n, "Detected more than one '~'. Separate arguments with commas")
}
typeName := fullTypeName(pass, file, n, strings.TrimSpace(matches[0][1]))
t := findType(pass, typeName)
if t == nil {
reportNodef(pass, n, "Type %s not found in project scope", typeName)
return false
}
missing := checkMembers(pass, n, t, exclude)
if len(missing) > 0 {
reportNodef(pass, n, "Type %s is missing %s", typeName, strings.Join(missing, ", "))
}
}
}
}
return true
})
}
return nil, nil
}
func checkMembers(pass *analysis.Pass, n ast.Node, target types.Type, exclude []string) []string {
membersFound := map[string]bool{}
for _, t := range []types.Type{target, types.NewPointer(target)} {
mset := types.NewMethodSet(t)
for i := 0; i < mset.Len(); i++ {
switch u := mset.At(i).Obj().(type) {
case *types.Func:
if u.Exported() {
membersFound[u.Name()] = false
}
}
}
}
switch u := target.Underlying().(type) {
case *types.Interface:
for i := 0; i < u.NumMethods(); i++ {
if u.Method(i).Exported() {
membersFound[u.Method(i).Name()] = false
}
}
ast.Inspect(n, func(n ast.Node) bool {
if se, ok := n.(*ast.SelectorExpr); ok {
t1 := pass.TypesInfo.TypeOf(se.X)
if t1 == nil {
return true
}
// either the type itself or the pointer of type should implement interface u
if !types.Implements(t1, u) && !types.Implements(types.NewPointer(t1), u) {
return true
}
if se.Sel != nil {
if _, ok := membersFound[se.Sel.Name]; ok {
membersFound[se.Sel.Name] = true
}
}
}
return true
})
case *types.Struct:
for i := 0; i < u.NumFields(); i++ {
if u.Field(i).Exported() {
membersFound[u.Field(i).Name()] = false
}
}
ast.Inspect(n, func(n ast.Node) bool {
switch nt := n.(type) {
case *ast.CompositeLit: // nt = MyType{Field: 1}
t := pass.TypesInfo.TypeOf(nt.Type)
if t == nil || strings.TrimPrefix(t.String(), "*") != target.String() {
return true
}
for _, e := range nt.Elts {
if k, ok := e.(*ast.KeyValueExpr); ok {
if i, ok2 := k.Key.(*ast.Ident); ok2 {
if _, ok3 := membersFound[i.Name]; ok3 {
membersFound[i.Name] = true
}
}
} else {
// todo: support CompositeLit with anonymous fields
}
}
case *ast.SelectorExpr: // nt.Field = val
t := pass.TypesInfo.TypeOf(nt.X)
if t == nil || strings.TrimPrefix(t.String(), "*") != target.String() {
return true
}
if nt.Sel != nil {
if _, ok := membersFound[nt.Sel.Name]; ok {
membersFound[nt.Sel.Name] = true
}
}
}
return true
})
}
// Mark all excluded members as found.
for _, e := range exclude {
if _, ok := membersFound[strings.TrimSpace(e)]; ok {
membersFound[e] = true
}
}
var missing []string
for member, found := range membersFound {
if !found {
missing = append(missing, member)
}
}
return missing
}
func findType(pass *analysis.Pass, targetType string) types.Type {
lastDot := strings.LastIndex(targetType, ".")
if lastDot == -1 {
panic(fmt.Sprintf("ill-formed targetType %q", targetType))
}
pkgName := targetType[:lastDot]
typeName := targetType[lastDot+1:]
if pkgMatch(pass.Pkg.Path(), pkgName) {
o := pass.Pkg.Scope().Lookup(typeName)
if o != nil {
return o.Type()
}
}
for _, imp := range pass.Pkg.Imports() {
if pkgMatch(imp.Path(), pkgName) {
o := imp.Scope().Lookup(typeName)
if o != nil {
return o.Type()
}
}
}
return nil
}
func pkgMatch(path, pkgName string) bool {
for strings.Contains(path, "/vendor/") {
path = strings.Split(path, "/vendor/")[1]
}
return path == pkgName
}
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 == "." {
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 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
}