-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
232 lines (191 loc) · 4.23 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
package main
import (
"encoding/json"
"flag"
"fmt"
"go/ast"
"go/parser"
"go/token"
"math"
"os"
"path/filepath"
"sort"
"text/tabwriter"
)
// Reports is a collection of Report.
type Reports []Report
// Report contains statistics for single function.
type Report struct {
Path string `json:"path"`
Line int `json:"line"`
Name string `json:"name"`
Assignment int `json:"assignment"`
Branch int `json:"branch"`
Condition int `json:"condition"`
Score int `json:"score"`
}
func main() {
var (
path string
format string
sort bool
noTest bool
reports Reports
)
flag.StringVar(&path, "path", "", "Path to file")
flag.StringVar(&format, "format", "table", "Output format")
flag.BoolVar(&sort, "sort", false, "Sort by score")
flag.BoolVar(&noTest, "no-test", false, "Skip *_test.go files")
flag.Parse()
if path == "" {
flag.PrintDefaults()
os.Exit(1)
}
fileList := listFiles(path)
fileSet := token.NewFileSet()
for _, path := range fileList {
node, err := parser.ParseFile(fileSet, path, nil, 0)
if err != nil {
continue
}
if noTest && isTest(path) {
continue
}
reports = append(reports, reportFile(fileSet, node)...)
}
if sort {
reports.Sort()
}
switch format {
case "summary":
reports.renderSummary()
case "table":
reports.renderTable()
case "json":
reports.renderJSON()
case "raw":
reports.renderRaw()
default:
panic("unknown format.")
}
}
func listFiles(path string) []string {
var fileList []string
fileInfo, _ := os.Stat(path)
appendAbsPath := func(p string) {
p, _ = filepath.Abs(p)
fileList = append(fileList, p)
}
if fileInfo.IsDir() {
filepath.Walk(path, func(p string, f os.FileInfo, err error) error {
if filepath.Ext(f.Name()) == ".go" {
appendAbsPath(p)
}
return nil
})
} else {
appendAbsPath(fileInfo.Name())
}
return fileList
}
func reportFile(fset *token.FileSet, n ast.Node) Reports {
var reports Reports
ast.Inspect(n, func(n ast.Node) bool {
if fn, ok := n.(*ast.FuncDecl); ok {
report := Report{
Path: fset.File(fn.Pos()).Name(),
Line: fset.Position(fn.Pos()).Line,
Name: fn.Name.Name,
}
ast.Inspect(n, func(n ast.Node) bool {
reportNode(&report, n)
return true
})
report.Calc()
reports = append(reports, report)
return false
}
return true
})
return reports
}
func reportNode(report *Report, n ast.Node) {
switch n := n.(type) {
case *ast.AssignStmt, *ast.IncDecStmt:
report.Assignment++
case *ast.CallExpr:
report.Branch++
case *ast.IfStmt:
if n.Else != nil {
report.Condition++
}
case *ast.BinaryExpr, *ast.CaseClause:
report.Condition++
}
}
func (report Report) String() string {
return fmt.Sprintf(
"%s:%d\t%s\t%d\t%d\t%d\t%d",
report.Path,
report.Line,
report.Name,
report.Score,
report.Assignment,
report.Branch,
report.Condition,
)
}
// Calc updates Score value.
func (report *Report) Calc() {
a := math.Pow(float64(report.Assignment), 2)
b := math.Pow(float64(report.Branch), 2)
c := math.Pow(float64(report.Condition), 2)
report.Score = int(math.Sqrt(a + b + c))
}
func (reports Reports) renderSummary() {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
defer w.Flush()
a, b, c := 0, 0, 0
for _, report := range reports {
a = a + report.Assignment
b = b + report.Branch
c = c + report.Condition
}
fmt.Fprintln(w, "\tA\tB\tC")
fmt.Fprintf(w, "%s\t%d\t%d\t%d\n", "Project summary:", a, b, c)
}
func (reports Reports) renderTable() {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
defer w.Flush()
fmt.Fprintln(w, "Source\tFunc\tScore\tA\tB\tC")
for _, report := range reports {
fmt.Fprintln(w, report.String())
}
}
func (reports Reports) renderJSON() {
bytes, err := json.Marshal(reports)
if err != nil {
fmt.Println(err)
}
os.Stdout.Write(bytes)
}
func (reports Reports) renderRaw() {
for _, report := range reports {
fmt.Printf(
"%s\t%d\t%s\t%d\n",
report.Path,
report.Line,
report.Name,
report.Score,
)
}
}
func (reports Reports) Sort() {
sort.Slice(reports, func(i, j int) bool {
return reports[i].Score > reports[j].Score
})
}
func isTest(p string) bool {
match, err := filepath.Match("*_test.go", filepath.Base(p))
return match && err == nil
}