forked from stathat/splint
/
splint.go
215 lines (184 loc) · 5.02 KB
/
splint.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
// Copyright 2011 Numrotron Inc.
// Use of this source code is governed by an MIT-style license
// that can be found in the LICENSE file.
//
// Developed at www.stathat.com by Patrick Crosby
// Contact us on twitter with any questions: twitter.com/stat_hat
// splint is a little Go application to analyze Go source files. It finds any functions that are
// too long or have too many parameters or results.
package main
import (
"encoding/json"
"flag"
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"path"
)
var statementThreshold = flag.Int("s", 30, "function statement count threshold")
var paramThreshold = flag.Int("p", 5, "parameter list length threshold")
var resultThreshold = flag.Int("r", 5, "result list length threshold")
var outputJSON = flag.Bool("j", false, "output results as json")
var ignoreTestFiles = flag.Bool("i", true, "ignore test files")
type Parser struct {
filename string
first bool
summary *Summary
}
type Offender struct {
Filename string
Function string
Count int
}
type Summary struct {
Statement []*Offender
Param []*Offender
Result []*Offender
// redundant, but using these for easy json output
NumAboveStatementThreshold int
NumAboveParamThreshold int
NumAboveResultThreshold int
}
func (s *Summary) addStatement(filename, function string, count int) {
s.Statement = append(s.Statement, &Offender{filename, function, count})
s.NumAboveStatementThreshold++
}
func (s *Summary) addParam(filename, function string, count int) {
s.Param = append(s.Param, &Offender{filename, function, count})
s.NumAboveParamThreshold++
}
func (s *Summary) addResult(filename, function string, count int) {
s.Result = append(s.Result, &Offender{filename, function, count})
s.NumAboveResultThreshold++
}
func NewParser(filename string, summary *Summary) *Parser {
return &Parser{filename, true, summary}
}
func statementCount(n ast.Node) int {
total := 0
counter := func(node ast.Node) bool {
switch node.(type) {
case ast.Stmt:
total += 1
}
return true
}
ast.Inspect(n, counter)
return total
}
func (p *Parser) outputFilename() {
if *outputJSON {
return
}
if p.first {
fmt.Printf("\n%s\n", p.filename)
p.first = false
}
}
func (p *Parser) checkFuncLength(x *ast.FuncDecl) {
numStatements := statementCount(x)
if numStatements <= *statementThreshold {
return
}
p.summary.addStatement(p.filename, x.Name.String(), numStatements)
if *outputJSON == false {
p.outputFilename()
fmt.Printf("function %s too long: %d\n", x.Name, numStatements)
}
}
func (p *Parser) checkParamCount(x *ast.FuncDecl) {
numFields := x.Type.Params.NumFields()
if numFields <= *paramThreshold {
return
}
p.summary.addParam(p.filename, x.Name.String(), numFields)
if *outputJSON == false {
p.outputFilename()
fmt.Printf("function %s has too many params: %d\n", x.Name, numFields)
}
}
func (p *Parser) checkResultCount(x *ast.FuncDecl) {
numResults := x.Type.Results.NumFields()
if numResults <= *resultThreshold {
return
}
p.summary.addResult(p.filename, x.Name.String(), numResults)
if *outputJSON == false {
p.outputFilename()
fmt.Printf("function %s has too many results: %d\n", x.Name, numResults)
}
}
func (p *Parser) examineFunc(x *ast.FuncDecl) {
p.checkFuncLength(x)
p.checkParamCount(x)
p.checkResultCount(x)
}
func (p *Parser) examineDecls(tree *ast.File) {
for _, v := range tree.Decls {
switch x := v.(type) {
case *ast.FuncDecl:
p.examineFunc(x)
}
}
}
func (p *Parser) Parse() {
fileset := token.NewFileSet()
tree, err := parser.ParseFile(fileset, p.filename, nil, 0)
if err != nil {
fmt.Printf("error parsing %s: %s\n", p.filename, err)
return
}
p.examineDecls(tree)
}
func isTestFile(filename string) bool {
base := path.Base(filename)
match, err := path.Match("*_test.go", base)
if err != nil {
fmt.Println("match error:", err)
return false
}
return match
}
func parseFile(filename string, summary *Summary) {
if *ignoreTestFiles && isTestFile(filename) {
return
}
parser := NewParser(filename, summary)
parser.Parse()
}
func main() {
flag.Parse()
args := flag.Args()
if len(args) == 0 {
fmt.Println("Usage: splint [options] <go file>...")
flag.PrintDefaults()
os.Exit(1)
}
summary := new(Summary)
for _, v := range args {
parseFile(v, summary)
}
if *outputJSON {
/*
buf := new(bytes.Buffer)
encoder := json.NewEncoder(buf)
err := encoder.Encode(summary)
if err != nil {
fmt.Println("json encode error:", err)
}
fmt.Println(string(buf.Bytes()))
*/
data, err := json.MarshalIndent(summary, "", "\t")
if err != nil {
fmt.Println("json encode error:", err)
}
fmt.Println(string(data))
} else {
fmt.Println()
fmt.Println("Number of functions above statement threshold:", summary.NumAboveStatementThreshold)
fmt.Println("Number of functions above param threshold:", summary.NumAboveParamThreshold)
fmt.Println("Number of functions above result threshold:", summary.NumAboveResultThreshold)
}
}