Skip to content

Commit

Permalink
Add Period setting.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krivak committed Oct 10, 2020
1 parent 8ef6dde commit b2479b9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
25 changes: 18 additions & 7 deletions godot.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ const (

// Settings contains linter settings.
type Settings struct {
// Which comments to check (top level declarations, top level, all).
Scope Scope

// Check periods at the end of sentences.
Period bool
}

// Issue contains a description of linting error and a recommended replacement.
Expand Down Expand Up @@ -90,7 +94,7 @@ func Run(file *ast.File, fset *token.FileSet, settings Settings) ([]Issue, error
return nil, fmt.Errorf("get comments: %v", err)
}

issues, err := checkComments(fset, comments)
issues, err := checkComments(fset, comments, settings)
if err != nil {
return nil, fmt.Errorf("check comments: %v", err)
}
Expand Down Expand Up @@ -340,8 +344,9 @@ func getAllComments(file *ast.File, fset *token.FileSet, lines []string) ([]comm
return comments, nil
}

// checkComments checks that every comment ends with a period.
func checkComments(fset *token.FileSet, comments []comment) ([]Issue, error) {
// checkComments checks every comment accordings to the rules from
// `settings` argument.
func checkComments(fset *token.FileSet, comments []comment, settings Settings) ([]Issue, error) {
var issues []Issue // nolint: prealloc
for _, c := range comments {
if c.ast == nil || len(c.ast.List) == 0 {
Expand All @@ -352,7 +357,13 @@ func checkComments(fset *token.FileSet, comments []comment) ([]Issue, error) {
start := fset.Position(c.ast.List[0].Slash)

text := getText(c.ast)
pos, ok := checkText(text)

// No checks were set
if !settings.Period {
continue
}

pos, ok := checkPeriod(text)
if ok {
continue
}
Expand Down Expand Up @@ -423,11 +434,11 @@ func getText(comment *ast.CommentGroup) (s string) {
return s[:len(s)-1] // trim last "\n"
}

// checkText checks extracted text from comment structure, and returns position
// of the issue if found.
// checkPeriod checks extracted text from comment structure, and returns
// position of the issue if found.
// NOTE: Returned position is a position inside given text, not position in
// the original file.
func checkText(comment string) (pos position, ok bool) {
func checkPeriod(comment string) (pos position, ok bool) {
isBlock := strings.HasPrefix(comment, "/*")

// Check last non-empty line
Expand Down
4 changes: 2 additions & 2 deletions godot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func TestGetText(t *testing.T) {
}
}

func TestCheckText(t *testing.T) {
func TestCheckPeriod(t *testing.T) {
testCases := []struct {
name string
comment string
Expand Down Expand Up @@ -307,7 +307,7 @@ func TestCheckText(t *testing.T) {
for _, tt := range testCases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
pos, ok := checkText(tt.comment)
pos, ok := checkPeriod(tt.comment)
if ok != tt.ok {
t.Fatalf("Wrong result\n expected: %v\n got: %v", tt.ok, ok)
}
Expand Down

0 comments on commit b2479b9

Please sign in to comment.