Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable directive for the Analyzer #33

Merged
merged 1 commit into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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