-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzer.go
139 lines (125 loc) · 4.15 KB
/
analyzer.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
package analyzer
import (
"go/ast"
"go/types"
"sort"
"strings"
"golang.org/x/tools/go/analysis"
)
var Analyzer = &analysis.Analyzer{
Name: "allfields",
Doc: "checks that marked (with //allfields comment) struct literals have all fields set",
Run: run,
}
func getAllfieldsComments(pass *analysis.Pass, file *ast.File) []*ast.Comment {
allfieldsComments := []*ast.Comment(nil)
for _, commentGroup := range file.Comments {
for _, comment := range commentGroup.List {
text := strings.TrimSpace(comment.Text)
if i := strings.Index(text, " // want"); i != -1 { // for testing
text = text[:i]
}
if text == "//allfields" || text == "//allfields:lint" {
allfieldsComments = append(allfieldsComments, comment)
} else if strings.HasPrefix(text, "//allfields ") || strings.HasPrefix(text, "//allfields:lint ") {
pass.ReportRangef(comment, "invalid allfields comment")
}
}
}
// Sort by position in ascending order
sort.Slice(allfieldsComments, func(i, j int) bool {
return allfieldsComments[i].Pos() < allfieldsComments[j].Pos()
})
return allfieldsComments
}
func findCommentForCompositeLiteral(pass *analysis.Pass, astCompositeLit *ast.CompositeLit, allfieldsComments []*ast.Comment) (*ast.Comment, bool) {
// Index of the first allfields comment after the composite literal start
commentI := sort.Search(len(allfieldsComments), func(i int) bool {
return allfieldsComments[i].Pos() >= astCompositeLit.Lbrace
})
commentsFor:
for {
if commentI >= len(allfieldsComments) {
// allfields comment not found
return nil, false
}
comment := allfieldsComments[commentI]
if comment.Pos() > astCompositeLit.Rbrace {
// allfields comment is after the composite literal end, so it's not for this composite literal
return nil, false
}
for _, elem := range astCompositeLit.Elts {
if elem.Pos() <= comment.Pos() && comment.Pos() <= elem.End() {
// allfields comment is not placed directly in the composite literal (it is placed deeper)
commentI++
continue commentsFor
}
}
return comment, true
}
}
func run(pass *analysis.Pass) (interface{}, error) {
for _, file := range pass.Files {
allfieldsComments := getAllfieldsComments(pass, file)
usedComments := map[*ast.Comment]bool{}
ast.Inspect(file, func(node ast.Node) bool {
astCompositeLit, ok := node.(*ast.CompositeLit)
if !ok {
return true
}
comment, ok := findCommentForCompositeLiteral(pass, astCompositeLit, allfieldsComments)
if !ok {
return true
}
typesObj, ok := pass.TypesInfo.Types[astCompositeLit]
if !ok {
pass.ReportRangef(astCompositeLit, "internal allfields's error: no types info for composite literal, please remove allfields comment for a while and report about the bug")
return true
}
typesStruct, ok := typesObj.Type.Underlying().(*types.Struct)
if !ok {
return true
}
usedComments[comment] = true
fields := map[string]bool{} // field name -> is used
fieldsOrdered := []string(nil)
for i := 0; i < typesStruct.NumFields(); i++ {
field := typesStruct.Field(i)
if !field.Exported() && field.Pkg() != pass.Pkg {
continue
}
fields[field.Name()] = false
fieldsOrdered = append(fieldsOrdered, field.Name())
}
for _, compositeElem := range astCompositeLit.Elts {
keyValue, ok := compositeElem.(*ast.KeyValueExpr)
if !ok {
pass.ReportRangef(comment, "allfields comment is placed in a non-keyed composite struct literal")
return true // we don't care about non-keyed literals
}
field := keyValue.Key.(*ast.Ident).Name
fields[field] = true
}
notSetFields := []string(nil)
for _, field := range fieldsOrdered {
if !fields[field] {
notSetFields = append(notSetFields, field)
}
}
if len(notSetFields) > 0 {
if len(notSetFields) == 1 {
pass.ReportRangef(astCompositeLit, "field %s is not set", notSetFields[0])
} else {
pass.ReportRangef(astCompositeLit, "fields %s are not set", strings.Join(notSetFields, ", "))
}
}
return true
})
for _, comment := range allfieldsComments {
if !usedComments[comment] {
pass.ReportRangef(comment, "allfields comment is not used")
}
}
}
return nil, nil
}