Skip to content

Commit

Permalink
Avoid excessive error checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krivak committed Oct 31, 2020
1 parent bd073f1 commit ac3f37d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 39 deletions.
42 changes: 12 additions & 30 deletions checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,47 +33,39 @@ var (

// checkComments checks every comment accordings to the rules from
// `settings` argument.
func checkComments(fset *token.FileSet, comments []comment, settings Settings) ([]Issue, error) {
func checkComments(fset *token.FileSet, comments []comment, settings Settings) []Issue {
var issues []Issue // nolint: prealloc
for _, c := range comments {
if c.ast == nil || len(c.ast.List) == 0 {
continue
}

if settings.Period {
iss, err := checkCommentForPeriod(fset, c)
if err != nil {
return nil, fmt.Errorf("check comment for period: %v", err)
}
if iss != nil {
if iss := checkCommentForPeriod(fset, c); iss != nil {
issues = append(issues, *iss)
}
}

if settings.Capital {
iss, err := checkCommentForCapital(fset, c)
if err != nil {
return nil, fmt.Errorf("check comment for capital: %v", err)
}
if len(iss) > 0 {
if iss := checkCommentForCapital(fset, c); len(iss) > 0 {
issues = append(issues, iss...)
}
}
}
return issues, nil
return issues
}

// checkCommentForPeriod checks that the last sentense of the comment ends
// in a period.
func checkCommentForPeriod(fset *token.FileSet, c comment) (*Issue, error) {
func checkCommentForPeriod(fset *token.FileSet, c comment) *Issue {
// Save global line number and indent
start := fset.Position(c.ast.List[0].Slash)

text := getText(c.ast)

pos, ok := checkPeriod(text)
if ok {
return nil, nil
return nil
}

// Shift position by the length of comment's special symbols: /* or //
Expand All @@ -95,38 +87,26 @@ func checkCommentForPeriod(fset *token.FileSet, c comment) (*Issue, error) {
// Make a replacement. Use `pos.line` to get an original line from
// attached lines. Use `iss.Pos.Column` because it's a position in
// the original line.
if pos.line-1 >= len(c.lines) {
return nil, fmt.Errorf(
"invalid line number inside comment: %s:%d",
iss.Pos.Filename, iss.Pos.Line,
)
}
original := []rune(c.lines[pos.line-1])
if iss.Pos.Column-1 > len(original) {
return nil, fmt.Errorf(
"invalid column number inside comment: %s:%d:%d",
iss.Pos.Filename, iss.Pos.Line, iss.Pos.Column,
)
}
iss.Replacement = fmt.Sprintf("%s.%s",
string(original[:iss.Pos.Column-1]),
string(original[iss.Pos.Column-1:]))

return &iss, nil
return &iss
}

// checkCommentForCapital checks that the each sentense of the comment starts with
// a capital letter.
// nolint: unparam
func checkCommentForCapital(fset *token.FileSet, c comment) ([]Issue, error) {
func checkCommentForCapital(fset *token.FileSet, c comment) []Issue {
// Save global line number and indent
start := fset.Position(c.ast.List[0].Slash)

text := getText(c.ast)

pp := checkCapital(text, c.decl)
if len(pp) == 0 {
return nil, nil
return nil
}

issues := make([]Issue, len(pp))
Expand All @@ -146,9 +126,11 @@ func checkCommentForCapital(fset *token.FileSet, c comment) ([]Issue, error) {
},
Message: noCapitalMessage,
}

// TODO: Make a replacement
}

return issues, nil
return issues
}

// checkPeriod checks that the last sentense of the text ends in a period.
Expand Down
5 changes: 1 addition & 4 deletions checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ func TestCheckComments(t *testing.T) {
{ast: nil},
{ast: &ast.CommentGroup{List: nil}},
}
issues, err := checkComments(nil, comments, Settings{})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
issues := checkComments(nil, comments, Settings{})
if len(issues) > 0 {
t.Fatalf("Unexpected issues: %d", len(issues))
}
Expand Down
7 changes: 2 additions & 5 deletions godot.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,9 @@ 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, settings)
if err != nil {
return nil, fmt.Errorf("check comments: %v", err)
}

issues := checkComments(fset, comments, settings)
sortIssues(issues)

return issues, nil
}

Expand Down

0 comments on commit ac3f37d

Please sign in to comment.