-
Notifications
You must be signed in to change notification settings - Fork 7
/
checks.go
299 lines (263 loc) · 7.71 KB
/
checks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package godot
import (
"go/token"
"regexp"
"strings"
"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.
lastChars = []string{".", "?", "!", ".)", "?)", "!)", "。", "?", "!", "。)", "?)", "!)", specialReplacer}
// Abbreviations to exclude from capital letters check.
abbreviations = []string{"i.e.", "i. e.", "e.g.", "e. g.", "etc."}
// Special tags in comments like "//nolint:", or "//+k8s:".
tags = regexp.MustCompile(`^\+?[a-z0-9]+:`)
// Special hashtags in comments like "// #nosec".
hashtags = regexp.MustCompile(`^#[a-z]+($|\s)`)
// URL at the end of the line.
endURL = regexp.MustCompile(`[a-z]+://[^\s]+$`)
)
// position is a position inside a comment (might be multiline comment).
type position struct {
line int // starts at 1
column int // starts at 1, byte count
}
// checkComments checks every comment accordings to the rules from
// `settings` argument.
func checkComments(comments []comment, settings Settings) []Issue {
var issues []Issue
for _, c := range comments {
if settings.Period {
if iss := checkPeriod(c); iss != nil {
issues = append(issues, *iss)
}
}
if settings.Capital {
if iss := checkCapital(c); len(iss) > 0 {
issues = append(issues, iss...)
}
}
}
return issues
}
// checkPeriod checks that the last sentense of the comment ends
// in a period.
func checkPeriod(c comment) *Issue {
// Check last non-empty line
var found bool
var line string
var pos position
lines := strings.Split(c.text, "\n")
for i := len(lines) - 1; i >= 0; i-- {
line = strings.TrimRightFunc(lines[i], unicode.IsSpace)
if line == "" {
continue
}
found = true
pos.line = i + 1
break
}
// All lines are empty
if !found {
return nil
}
// Correct line
if hasSuffix(line, lastChars) {
return nil
}
pos.column = len(line) + 1
// Shift position to its real value. `c.text` doesn't contain comment's
// special symbols: /* or //, and line indentations inside. It also
// contains */ in the end in case of block comment.
pos.column += strings.Index(
c.lines[pos.line-1],
strings.Split(c.text, "\n")[pos.line-1],
)
iss := Issue{
Pos: token.Position{
Filename: c.start.Filename,
Offset: c.start.Offset,
Line: pos.line + c.start.Line - 1,
Column: pos.column,
},
Message: noPeriodMessage,
}
// 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.
original := c.lines[pos.line-1]
if len(original) < iss.Pos.Column-1 {
// This should never happen. Avoid panics, skip this check.
return nil
}
iss.Replacement = original[:iss.Pos.Column-1] + "." +
original[iss.Pos.Column-1:]
// Save replacement to raw lines to be able to combine it with
// further replacements
c.lines[pos.line-1] = iss.Replacement
return &iss
}
// checkCapital checks that each sentense of the comment starts with
// a capital letter.
//
//nolint:cyclop,funlen
func checkCapital(c comment) []Issue {
// Remove common abbreviations from the comment
for _, abbr := range abbreviations {
repl := strings.ReplaceAll(abbr, ".", "_")
c.text = strings.ReplaceAll(c.text, 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.
const empty, endChar, endOfSentence = 1, 2, 3
var pp []position
pos := position{line: 1}
state := endOfSentence
if c.decl {
// Skip first
state = empty
}
for _, r := range c.text {
s := string(r)
pos.column++
if s == "\n" {
pos.line++
pos.column = 0
if state == endChar {
state = endOfSentence
}
continue
}
if s == "." || s == "!" || s == "?" {
state = endChar
continue
}
if s == ")" && state == endChar {
continue
}
if s == " " {
if state == endChar {
state = endOfSentence
}
continue
}
if state == endOfSentence && unicode.IsLower(r) {
pp = append(pp, position{
line: pos.line,
column: runeToByteColumn(c.lines[pos.line-1], pos.column),
})
}
state = empty
}
if len(pp) == 0 {
return nil
}
issues := make([]Issue, len(pp))
for i, pos := range pp {
// Shift position by the length of comment's special symbols: /* or //
isBlock := strings.HasPrefix(c.lines[0], "/*")
if (isBlock && pos.line == 1) || !isBlock {
pos.column += 2
}
iss := Issue{
Pos: token.Position{
Filename: c.start.Filename,
Offset: c.start.Offset,
Line: pos.line + c.start.Line - 1,
Column: pos.column + c.start.Column - 1,
},
Message: noCapitalMessage,
}
// Make a replacement. Use `pos.original` to get an original original from
// attached lines. Use `iss.Pos.Column` because it's a position in
// the original original.
original := c.lines[pos.line-1]
col := byteToRuneColumn(original, iss.Pos.Column) - 1
rep := string(unicode.ToTitle([]rune(original)[col])) // capital letter
if len(original) < iss.Pos.Column-1+len(rep) {
// This should never happen. Avoid panics, skip this check.
continue
}
iss.Replacement = original[:iss.Pos.Column-1] + rep +
original[iss.Pos.Column-1+len(rep):]
// Save replacement to raw lines to be able to combine it with
// further replacements
c.lines[pos.line-1] = iss.Replacement
issues[i] = iss
}
return issues
}
// isSpecialBlock checks that given block of comment lines is special and
// shouldn't be checked as a regular sentence.
func isSpecialBlock(comment string) bool {
// Skip cgo code blocks
// TODO: Find a better way to detect cgo code
if strings.HasPrefix(comment, "/*") && (strings.Contains(comment, "#include") ||
strings.Contains(comment, "#define")) {
return true
}
// This should only be skipped in test files, but we don't have this
// information here, so - always skip
if strings.HasPrefix(comment, "// Output:") ||
strings.HasPrefix(comment, "// Unordered output:") {
return true
}
return false
}
// isSpecialLine checks that given comment line is special and
// shouldn't be checked as a regular sentence.
func isSpecialLine(comment string) bool {
// Skip cgo export tags: https://golang.org/cmd/cgo/#hdr-C_references_to_Go
if strings.HasPrefix(comment, "//export ") {
return true
}
comment = strings.TrimPrefix(comment, "//")
comment = strings.TrimPrefix(comment, "/*")
// Don't check comments starting with space indentation - they may
// contain code examples, which shouldn't end with period
if strings.HasPrefix(comment, " ") ||
strings.HasPrefix(comment, " \t") ||
strings.HasPrefix(comment, "\t") {
return true
}
// Skip tags and URLs
comment = strings.TrimSpace(comment)
if tags.MatchString(comment) ||
hashtags.MatchString(comment) ||
endURL.MatchString(comment) ||
strings.HasPrefix(comment, "+build") {
return true
}
return false
}
func hasSuffix(s string, suffixes []string) bool {
for _, suffix := range suffixes {
if strings.HasSuffix(s, suffix) {
return true
}
}
return false
}
// The following two functions convert byte and rune indexes.
//
// Example:
// text: a b c Ш e f
// runes: 1 2 3 4 5 6
// bytes: 0 1 2 3 5 6
// The reason of the difference is that the size of "Ш" is 2 bytes.
// NOTE: Works only for 1-based indexes (line columns).
// byteToRuneColumn converts byte index inside the string to rune index.
func byteToRuneColumn(s string, i int) int {
return len([]rune(s[:i-1])) + 1
}
// runeToByteColumn converts rune index inside the string to byte index.
func runeToByteColumn(s string, i int) int {
return len(string([]rune(s)[:i-1])) + 1
}