Skip to content

Commit

Permalink
Change exclude string to array of strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krivak committed Jan 15, 2021
1 parent c85e9f3 commit 9df156f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
24 changes: 17 additions & 7 deletions getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func newParsedFile(file *ast.File, fset *token.FileSet) (*parsedFile, error) {
}

// getComments extracts comments from a file.
func (pf *parsedFile) getComments(scope Scope, exclude *regexp.Regexp) []comment {
func (pf *parsedFile) getComments(scope Scope, exclude []*regexp.Regexp) []comment {
if len(pf.file.Comments) == 0 {
return nil
}
Expand Down Expand Up @@ -79,7 +79,7 @@ func (pf *parsedFile) getComments(scope Scope, exclude *regexp.Regexp) []comment

// getBlockComments gets comments from the inside of top level blocks:
// var (...), const (...).
func (pf *parsedFile) getBlockComments(exclude *regexp.Regexp) []comment {
func (pf *parsedFile) getBlockComments(exclude []*regexp.Regexp) []comment {
var comments []comment
for _, decl := range pf.file.Decls {
d, ok := decl.(*ast.GenDecl)
Expand Down Expand Up @@ -118,7 +118,7 @@ func (pf *parsedFile) getBlockComments(exclude *regexp.Regexp) []comment {
}

// getTopLevelComments gets all top level comments.
func (pf *parsedFile) getTopLevelComments(exclude *regexp.Regexp) []comment {
func (pf *parsedFile) getTopLevelComments(exclude []*regexp.Regexp) []comment {
var comments []comment // nolint: prealloc
for _, c := range pf.file.Comments {
if c == nil || len(c.List) == 0 {
Expand All @@ -139,7 +139,7 @@ func (pf *parsedFile) getTopLevelComments(exclude *regexp.Regexp) []comment {
}

// getDeclarationComments gets top level declaration comments.
func (pf *parsedFile) getDeclarationComments(exclude *regexp.Regexp) []comment {
func (pf *parsedFile) getDeclarationComments(exclude []*regexp.Regexp) []comment {
var comments []comment // nolint: prealloc
for _, decl := range pf.file.Decls {
var cg *ast.CommentGroup
Expand All @@ -166,7 +166,7 @@ func (pf *parsedFile) getDeclarationComments(exclude *regexp.Regexp) []comment {
}

// getAllComments gets every single comment from the file.
func (pf *parsedFile) getAllComments(exclude *regexp.Regexp) []comment {
func (pf *parsedFile) getAllComments(exclude []*regexp.Regexp) []comment {
var comments []comment //nolint: prealloc
for _, c := range pf.file.Comments {
if c == nil || len(c.List) == 0 {
Expand All @@ -188,7 +188,7 @@ func (pf *parsedFile) getAllComments(exclude *regexp.Regexp) []comment {
// special lines (e.g., tags or indented code examples), they are replaced
// with `specialReplacer` to skip checks for it.
// The result can be multiline.
func getText(comment *ast.CommentGroup, exclude *regexp.Regexp) (s string) {
func getText(comment *ast.CommentGroup, exclude []*regexp.Regexp) (s string) {
if len(comment.List) == 1 &&
strings.HasPrefix(comment.List[0].Text, "/*") &&
isSpecialBlock(comment.List[0].Text) {
Expand All @@ -211,7 +211,7 @@ func getText(comment *ast.CommentGroup, exclude *regexp.Regexp) (s string) {
if !isBlock {
line = strings.TrimPrefix(line, "//")
}
if exclude != nil && exclude.MatchString(line) {
if matchAny(line, exclude) {
s += specialReplacer + "\n"
continue
}
Expand Down Expand Up @@ -245,3 +245,13 @@ func setDecl(comments, decl []comment) {
}
}
}

// matchAny checks if string matches any of given regexps.
func matchAny(s string, rr []*regexp.Regexp) bool {
for _, re := range rr {
if re.MatchString(s) {
return true
}
}
return false
}
6 changes: 5 additions & 1 deletion getters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,11 @@ func TestGetText(t *testing.T) {
for _, tt := range testCases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
if text := getText(tt.comment, tt.exclude); text != tt.text {
var re []*regexp.Regexp
if tt.exclude != nil {
re = []*regexp.Regexp{tt.exclude}
}
if text := getText(tt.comment, re); text != tt.text {
t.Fatalf("Wrong text\n expected: '%s'\n got: '%s'", tt.text, text)
}
})
Expand Down
6 changes: 3 additions & 3 deletions godot.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func Run(file *ast.File, fset *token.FileSet, settings Settings) ([]Issue, error
return nil, fmt.Errorf("parse input file: %v", err)
}

var exclude *regexp.Regexp
if settings.Exclude != "" {
exclude, err = regexp.Compile(settings.Exclude)
exclude := make([]*regexp.Regexp, len(settings.Exclude))
for i := 0; i < len(settings.Exclude); i++ {
exclude[i], err = regexp.Compile(settings.Exclude[i])
if err != nil {
return nil, fmt.Errorf("invalid regexp: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions godot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// testExclude is a test regexp to exclude lines that starts with @ symbol.
const testExclude = "^ ?@"
var testExclude = []string{"^ ?@"}

func TestRun(t *testing.T) {
testFile := filepath.Join("testdata", "check", "main.go")
Expand All @@ -24,7 +24,7 @@ func TestRun(t *testing.T) {
// Test invalid regexp
_, err = Run(f, fset, Settings{
Scope: DeclScope,
Exclude: "[",
Exclude: []string{"["},
Period: true,
Capital: true,
})
Expand Down
2 changes: 1 addition & 1 deletion settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type Settings struct {
Scope Scope

// Regexp for excluding particular comment lines from check.
Exclude string
Exclude []string

// Check periods at the end of sentences.
Period bool
Expand Down

0 comments on commit 9df156f

Please sign in to comment.