/
analysis.go
65 lines (54 loc) · 1.54 KB
/
analysis.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
package analysis
import (
"go/ast"
"os"
"path/filepath"
"github.com/knsh14/go-conditional-complexity/complexity"
"github.com/knsh14/go-conditional-complexity/result"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
const doc = `checks conditional complexity
conditional complexity is index for how function is complex and hard to understand.
`
var threshold int
var Analyzer = &analysis.Analyzer{
Name: "conditionalcomplexity",
Doc: doc,
Run: run,
Requires: []*analysis.Analyzer{inspect.Analyzer},
}
func init() {
Analyzer.Flags.IntVar(&threshold, "max", 0, "threshold complexity to notice")
}
func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{
(*ast.FuncDecl)(nil),
(*ast.FuncLit)(nil),
}
inspect.Preorder(nodeFilter, func(n ast.Node) {
count, err := complexity.Count(n)
if err != nil {
pass.Reportf(n.Pos(), "failed to count: %s", err.Error())
return
}
if threshold < count {
fn := pass.Fset.PositionFor(n.Pos(), false).Filename
wd, err := os.Getwd()
if err != nil {
pass.Reportf(n.Pos(), "failed to get working dir: %s", err.Error())
return
}
rel, err := filepath.Rel(wd, fn)
if err != nil {
pass.Reportf(n.Pos(), "failed to relative path from %s to %s: %s", wd, fn, err.Error())
return
}
m := result.New(pass.Fset, rel, n, count)
pass.Reportf(n.Pos(), m.AnalysisResult())
}
})
return nil, nil
}