-
Notifications
You must be signed in to change notification settings - Fork 5
/
analyzer.go
257 lines (213 loc) · 6.24 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package analyzer
import (
"flag"
"go/ast"
"go/token"
"strings"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
const (
TimeWeekdayFlag = "time-weekday"
TimeMonthFlag = "time-month"
TimeLayoutFlag = "time-layout"
CryptoHashFlag = "crypto-hash"
HTTPMethodFlag = "http-method"
HTTPStatusCodeFlag = "http-status-code"
DefaultRPCPathFlag = "default-rpc-path"
)
// New returns new usestdlibvars analyzer.
func New() *analysis.Analyzer {
return &analysis.Analyzer{
Name: "usestdlibvars",
Doc: "A linter that detect the possibility to use variables/constants from the Go standard library.",
Run: run,
Flags: flags(),
Requires: []*analysis.Analyzer{inspect.Analyzer},
}
}
func flags() flag.FlagSet {
flags := flag.NewFlagSet("", flag.ExitOnError)
flags.Bool(HTTPMethodFlag, true, "suggest the use of http.MethodXX")
flags.Bool(HTTPStatusCodeFlag, true, "suggest the use of http.StatusXX")
flags.Bool(TimeWeekdayFlag, false, "suggest the use of time.Weekday")
flags.Bool(TimeMonthFlag, false, "suggest the use of time.Month")
flags.Bool(TimeLayoutFlag, false, "suggest the use of time.Layout")
flags.Bool(CryptoHashFlag, false, "suggest the use of crypto.Hash")
flags.Bool(DefaultRPCPathFlag, false, "suggest the use of rpc.DefaultXXPath")
return *flags
}
func run(pass *analysis.Pass) (interface{}, error) {
insp := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
filter := []ast.Node{
(*ast.CallExpr)(nil),
(*ast.BasicLit)(nil),
(*ast.CompositeLit)(nil),
}
insp.Preorder(filter, func(node ast.Node) {
switch n := node.(type) {
case *ast.CallExpr:
selectorExpr, ok := n.Fun.(*ast.SelectorExpr)
if !ok {
return
}
switch selectorExpr.Sel.Name {
case "WriteHeader":
if !lookupFlag(pass, HTTPStatusCodeFlag) {
return
}
if basicLit := getBasicLitFromArgs(n.Args, 1, 0, token.INT); basicLit != nil {
checkHTTPStatusCode(pass, basicLit)
}
case "NewRequest":
if !lookupFlag(pass, HTTPMethodFlag) {
return
}
if basicLit := getBasicLitFromArgs(n.Args, 3, 0, token.STRING); basicLit != nil {
checkHTTPMethod(pass, basicLit)
}
case "NewRequestWithContext":
if !lookupFlag(pass, HTTPMethodFlag) {
return
}
if basicLit := getBasicLitFromArgs(n.Args, 4, 1, token.STRING); basicLit != nil {
checkHTTPMethod(pass, basicLit)
}
}
case *ast.BasicLit:
currentVal := getBasicLitValue(n)
if lookupFlag(pass, TimeWeekdayFlag) {
checkTimeWeekday(pass, n.Pos(), currentVal)
}
if lookupFlag(pass, TimeMonthFlag) {
checkTimeMonth(pass, n.Pos(), currentVal)
}
if lookupFlag(pass, TimeLayoutFlag) {
checkTimeLayout(pass, n.Pos(), currentVal)
}
if lookupFlag(pass, CryptoHashFlag) {
checkCryptoHash(pass, n.Pos(), currentVal)
}
if lookupFlag(pass, DefaultRPCPathFlag) {
checkDefaultRPCPath(pass, n.Pos(), currentVal)
}
case *ast.CompositeLit:
selectorExpr, ok := n.Type.(*ast.SelectorExpr)
if !ok {
return
}
ident, ok := selectorExpr.X.(*ast.Ident)
if !ok {
return
}
if ident.Name == "http" {
switch selectorExpr.Sel.Name {
case "Request":
if !lookupFlag(pass, HTTPMethodFlag) {
return
}
if basicLit := getBasicLitFromElts(n.Elts, "Method"); basicLit != nil {
checkHTTPMethod(pass, basicLit)
}
case "Response":
if !lookupFlag(pass, HTTPStatusCodeFlag) {
return
}
if basicLit := getBasicLitFromElts(n.Elts, "StatusCode"); basicLit != nil {
checkHTTPStatusCode(pass, basicLit)
}
}
}
}
})
return nil, nil
}
func lookupFlag(pass *analysis.Pass, name string) bool {
return pass.Analyzer.Flags.Lookup(name).Value.(flag.Getter).Get().(bool)
}
func checkHTTPMethod(pass *analysis.Pass, basicLit *ast.BasicLit) {
currentVal := getBasicLitValue(basicLit)
if newVal, ok := httpMethod[currentVal]; ok {
report(pass, basicLit.Pos(), newVal, currentVal)
}
}
func checkHTTPStatusCode(pass *analysis.Pass, basicLit *ast.BasicLit) {
currentVal := getBasicLitValue(basicLit)
if newVal, ok := httpStatusCode[currentVal]; ok {
report(pass, basicLit.Pos(), newVal, currentVal)
}
}
func checkTimeWeekday(pass *analysis.Pass, pos token.Pos, currentVal string) {
if newVal, ok := timeWeekday[currentVal]; ok {
report(pass, pos, newVal, currentVal)
}
}
func checkTimeMonth(pass *analysis.Pass, pos token.Pos, currentVal string) {
if newVal, ok := timeMonth[currentVal]; ok {
report(pass, pos, newVal, currentVal)
}
}
func checkTimeLayout(pass *analysis.Pass, pos token.Pos, currentVal string) {
if newVal, ok := timeLayout[currentVal]; ok {
report(pass, pos, newVal, currentVal)
}
}
func checkCryptoHash(pass *analysis.Pass, pos token.Pos, currentVal string) {
if newVal, ok := cryptoHash[currentVal]; ok {
report(pass, pos, newVal, currentVal)
}
}
func checkDefaultRPCPath(pass *analysis.Pass, pos token.Pos, currentVal string) {
if newVal, ok := defaultRPCPath[currentVal]; ok {
report(pass, pos, newVal, currentVal)
}
}
// getBasicLitFromArgs gets the *ast.BasicLit of a function argument.
//
// Arguments:
// - count - expected number of argument in function
// - idx - index of the argument to get the *ast.BasicLit
// - typ - argument type
func getBasicLitFromArgs(args []ast.Expr, count, idx int, typ token.Token) *ast.BasicLit {
if len(args) != count {
return nil
}
basicLit, ok := args[idx].(*ast.BasicLit)
if !ok {
return nil
}
if basicLit.Kind != typ {
return nil
}
return basicLit
}
// getBasicLitFromElts gets the *ast.BasicLit of a struct elements.
//
// Arguments:
// - key: name of key in struct
func getBasicLitFromElts(elts []ast.Expr, key string) *ast.BasicLit {
for _, e := range elts {
expr, ok := e.(*ast.KeyValueExpr)
if !ok {
continue
}
ident, ok := expr.Key.(*ast.Ident)
if !ok {
continue
}
if ident.Name != key {
continue
}
if basicLit, ok := expr.Value.(*ast.BasicLit); ok {
return basicLit
}
}
return nil
}
func getBasicLitValue(basicLit *ast.BasicLit) string {
return strings.Trim(basicLit.Value, "\"")
}
func report(pass *analysis.Pass, pos token.Pos, newVal, currentVal string) {
pass.Reportf(pos, `%q can be replaced by %s`, currentVal, newVal)
}