-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinechecker.go
57 lines (46 loc) · 1.34 KB
/
linechecker.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
package pkglint
import (
"github.com/rillig/pkglint/v23/textproc"
"strings"
)
type LineChecker struct {
line *Line
}
func (ck LineChecker) CheckLength(maxLength int) {
if len(ck.line.Text) <= maxLength {
return
}
prefix := ck.line.Text[0:maxLength]
if !strings.ContainsAny(prefix, " \t") {
return
}
ck.line.Warnf("Line too long (should be no more than %d characters).",
maxLength)
ck.line.Explain(
"Back in the old time, terminals with 80x25 characters were common.",
"And this is still the default size of many terminal emulators.",
"Moderately short lines also make reading easier.")
}
func (ck LineChecker) CheckValidCharacters() {
word, invalid := invalidCharacters(ck.line.Text, textproc.XPrint)
if invalid == "" {
return
}
ck.line.Warnf("Line contains invalid %s \"%s\".", word, invalid)
}
func (ck LineChecker) CheckTrailingWhitespace() {
// Markdown files may need trailing whitespace. If there should ever
// be Markdown files in pkgsrc, this code has to be adjusted.
rawIndex := len(ck.line.raw) - 1
text := ck.line.RawText(rawIndex)
trimmedLen := len(rtrimHspace(text))
if trimmedLen == len(text) {
return
}
fix := ck.line.Autofix()
fix.Notef("Trailing whitespace.")
fix.Explain(
"This whitespace is irrelevant and can be removed.")
fix.ReplaceAt(rawIndex, trimmedLen, text[trimmedLen:], "")
fix.Apply()
}