diff --git a/cmd/gocognit/main.go b/cmd/gocognit/main.go index dd82582..8855dbb 100644 --- a/cmd/gocognit/main.go +++ b/cmd/gocognit/main.go @@ -170,7 +170,7 @@ func isDir(filename string) bool { func analyzeFile(fname string, stats []gocognit.Stat) ([]gocognit.Stat, error) { fset := token.NewFileSet() - f, err := parser.ParseFile(fset, fname, nil, 0) + f, err := parser.ParseFile(fset, fname, nil, parser.ParseComments) if err != nil { return nil, err } diff --git a/gocognit.go b/gocognit.go index 2fe22ab..52d7030 100644 --- a/gocognit.go +++ b/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 {