aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/uudashr
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/uudashr')
-rw-r--r--vendor/github.com/uudashr/gocognit/README.md56
-rw-r--r--vendor/github.com/uudashr/gocognit/gocognit.go37
2 files changed, 78 insertions, 15 deletions
diff --git a/vendor/github.com/uudashr/gocognit/README.md b/vendor/github.com/uudashr/gocognit/README.md
index 1e028c789..57f31cf74 100644
--- a/vendor/github.com/uudashr/gocognit/README.md
+++ b/vendor/github.com/uudashr/gocognit/README.md
@@ -1,6 +1,6 @@
[![GoDoc](https://godoc.org/github.com/uudashr/gocognit?status.svg)](https://godoc.org/github.com/uudashr/gocognit)
# Gocognit
-Gocognit calculates cognitive complexities of functions in Go source code. A measurement of how hard does the code is intuitively to understand.
+Gocognit calculates cognitive complexities of functions (and methods) in Go source code. A measurement of how hard does the code is intuitively to understand.
## Understanding the complexity
@@ -37,10 +37,10 @@ func GetWords(number int) string {
As you see above codes are the same, but the second code are easier to understand, that is why the cognitive complexity score are lower compare to the first one.
-## Comparison with cyclometic complexity
+## Comparison with cyclomatic complexity
### Example 1
-#### Cyclometic complexity
+#### Cyclomatic complexity
```go
func GetWords(number int) string { // +1
switch number {
@@ -160,16 +160,40 @@ $ go get github.com/uudashr/gocognit/cmd/gocognit
```
$ gocognit
Calculate cognitive complexities of Go functions.
+
Usage:
- gocognit [flags] <Go file or directory> ...
+
+ gocognit [<flag> ...] <Go file or directory> ...
+
Flags:
- -over N show functions with complexity > N only and
- return exit code 1 if the set is non-empty
- -top N show the top N most complex functions only
- -avg show the average complexity over all functions,
- not depending on whether -over or -top are set
-The output fields for each line are:
-<complexity> <package> <function> <file:row:column>
+
+ -over N show functions with complexity > N only
+ and return exit code 1 if the output is non-empty
+ -top N show the top N most complex functions only
+ -avg show the average complexity over all functions,
+ not depending on whether -over or -top are set
+ -json encode the output as JSON
+ -f format string the format to use
+ (default "{{.PkgName}}.{{.FuncName}}:{{.Complexity}}:{{.Pos}}")
+
+The (default) output fields for each line are:
+
+ <complexity> <package> <function> <file:row:column>
+
+The (default) output fields for each line are:
+
+ {{.Complexity}} {{.PkgName}} {{.FuncName}} {{.Pos}}
+
+or equal to <complexity> <package> <function> <file:row:column>
+
+The struct being passed to the template is:
+
+ type Stat struct {
+ PkgName string
+ FuncName string
+ Complexity int
+ Pos token.Position
+ }
```
Examples:
@@ -180,6 +204,7 @@ $ gocognit main.go
$ gocognit -top 10 src/
$ gocognit -over 25 docker
$ gocognit -avg .
+$ gocognit -ignore "_test|testdata" .
```
The output fields for each line are:
@@ -187,6 +212,15 @@ The output fields for each line are:
<complexity> <package> <function> <file:row:column>
```
+## Ignore individual functions
+Ignore individual functions by specifying `gocognit:ignore` directive.
+```go
+//gocognit:ignore
+func IgnoreMe() {
+ // ...
+}
+```
+
## Related project
- [Gocyclo](https://github.com/fzipp/gocyclo) where the code are based on.
- [Cognitive Complexity: A new way of measuring understandability](https://www.sonarsource.com/docs/CognitiveComplexity.pdf) white paper by G. Ann Campbell. \ No newline at end of file
diff --git a/vendor/github.com/uudashr/gocognit/gocognit.go b/vendor/github.com/uudashr/gocognit/gocognit.go
index 2fe22abc4..2bba2eb4f 100644
--- a/vendor/github.com/uudashr/gocognit/gocognit.go
+++ b/vendor/github.com/uudashr/gocognit/gocognit.go
@@ -26,6 +26,11 @@ func (s Stat) String() string {
func ComplexityStats(f *ast.File, fset *token.FileSet, stats []Stat) []Stat {
for _, decl := range f.Decls {
if fn, ok := decl.(*ast.FuncDecl); ok {
+ d := parseDirective(fn.Doc)
+ if d.Ignore {
+ continue
+ }
+
stats = append(stats, Stat{
PkgName: f.Name.Name,
FuncName: funcName(fn),
@@ -37,6 +42,24 @@ func ComplexityStats(f *ast.File, fset *token.FileSet, stats []Stat) []Stat {
return stats
}
+type directive struct {
+ Ignore bool
+}
+
+func parseDirective(doc *ast.CommentGroup) directive {
+ if doc == nil {
+ return directive{}
+ }
+
+ for _, c := range doc.List {
+ if c.Text == "//gocognit:ignore" {
+ return directive{Ignore: true}
+ }
+ }
+
+ return directive{}
+}
+
// funcName returns the name representation of a function or method:
// "(Type).Name" for methods or simply "Name" for functions.
func funcName(fn *ast.FuncDecl) string {
@@ -356,13 +379,19 @@ func run(pass *analysis.Pass) (interface{}, error) {
(*ast.FuncDecl)(nil),
}
inspect.Preorder(nodeFilter, func(n ast.Node) {
- fnDecl := n.(*ast.FuncDecl)
+ funcDecl := n.(*ast.FuncDecl)
+
+ d := parseDirective(funcDecl.Doc)
+ if d.Ignore {
+ return
+ }
+
+ fnName := funcName(funcDecl)
- fnName := funcName(fnDecl)
- fnComplexity := Complexity(fnDecl)
+ fnComplexity := Complexity(funcDecl)
if fnComplexity > over {
- pass.Reportf(fnDecl.Pos(), "cognitive complexity %d of func %s is high (> %d)", fnComplexity, fnName, over)
+ pass.Reportf(funcDecl.Pos(), "cognitive complexity %d of func %s is high (> %d)", fnComplexity, fnName, over)
}
})