woodpecker/vendor/github.com/yagipy/maintidx/maintidx.go
6543 56a854fe14
Update deps (#789)
* update github.com/docker/cli

* update github.com/docker/distribution

* update github.com/docker/docker

* update github.com/gin-gonic/gin

* update github.com/golang-jwt/jwt/v4

* update github.com/golangci/golangci-lint

* update github.com/gorilla/securecookie

* update github.com/mattn/go-sqlite3

* update github.com/moby/moby

* update github.com/prometheus/client_golang

* update github.com/xanzy/go-gitlab
2022-02-24 17:33:24 +01:00

63 lines
1.3 KiB
Go

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
}