diff --git a/checks.go b/checks.go index fdd8999..e9b43f0 100644 --- a/checks.go +++ b/checks.go @@ -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]+:`) @@ -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. diff --git a/checks_test.go b/checks_test.go index 714bb5c..e9dfd1f 100644 --- a/checks_test.go +++ b/checks_test.go @@ -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 { diff --git a/testdata/check/main.go b/testdata/check/main.go index fb8821e..0823cf1 100644 --- a/testdata/check/main.go +++ b/testdata/check/main.go @@ -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]