forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
323 lines (263 loc) · 7.17 KB
/
main.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"io"
"os"
"path/filepath"
"sort"
"strings"
"github.com/fatih/color"
)
type warning struct {
format string
vars []interface{}
token.Position
}
type warningPrinter struct {
warnings []warning
}
func (w warningPrinter) print(writer io.Writer) {
w.sortWarnings()
for _, warning := range w.warnings {
coloredVars := make([]interface{}, len(warning.vars))
for i, v := range warning.vars {
coloredVars[i] = color.CyanString(v.(string))
}
message := fmt.Sprintf(warning.format, coloredVars...)
fmt.Printf(
"%s %s %s\n",
color.MagentaString(warning.Position.Filename),
color.MagentaString(fmt.Sprintf("+%d", warning.Position.Line)),
message)
}
}
func (w warningPrinter) sortWarnings() {
sort.Slice(w.warnings, func(i int, j int) bool {
if w.warnings[i].Position.Filename < w.warnings[j].Position.Filename {
return true
}
if w.warnings[i].Position.Filename > w.warnings[j].Position.Filename {
return false
}
if w.warnings[i].Position.Line < w.warnings[j].Position.Line {
return true
}
if w.warnings[i].Position.Line > w.warnings[j].Position.Line {
return false
}
iMessage := fmt.Sprintf(w.warnings[i].format, w.warnings[i].vars...)
jMessage := fmt.Sprintf(w.warnings[j].format, w.warnings[j].vars...)
return iMessage < jMessage
})
}
type visitor struct {
fileSet *token.FileSet
lastConstSpec string
lastFuncDecl string
lastReceiverFunc string
lastReceiver string
lastVarSpec string
typeSpecs []string
warnings []warning
previousPass *visitor
}
func (v *visitor) Visit(node ast.Node) ast.Visitor {
switch typedNode := node.(type) {
case *ast.File:
return v
case *ast.GenDecl:
if typedNode.Tok == token.CONST {
v.checkConst(typedNode)
} else if typedNode.Tok == token.VAR {
v.checkVar(typedNode)
}
return v
case *ast.FuncDecl:
v.checkFunc(typedNode)
case *ast.TypeSpec:
v.checkType(typedNode)
}
return nil
}
func (v *visitor) addWarning(pos token.Pos, format string, vars ...interface{}) {
v.warnings = append(v.warnings, warning{
format: format,
vars: vars,
Position: v.fileSet.Position(pos),
})
}
func (v *visitor) checkConst(node *ast.GenDecl) {
constName := node.Specs[0].(*ast.ValueSpec).Names[0].Name
if v.lastFuncDecl != "" {
v.addWarning(node.Pos(), "constant %s defined after a function declaration", constName)
}
if len(v.typeSpecs) != 0 {
v.addWarning(node.Pos(), "constant %s defined after a type declaration", constName)
}
if v.lastVarSpec != "" {
v.addWarning(node.Pos(), "constant %s defined after a variable declaration", constName)
}
if strings.Compare(constName, v.lastConstSpec) == -1 {
v.addWarning(node.Pos(), "constant %s defined after constant %s", constName, v.lastConstSpec)
}
v.lastConstSpec = constName
}
func (v *visitor) checkFunc(node *ast.FuncDecl) {
if node.Recv != nil {
v.checkFuncWithReceiver(node)
} else {
funcName := node.Name.Name
if funcName == "Execute" || strings.HasPrefix(funcName, "New") {
return
}
if strings.Compare(funcName, v.lastFuncDecl) == -1 {
v.addWarning(node.Pos(), "function %s defined after function %s", funcName, v.lastFuncDecl)
}
v.lastFuncDecl = funcName
}
}
func (v *visitor) checkFuncWithReceiver(node *ast.FuncDecl) {
funcName := node.Name.Name
var receiver string
switch typedType := node.Recv.List[0].Type.(type) {
case *ast.Ident:
receiver = typedType.Name
case *ast.StarExpr:
receiver = typedType.X.(*ast.Ident).Name
}
if v.lastFuncDecl != "" {
v.addWarning(node.Pos(), "method %s.%s defined after function %s", receiver, funcName, v.lastFuncDecl)
}
if len(v.typeSpecs) > 0 {
lastTypeSpec := v.typeSpecs[len(v.typeSpecs)-1]
if v.typeDefinedInFile(receiver) && receiver != lastTypeSpec {
v.addWarning(node.Pos(), "method %s.%s should be defined immediately after type %s", receiver, funcName, receiver)
}
}
if receiver == v.lastReceiver {
if strings.Compare(funcName, v.lastReceiverFunc) == -1 {
v.addWarning(node.Pos(), "method %s.%s defined after method %s.%s", receiver, funcName, receiver, v.lastReceiverFunc)
}
}
v.lastReceiver = receiver
v.lastReceiverFunc = funcName
}
func (v *visitor) checkType(node *ast.TypeSpec) {
typeName := node.Name.Name
if v.lastFuncDecl != "" {
v.addWarning(node.Pos(), "type declaration %s defined after a function declaration", typeName)
}
v.typeSpecs = append(v.typeSpecs, typeName)
}
func (v *visitor) checkVar(node *ast.GenDecl) {
varName := node.Specs[0].(*ast.ValueSpec).Names[0].Name
if v.lastFuncDecl != "" {
v.addWarning(node.Pos(), "variable %s defined after a function declaration", varName)
}
if len(v.typeSpecs) != 0 {
v.addWarning(node.Pos(), "variable %s defined after a type declaration", varName)
}
if strings.Compare(varName, v.lastVarSpec) == -1 {
v.addWarning(node.Pos(), "variable %s defined after variable %s", varName, v.lastVarSpec)
}
v.lastVarSpec = varName
}
func (v *visitor) typeDefinedInFile(typeName string) bool {
if v.previousPass == nil {
return true
}
for _, definedTypeName := range v.previousPass.typeSpecs {
if definedTypeName == typeName {
return true
}
}
return false
}
func check(fileSet *token.FileSet, path string) ([]warning, error) {
stat, err := os.Stat(path)
if err != nil {
return nil, err
}
if stat.IsDir() {
return checkDir(fileSet, path)
} else {
return checkFile(fileSet, path)
}
}
func checkDir(fileSet *token.FileSet, path string) ([]warning, error) {
var warnings []warning
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
return nil
}
if shouldSkipDir(path) {
return filepath.SkipDir
}
packages, err := parser.ParseDir(fileSet, path, shouldParseFile, 0)
if err != nil {
return err
}
for _, packag := range packages {
for _, file := range packag.Files {
warnings = append(warnings, walkFile(fileSet, file)...)
}
}
return nil
})
return warnings, err
}
func checkFile(fileSet *token.FileSet, path string) ([]warning, error) {
file, err := parser.ParseFile(fileSet, path, nil, 0)
if err != nil {
return nil, err
}
return walkFile(fileSet, file), nil
}
func main() {
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Usage: %s [--] [FILE or DIRECTORY]...\n", os.Args[0])
os.Exit(1)
}
var allWarnings []warning
args := os.Args[1:]
if args[0] == "--" {
args = args[1:]
}
fileSet := token.NewFileSet()
for _, arg := range args {
warnings, err := check(fileSet, arg)
if err != nil {
panic(err)
}
allWarnings = append(allWarnings, warnings...)
}
warningPrinter := warningPrinter{
warnings: allWarnings,
}
warningPrinter.print(os.Stdout)
if len(allWarnings) > 0 {
os.Exit(1)
}
}
func shouldParseFile(info os.FileInfo) bool {
return !strings.HasSuffix(info.Name(), "_test.go")
}
func shouldSkipDir(path string) bool {
base := filepath.Base(path)
return base == "vendor" || base == ".git" || strings.HasSuffix(base, "fakes")
}
func walkFile(fileSet *token.FileSet, file *ast.File) []warning {
firstPass := visitor{
fileSet: fileSet,
}
ast.Walk(&firstPass, file)
v := visitor{
fileSet: fileSet,
previousPass: &firstPass,
}
ast.Walk(&v, file)
return v.warnings
}