Skip to content

Commit

Permalink
Support ignore directive (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
uudashr authored Sep 6, 2023
1 parent 4193923 commit bc9ca12
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/gocognit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
23 changes: 23 additions & 0 deletions gocognit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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 {
Expand Down

0 comments on commit bc9ca12

Please sign in to comment.