Skip to content

Commit

Permalink
Add (empty) check for capital letters in sentences.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krivak committed Oct 11, 2020
1 parent 03625b1 commit 6978e13
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 3 deletions.
56 changes: 56 additions & 0 deletions checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import (
"unicode"
)

// Error messages.
const (
noPeriodMessage = "Comment should end in a period"
noCapitalMessage = "Sentence should start with a capital letter"
)

var (
// List of valid sentence ending.
// A sentence can be inside parenthesis, and therefore ends with parenthesis.
Expand Down Expand Up @@ -43,6 +49,16 @@ func checkComments(fset *token.FileSet, comments []comment, settings Settings) (
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 iss != nil {
issues = append(issues, *iss)
}
}
}
return issues, nil
}
Expand Down Expand Up @@ -99,6 +115,39 @@ func checkCommentForPeriod(fset *token.FileSet, c comment) (*Issue, error) {
return &iss, nil
}

// 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) {
// Save global line number and indent
start := fset.Position(c.ast.List[0].Slash)

text := getText(c.ast)

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

// Shift position by the length of comment's special symbols: /* or //
isBlock := strings.HasPrefix(c.ast.List[0].Text, "/*")
if (isBlock && pos.line == 1) || !isBlock {
pos.column += 2
}

iss := Issue{
Pos: token.Position{
Filename: start.Filename,
Offset: start.Offset,
Line: pos.line + start.Line - 1,
Column: pos.column + start.Column - 1,
},
Message: noCapitalMessage,
}

return &iss, nil
}

// checkPeriod checks that the last sentense of the text ends in a period.
// NOTE: Returned position is a position inside given text, not in the
// original file.
Expand Down Expand Up @@ -129,6 +178,13 @@ func checkPeriod(comment string) (pos position, ok bool) {
return pos, false
}

// checkCapital checks that the each sentense of the text starts with
// a capital letter.
func checkCapital(comment string) (pos position, ok bool) {
// TODO: Implement
return position{}, true
}

// isSpecialBlock checks that given block of comment lines is special and
// shouldn't be checked as a regular sentence.
func isSpecialBlock(comment string) bool {
Expand Down
27 changes: 27 additions & 0 deletions checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,33 @@ func TestCheckPeriod(t *testing.T) {
}
}

func TestCheckCapital(t *testing.T) {
testCases := []struct {
name string
text string
ok bool
issue position
}{
// TODO: Add tests
}

for _, tt := range testCases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
pos, ok := checkCapital(tt.text)
if ok != tt.ok {
t.Fatalf("Wrong result\n expected: %v\n got: %v", tt.ok, ok)
}
if pos.line != tt.issue.line {
t.Fatalf("Wrong line\n expected: %d\n got: %d", tt.issue.line, pos.line)
}
if pos.column != tt.issue.column {
t.Fatalf("Wrong column\n expected: %d\n got: %d", tt.issue.column, pos.column)
}
})
}
}

func TestIsSpecialBlock(t *testing.T) {
testCases := []struct {
name string
Expand Down
3 changes: 0 additions & 3 deletions godot.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ import (
// NOTE: Errors `invalid line number inside comment...` should never happen.
// Their goal is to prevent panic, if there's a bug with array indexes.

// noPeriodMessage is an error message to return.
const noPeriodMessage = "Comment should end in a period"

// Issue contains a description of linting error and a recommended replacement.
type Issue struct {
Pos token.Position
Expand Down
3 changes: 3 additions & 0 deletions settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ type Settings struct {

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

// Check that first letter of each sentence is capital.
Capital bool
}

// Scope sets which comments should be checked.
Expand Down

0 comments on commit 6978e13

Please sign in to comment.