-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
255 lines (224 loc) · 5.1 KB
/
Copy pathmain.go
File metadata and controls
255 lines (224 loc) · 5.1 KB
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
package main
import (
"flag"
"fmt"
"go/ast"
"go/parser"
"go/token"
"log"
"os"
"regexp"
"strings"
"unicode"
)
func main() {
l := &linter{
fset: token.NewFileSet(),
}
flag.StringVar(&l.path, "path", "", `path to package to be checked`)
flag.Parse()
if l.path == "" {
log.Fatalf("path can't be empty")
}
packages, err := parser.ParseDir(l.fset, l.path, nil, parser.ParseComments)
if err != nil {
log.Fatalf("parse path: %v", err)
}
l.Init()
for _, pkg := range packages {
l.CheckPackage(pkg)
for _, f := range pkg.Files {
l.CheckFile(f)
}
}
os.Exit(l.ExitCode())
}
type linter struct {
path string
fset *token.FileSet
current struct {
fn *ast.FuncDecl
}
regexp struct {
predAntipattern *regexp.Regexp
predPrefix *regexp.Regexp
directive *regexp.Regexp
}
issues int
}
func (l *linter) Init() {
l.initRegexps()
}
func (l *linter) initRegexps() {
{
prefixes := []string{
"Has",
"Is",
"Contains",
"Can",
}
for _, p := range prefixes {
prefixes = append(prefixes, strings.ToLower(p))
}
pat := `(?:` + strings.Join(prefixes, "|") + `)[A-Z0-9]\w*`
l.regexp.predPrefix = regexp.MustCompile(pat)
}
{
patterns := []string{
"returns true if",
"returns false if",
"returns true iff",
"returns false iff",
"returns true for",
"returns false for",
"returns true when",
"returns false when",
"tells whether",
"tests whether",
"determines whether",
"indicates whether",
}
for i, p := range patterns {
patterns[i] = " " + p + " "
}
pat := strings.Join(patterns, "|")
l.regexp.predAntipattern = regexp.MustCompile(pat)
}
l.regexp.directive = regexp.MustCompile(`//\w+: .*`)
}
func (l *linter) warnPkg(fileName, format string, args ...interface{}) {
l.issues++
var anchor string
if fileName == "" {
anchor = l.path + ": "
} else {
anchor = fileName + ": "
}
fmt.Fprintf(os.Stderr, anchor+format+"\n", args...)
}
func (l *linter) warnFunc(format string, args ...interface{}) {
l.issues++
anchor := l.fset.Position(l.current.fn.Pos()).String() + ": "
fmt.Fprintf(os.Stderr, anchor+format+"\n", args...)
}
func (l *linter) ExitCode() int {
if l.issues == 0 {
return 0
}
return 1
}
func (l *linter) CheckPackage(pkg *ast.Package) {
var docFilename string
var doc *ast.CommentGroup
count := 0
for filename, f := range pkg.Files {
if f.Doc != nil {
count++
doc = f.Doc
docFilename = filename
}
}
switch count {
case 1:
// Good. Safe to run other checks.
case 0:
l.warnPkg("", "no doc-comment found")
return
default:
l.warnPkg("", "found %d doc-comments, expected 1", count)
return
}
if pkg.Name != "main" {
lines := 0
for _, c := range doc.List {
lines += strings.Count(c.Text, "\n") + 1
}
if lines > 100 && docFilename != "doc.go" {
l.warnPkg(docFilename, "long doc-comments should go into doc.go file")
}
}
}
func (l *linter) CheckFile(f *ast.File) {
for _, decl := range f.Decls {
switch decl := decl.(type) {
case *ast.FuncDecl:
if decl.Doc != nil {
l.current.fn = decl
doc := decl.Doc
l.checkBoolFuncStyle(doc)
l.checkNoMultiline(doc)
l.checkEndsWithPunct(doc)
l.checkSpacing(doc)
}
}
}
}
func (l *linter) checkSpacing(doc *ast.CommentGroup) {
for _, c := range doc.List {
if strings.HasPrefix(c.Text, "/*") {
continue
}
if l.regexp.directive.MatchString(c.Text) {
continue
}
if !strings.HasPrefix(c.Text, "// ") && !strings.HasPrefix(c.Text, "//\t") {
l.warnFunc("found comment without leading space and it's not a pragma")
}
}
}
func (l *linter) checkEndsWithPunct(doc *ast.CommentGroup) {
// Check only 1-line comments for now as it's easier to avoid
// false-positives this way.
if len(doc.List) != 1 || !strings.HasPrefix(doc.List[0].Text, "//") {
return
}
line := doc.List[0].Text
if !unicode.IsPunct(rune(line[len(line)-1])) {
l.warnFunc("doc-comment should end with punctuation, usually with period")
}
}
func (l *linter) checkNoMultiline(doc *ast.CommentGroup) {
for _, c := range doc.List {
if strings.HasPrefix(c.Text, "/*") {
l.warnFunc("should not use /**/ comments in doc-comments")
return
}
}
}
func (l *linter) checkBoolFuncStyle(doc *ast.CommentGroup) {
if !isBooleanFunc(l.current.fn) {
return
}
line := doc.List[0].Text
name := l.current.fn.Name.Name
// 1. Check if doc string has common pattern that is considered
// less idiomatic than proposed alternative.
loc := l.regexp.predAntipattern.FindStringIndex(line)
if loc != nil {
diff := loc[0] - len(name)
if diff > 1 && diff <= 4 {
l.warnFunc("bad predicate comment")
}
}
// 2. Guess predicate function by it's name.
// If it is a predicate, check doc-comment.
if l.regexp.predPrefix.MatchString(name) {
if !strings.Contains(line, name+" reports whether ") {
l.warnFunc("bad predicate comment")
return
}
}
}
func isBooleanFunc(decl *ast.FuncDecl) bool {
if decl.Type.Results == nil || len(decl.Type.Results.List) != 1 {
return false
}
res := decl.Type.Results.List[0]
if len(res.Names) != 1 {
return false
}
if typ, ok := res.Type.(*ast.Ident); ok {
return typ.Name == "bool"
}
return false
}