/
commentFormatting_checker.go
123 lines (107 loc) · 3.08 KB
/
commentFormatting_checker.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
package checkers
import (
"go/ast"
"regexp"
"strings"
"unicode"
"unicode/utf8"
"github.com/go-critic/go-critic/checkers/internal/astwalk"
"github.com/go-critic/go-critic/linter"
)
func init() {
var info linter.CheckerInfo
info.Name = "commentFormatting"
info.Tags = []string{linter.StyleTag}
info.Summary = "Detects comments with non-idiomatic formatting"
info.Before = `//This is a comment`
info.After = `// This is a comment`
collection.AddChecker(&info, func(ctx *linter.CheckerContext) (linter.FileWalker, error) {
regexpPatterns := []*regexp.Regexp{
regexp.MustCompile(`^//[\w-]+:.*$`), // e.g.: key: value
}
equalPatterns := []string{
"//nolint",
}
parts := []string{
"//go:generate ", // e.g.: go:generate value
"//line /", // e.g.: line /path/to/file:123
"//nolint ", // e.g.: nolint
"//noinspection ", // e.g.: noinspection ALL, some GoLand and friends versions
"//region", // e.g.: region awawa, used by GoLand and friends for custom folding
"//endregion", // e.g.: endregion awawa or endregion, closes GoLand regions
"//<editor-fold", // e.g.: <editor-fold desc="awawa"> or <editor-fold>, used by VSCode for custom folding
"//</editor-fold>", // e.g.: </editor-fold>, closes VSCode regions
"//export ", // e.g.: export Foo
"///", // e.g.: vertical breaker /////////////
"//+",
"//#",
"//-",
"//!",
}
return astwalk.WalkerForComment(&commentFormattingChecker{
ctx: ctx,
partPatterns: parts,
equalPatterns: equalPatterns,
regexpPatterns: regexpPatterns,
}), nil
})
}
type commentFormattingChecker struct {
astwalk.WalkHandler
ctx *linter.CheckerContext
partPatterns []string
equalPatterns []string
regexpPatterns []*regexp.Regexp
}
func (c *commentFormattingChecker) VisitComment(cg *ast.CommentGroup) {
if strings.HasPrefix(cg.List[0].Text, "/*") {
return
}
outerLoop:
for _, comment := range cg.List {
commentLen := len(comment.Text)
if commentLen <= len("// ") {
continue
}
for _, p := range c.partPatterns {
if commentLen < len(p) {
continue
}
if strings.EqualFold(comment.Text[:len(p)], p) {
continue outerLoop
}
}
for _, p := range c.equalPatterns {
if strings.EqualFold(comment.Text, p) {
continue outerLoop
}
}
for _, p := range c.regexpPatterns {
if p.MatchString(comment.Text) {
continue outerLoop
}
}
// Make a decision based on a first comment text rune.
r, _ := utf8.DecodeRuneInString(comment.Text[len("//"):])
if !c.specialChar(r) && !unicode.IsSpace(r) {
c.warn(comment)
return
}
}
}
func (c *commentFormattingChecker) specialChar(r rune) bool {
// Permitted list to avoid false-positives.
switch r {
case '+', '-', '#', '!':
return true
default:
return false
}
}
func (c *commentFormattingChecker) warn(comment *ast.Comment) {
c.ctx.WarnFixable(comment, linter.QuickFix{
From: comment.Pos(),
To: comment.End(),
Replacement: []byte(strings.Replace(comment.Text, "//", "// ", 1)),
}, "put a space between `//` and comment text")
}