aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/fzipp/gocyclo/complexity.go
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2021-02-22 20:37:25 +0100
committerDmitry Vyukov <dvyukov@google.com>2021-02-22 21:02:12 +0100
commitfcc6d71be2c3ce7d9305c04fc2e87af554571bac (patch)
treeb01dbb3d1e2988e28ea158d2d543d603ec0b9569 /vendor/github.com/fzipp/gocyclo/complexity.go
parent8f23c528ad5a943b9ffec5dcaf332fd0f614006e (diff)
go.mod: update golangci-lint to v1.37
Diffstat (limited to 'vendor/github.com/fzipp/gocyclo/complexity.go')
-rw-r--r--vendor/github.com/fzipp/gocyclo/complexity.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/vendor/github.com/fzipp/gocyclo/complexity.go b/vendor/github.com/fzipp/gocyclo/complexity.go
new file mode 100644
index 000000000..65f5077e8
--- /dev/null
+++ b/vendor/github.com/fzipp/gocyclo/complexity.go
@@ -0,0 +1,48 @@
+// Copyright 2020 Frederik Zipp. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Package gocyclo calculates the cyclomatic complexities of functions and
+// methods in Go source code.
+package gocyclo
+
+import (
+ "go/ast"
+ "go/token"
+)
+
+// Complexity calculates the cyclomatic complexity of a function.
+// The 'fn' node is either a *ast.FuncDecl or a *ast.FuncLit.
+func Complexity(fn ast.Node) int {
+ v := complexityVisitor{
+ complexity: 1,
+ }
+ ast.Walk(&v, fn)
+ return v.complexity
+}
+
+type complexityVisitor struct {
+ // complexity is the cyclomatic complexity
+ complexity int
+}
+
+// Visit implements the ast.Visitor interface.
+func (v *complexityVisitor) Visit(n ast.Node) ast.Visitor {
+ switch n := n.(type) {
+ case *ast.IfStmt, *ast.ForStmt, *ast.RangeStmt:
+ v.complexity++
+ case *ast.CaseClause:
+ if n.List != nil { // ignore default case
+ v.complexity++
+ }
+ case *ast.CommClause:
+ if n.Comm != nil { // ignore default case
+ v.complexity++
+ }
+ case *ast.BinaryExpr:
+ if n.Op == token.LAND || n.Op == token.LOR {
+ v.complexity++
+ }
+ }
+ return v
+}