Skip to content

Commit

Permalink
Enable directive for the Analyzer (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
uudashr committed Sep 7, 2023
1 parent 667cb69 commit abed129
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -207,6 +207,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.
14 changes: 10 additions & 4 deletions gocognit.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,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)

fnName := funcName(fnDecl)
fnComplexity := Complexity(fnDecl)
d := parseDirective(funcDecl.Doc)
if d.Ignore {
return
}

fnName := funcName(funcDecl)

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)
}
})

Expand Down
9 changes: 9 additions & 0 deletions testdata/src/a/a.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,12 @@ func MyFunc2(a bool) { // want "cognitive complexity 2 of func MyFunc2 is high \

x()
} // total complexity = 2

//gocognit:ignore
func IgnoreMe(name string) bool {
if name == "me" { // +1
return true
}

return false
} // total complexity = 1
9 changes: 9 additions & 0 deletions testdata/src/b/b.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,12 @@ func MyFunc2(a bool) {

x()
} // total complexity = 2

//gocognit:ignore
func IgnoreMe(name string) bool {
if name == "me" { // +1
return true
}

return false
} // total complexity = 1

0 comments on commit abed129

Please sign in to comment.