-
Notifications
You must be signed in to change notification settings - Fork 3
/
maintidx.go
63 lines (51 loc) · 1.34 KB
/
maintidx.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
package maintidx
import (
"go/ast"
"go/token"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
const doc = "maintidx measures the maintainability index of each function."
var Analyzer = &analysis.Analyzer{
Name: "maintidx",
Doc: doc,
Run: run,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
}
var under int
func init() {
Analyzer.Flags.IntVar(&under, "under", 20, "show functions with maintainability index < N only.")
}
func run(pass *analysis.Pass) (interface{}, error) {
i := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{
(*ast.FuncDecl)(nil),
}
i.Preorder(nodeFilter, func(n ast.Node) {
switch n := n.(type) {
case *ast.FuncDecl:
v := analyze(n)
v.Coef.Cyc.Calc()
v.Coef.HalstVol.Calc()
v.calc(loc(pass.Fset, n))
if v.MaintIdx < under {
pass.Reportf(n.Pos(), "Function name: %v, Cyclomatic Complexity: %v, Halstead Volume: %0.2f, Maintainability Index: %v", n.Name, v.Coef.Cyc.Val, v.Coef.HalstVol.Val, v.MaintIdx)
}
}
})
return nil, nil
}
func analyze(n ast.Node) Visitor {
v := NewVisitor()
ast.Walk(v, n)
return *v
}
func loc(fs *token.FileSet, n *ast.FuncDecl) int {
f := fs.File(n.Pos())
startLine := f.Line(n.Pos())
endLine := f.Line(n.End())
return endLine - startLine + 1
}