forked from go-critic/go-critic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commentFormatting_checker.go
81 lines (71 loc) · 2.03 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
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/framework/linter"
)
func init() {
var info linter.CheckerInfo
info.Name = "commentFormatting"
info.Tags = []string{"style"}
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) {
parts := []string{
`^//go:generate .*$`, // e.g.: go:generate value
`^//[\w-]+:.*$`, // e.g.: key: value
`^//nolint\b`, // e.g.: nolint
`^//line /.*:\d+`, // e.g.: line /path/to/file:123
`^//export \w+$`, // e.g.: export Foo
`^//[/+#-]+.*$`, // e.g.: vertical breaker /////////////
`^//noinspection `, // e.g.: noinspection ALL, some GoLand and friends versions
}
pat := "(?m)" + strings.Join(parts, "|")
pragmaRE := regexp.MustCompile(pat)
return astwalk.WalkerForComment(&commentFormattingChecker{
ctx: ctx,
pragmaRE: pragmaRE,
}), nil
})
}
type commentFormattingChecker struct {
astwalk.WalkHandler
ctx *linter.CheckerContext
pragmaRE *regexp.Regexp
}
func (c *commentFormattingChecker) VisitComment(cg *ast.CommentGroup) {
if strings.HasPrefix(cg.List[0].Text, "/*") {
return
}
for _, comment := range cg.List {
if len(comment.Text) <= len("// ") {
continue
}
if c.pragmaRE.MatchString(comment.Text) {
continue
}
// 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.Warn(comment, "put a space between `//` and comment text")
}