diff options
Diffstat (limited to 'vendor/github.com/uudashr')
| -rw-r--r-- | vendor/github.com/uudashr/gocognit/README.md | 7 | ||||
| -rw-r--r-- | vendor/github.com/uudashr/gocognit/doc.go | 2 | ||||
| -rw-r--r-- | vendor/github.com/uudashr/gocognit/go.mod | 7 | ||||
| -rw-r--r-- | vendor/github.com/uudashr/gocognit/go.sum | 27 | ||||
| -rw-r--r-- | vendor/github.com/uudashr/gocognit/gocognit.go | 130 | ||||
| -rw-r--r-- | vendor/github.com/uudashr/gocognit/recv.go | 24 | ||||
| -rw-r--r-- | vendor/github.com/uudashr/gocognit/recv_pre118.go | 20 |
7 files changed, 181 insertions, 36 deletions
diff --git a/vendor/github.com/uudashr/gocognit/README.md b/vendor/github.com/uudashr/gocognit/README.md index 4a8846907..1e028c789 100644 --- a/vendor/github.com/uudashr/gocognit/README.md +++ b/vendor/github.com/uudashr/gocognit/README.md @@ -144,6 +144,13 @@ The following structures receive a nesting increment commensurate with their nes 3. `for` ## Installation + +``` +$ go install github.com/uudashr/gocognit/cmd/gocognit@latest +``` + +or + ``` $ go get github.com/uudashr/gocognit/cmd/gocognit ``` diff --git a/vendor/github.com/uudashr/gocognit/doc.go b/vendor/github.com/uudashr/gocognit/doc.go new file mode 100644 index 000000000..ae3d0a226 --- /dev/null +++ b/vendor/github.com/uudashr/gocognit/doc.go @@ -0,0 +1,2 @@ +// Package gocognit defines Analyzer other utilities to checks and calculate the complexity of function based on "cognitive complexity" methods. +package gocognit diff --git a/vendor/github.com/uudashr/gocognit/go.mod b/vendor/github.com/uudashr/gocognit/go.mod index 1a68d3880..128c73d28 100644 --- a/vendor/github.com/uudashr/gocognit/go.mod +++ b/vendor/github.com/uudashr/gocognit/go.mod @@ -1,3 +1,8 @@ module github.com/uudashr/gocognit -go 1.13 +go 1.16 + +require ( + golang.org/x/sys v0.0.0-20220702020025-31831981b65f // indirect + golang.org/x/tools v0.1.11 +) diff --git a/vendor/github.com/uudashr/gocognit/go.sum b/vendor/github.com/uudashr/gocognit/go.sum index e69de29bb..a2b3fa1c8 100644 --- a/vendor/github.com/uudashr/gocognit/go.sum +++ b/vendor/github.com/uudashr/gocognit/go.sum @@ -0,0 +1,27 @@ +github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220702020025-31831981b65f h1:xdsejrW/0Wf2diT5CPp3XmKUNbr7Xvw8kYilQ+6qjRY= +golang.org/x/sys v0.0.0-20220702020025-31831981b65f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.11 h1:loJ25fNOEhSXfHrpoGj91eCUThwdNX6u24rO1xnNteY= +golang.org/x/tools v0.1.11/go.mod h1:SgwaegtQh8clINPpECJMqnxLv9I09HLqnW3RMqW0CA4= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/vendor/github.com/uudashr/gocognit/gocognit.go b/vendor/github.com/uudashr/gocognit/gocognit.go index 11d5ee527..1d539ee78 100644 --- a/vendor/github.com/uudashr/gocognit/gocognit.go +++ b/vendor/github.com/uudashr/gocognit/gocognit.go @@ -4,6 +4,10 @@ import ( "fmt" "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" ) // Stat is statistic of the complexity. @@ -45,23 +49,12 @@ func funcName(fn *ast.FuncDecl) string { return fn.Name.Name } -// recvString returns a string representation of recv of the -// form "T", "*T", or "BADRECV" (if not a proper receiver type). -func recvString(recv ast.Expr) string { - switch t := recv.(type) { - case *ast.Ident: - return t.Name - case *ast.StarExpr: - return "*" + recvString(t.X) - } - return "BADRECV" -} - // Complexity calculates the cognitive complexity of a function. func Complexity(fn *ast.FuncDecl) int { v := complexityVisitor{ name: fn.Name, } + ast.Walk(&v, fn) return v.complexity } @@ -129,6 +122,8 @@ func (v *complexityVisitor) Visit(n ast.Node) ast.Visitor { return v.visitIfStmt(n) case *ast.SwitchStmt: return v.visitSwitchStmt(n) + case *ast.TypeSwitchStmt: + return v.visitTypeSwitchStmt(n) case *ast.SelectStmt: return v.visitSelectStmt(n) case *ast.ForStmt: @@ -150,38 +145,59 @@ func (v *complexityVisitor) Visit(n ast.Node) ast.Visitor { func (v *complexityVisitor) visitIfStmt(n *ast.IfStmt) ast.Visitor { v.incIfComplexity(n) - if n.Init != nil { - ast.Walk(v, n.Init) + if n := n.Init; n != nil { + ast.Walk(v, n) } ast.Walk(v, n.Cond) - v.incNesting() - ast.Walk(v, n.Body) - v.decNesting() + pure := !v.markedAsElseNode(n) // pure `if` statement, not an `else if` + if pure { + v.incNesting() + ast.Walk(v, n.Body) + v.decNesting() + } else { + ast.Walk(v, n.Body) + } if _, ok := n.Else.(*ast.BlockStmt); ok { v.incComplexity() - v.incNesting() ast.Walk(v, n.Else) - v.decNesting() } else if _, ok := n.Else.(*ast.IfStmt); ok { v.markAsElseNode(n.Else) ast.Walk(v, n.Else) } + return nil } func (v *complexityVisitor) visitSwitchStmt(n *ast.SwitchStmt) ast.Visitor { v.nestIncComplexity() - if n.Init != nil { - ast.Walk(v, n.Init) + if n := n.Init; n != nil { + ast.Walk(v, n) } - if n.Tag != nil { - ast.Walk(v, n.Tag) + if n := n.Tag; n != nil { + ast.Walk(v, n) + } + + v.incNesting() + ast.Walk(v, n.Body) + v.decNesting() + return nil +} + +func (v *complexityVisitor) visitTypeSwitchStmt(n *ast.TypeSwitchStmt) ast.Visitor { + v.nestIncComplexity() + + if n := n.Init; n != nil { + ast.Walk(v, n) + } + + if n := n.Assign; n != nil { + ast.Walk(v, n) } v.incNesting() @@ -202,16 +218,16 @@ func (v *complexityVisitor) visitSelectStmt(n *ast.SelectStmt) ast.Visitor { func (v *complexityVisitor) visitForStmt(n *ast.ForStmt) ast.Visitor { v.nestIncComplexity() - if n.Init != nil { - ast.Walk(v, n.Init) + if n := n.Init; n != nil { + ast.Walk(v, n) } - if n.Cond != nil { - ast.Walk(v, n.Cond) + if n := n.Cond; n != nil { + ast.Walk(v, n) } - if n.Post != nil { - ast.Walk(v, n.Post) + if n := n.Post; n != nil { + ast.Walk(v, n) } v.incNesting() @@ -223,12 +239,12 @@ func (v *complexityVisitor) visitForStmt(n *ast.ForStmt) ast.Visitor { func (v *complexityVisitor) visitRangeStmt(n *ast.RangeStmt) ast.Visitor { v.nestIncComplexity() - if n.Key != nil { - ast.Walk(v, n.Key) + if n := n.Key; n != nil { + ast.Walk(v, n) } - if n.Value != nil { - ast.Walk(v, n.Value) + if n := n.Value; n != nil { + ast.Walk(v, n) } ast.Walk(v, n.X) @@ -271,8 +287,10 @@ func (v *complexityVisitor) visitBinaryExpr(n *ast.BinaryExpr) ast.Visitor { } func (v *complexityVisitor) visitCallExpr(n *ast.CallExpr) ast.Visitor { - if name, ok := n.Fun.(*ast.Ident); ok { - if name.Obj == v.name.Obj && name.Name == v.name.Name { + if callIdent, ok := n.Fun.(*ast.Ident); ok { + obj, name := callIdent.Obj, callIdent.Name + if obj == v.name.Obj && name == v.name.Name { + // called by same function directly (direct recursion) v.incComplexity() } } @@ -311,3 +329,45 @@ func mergeBinaryOps(x []token.Token, op token.Token, y []token.Token) []token.To } return out } + +const Doc = `Find complex function using cognitive complexity calculation. + +The gocognit analysis reports functions or methods which the complexity is over +than the specified limit.` + +// Analyzer reports a diagnostic for every function or method which is +// too complex specified by its -over flag. +var Analyzer = &analysis.Analyzer{ + Name: "gocognit", + Doc: Doc, + Requires: []*analysis.Analyzer{inspect.Analyzer}, + Run: run, +} + +var ( + over int // -over flag +) + +func init() { + Analyzer.Flags.IntVar(&over, "over", over, "show functions with complexity > N only") +} + +func run(pass *analysis.Pass) (interface{}, error) { + inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) + + nodeFilter := []ast.Node{ + (*ast.FuncDecl)(nil), + } + inspect.Preorder(nodeFilter, func(n ast.Node) { + fnDecl := n.(*ast.FuncDecl) + + fnName := funcName(fnDecl) + fnComplexity := Complexity(fnDecl) + + if fnComplexity > over { + pass.Reportf(fnDecl.Pos(), "cognitive complexity %d of func %s is high (> %d)", fnComplexity, fnName, over) + } + }) + + return nil, nil +} diff --git a/vendor/github.com/uudashr/gocognit/recv.go b/vendor/github.com/uudashr/gocognit/recv.go new file mode 100644 index 000000000..2f20d843a --- /dev/null +++ b/vendor/github.com/uudashr/gocognit/recv.go @@ -0,0 +1,24 @@ +//go:build go1.18 +// +build go1.18 + +package gocognit + +import ( + "go/ast" +) + +// recvString returns a string representation of recv of the +// form "T", "*T", or "BADRECV" (if not a proper receiver type). +func recvString(recv ast.Expr) string { + switch t := recv.(type) { + case *ast.Ident: + return t.Name + case *ast.StarExpr: + return "*" + recvString(t.X) + case *ast.IndexExpr: + return recvString(t.X) + case *ast.IndexListExpr: + return recvString(t.X) + } + return "BADRECV" +} diff --git a/vendor/github.com/uudashr/gocognit/recv_pre118.go b/vendor/github.com/uudashr/gocognit/recv_pre118.go new file mode 100644 index 000000000..9e0ebfd82 --- /dev/null +++ b/vendor/github.com/uudashr/gocognit/recv_pre118.go @@ -0,0 +1,20 @@ +//go:build !go1.18 +// +build !go1.18 + +package gocognit + +import ( + "go/ast" +) + +// recvString returns a string representation of recv of the +// form "T", "*T", or "BADRECV" (if not a proper receiver type). +func recvString(recv ast.Expr) string { + switch t := recv.(type) { + case *ast.Ident: + return t.Name + case *ast.StarExpr: + return "*" + recvString(t.X) + } + return "BADRECV" +} |
