Skip to content

Commit

Permalink
Fix working with abbreviations.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krivak committed Jul 18, 2021
1 parent f4ed6e0 commit 09b0ef3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
9 changes: 9 additions & 0 deletions checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ var (
// A sentence can be inside parenthesis, and therefore ends with parenthesis.
lastChars = []string{".", "?", "!", ".)", "?)", "!)", specialReplacer}

// Abbreviations to exclude from capital letters check.
abbreviations = []string{"i.e.", "i. e.", "e.g.", "e. g."}

// Special tags in comments like "// nolint:", or "// +k8s:".
tags = regexp.MustCompile(`^\+?[a-z0-9]+:`)

Expand Down Expand Up @@ -164,6 +167,12 @@ func checkPeriod(comment string) (pos position, ok bool) {
// NOTE: First letter is not checked in declaration comments, because they
// can describe unexported functions, which start from small letter.
func checkCapital(comment string, skipFirst bool) (pp []position) {
// Remove common abbreviations from the comment
for _, abbr := range abbreviations {
repl := strings.ReplaceAll(abbr, ".", "_")
comment = strings.ReplaceAll(comment, abbr, repl)
}

// List of states during the scan: `empty` - nothing special,
// `endChar` - found one of sentence ending chars (.!?),
// `endOfSentence` - found `endChar`, and then space or newline.
Expand Down
6 changes: 6 additions & 0 deletions checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ func TestCheckCapital(t *testing.T) {
{line: 1, column: 5},
},
},
{
name: "sentence with abbreviations",
text: "One two, i.e. hello, world, e.g. e. g. word",
skipFirst: false,
issues: nil,
},
}

for _, tt := range testCases {
Expand Down
5 changes: 4 additions & 1 deletion testdata/check/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,11 @@ func inside() {

// nonCapital is a function. non-capital-decl first letter [CAPITAL_DECL].
func nonCapital() int {
// Test abbreviation (e.g. like this) [PASS].
x := 10

// non-capital-all [CAPITAL_ALL].
return 10 // non-capital-all [CAPITAL_ALL].
return x // non-capital-all [CAPITAL_ALL].
}

// Comment with a URL - http://example.com/[PASS]
Expand Down

0 comments on commit 09b0ef3

Please sign in to comment.