forked from mgechev/revive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage_comments.go
164 lines (148 loc) · 4.18 KB
/
package_comments.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
package rule
import (
"fmt"
"go/ast"
"go/token"
"strings"
"sync"
"github.com/mgechev/revive/lint"
)
// PackageCommentsRule lints the package comments. It complains if
// there is no package comment, or if it is not of the right form.
// This has a notable false positive in that a package comment
// could rightfully appear in a different file of the same package,
// but that's not easy to fix since this linter is file-oriented.
type PackageCommentsRule struct {
checkPackageCommentCache sync.Map
}
// Apply applies the rule to given file.
func (r *PackageCommentsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
var failures []lint.Failure
if file.IsTest() {
return failures
}
onFailure := func(failure lint.Failure) {
failures = append(failures, failure)
}
fileAst := file.AST
w := &lintPackageComments{fileAst, file, onFailure, r}
ast.Walk(w, fileAst)
return failures
}
// Name returns the rule name.
func (*PackageCommentsRule) Name() string {
return "package-comments"
}
type lintPackageComments struct {
fileAst *ast.File
file *lint.File
onFailure func(lint.Failure)
rule *PackageCommentsRule
}
func (l *lintPackageComments) checkPackageComment() []lint.Failure {
// deduplicate warnings in package
if _, exists := l.rule.checkPackageCommentCache.LoadOrStore(l.file.Pkg, struct{}{}); exists {
return nil
}
var docFile *ast.File // which name is doc.go
var packageFile *ast.File // which name is $package.go
var firstFile *ast.File
var firstFileName string
var fileSource string
for name, file := range l.file.Pkg.Files() {
if file.AST.Doc != nil {
return nil
}
if name == "doc.go" {
docFile = file.AST
fileSource = "doc.go"
}
if name == file.AST.Name.String()+".go" {
packageFile = file.AST
}
if firstFileName == "" || firstFileName > name {
firstFile = file.AST
firstFileName = name
}
}
// prefer warning on doc.go, $package.go over first file
if docFile == nil {
docFile = packageFile
fileSource = l.fileAst.Name.String() + ".go"
}
if docFile == nil {
docFile = firstFile
fileSource = firstFileName
}
if docFile != nil {
pkgFile := l.file.Pkg.Files()[fileSource]
return []lint.Failure{{
Category: "comments",
Position: lint.FailurePosition{
Start: pkgFile.ToPosition(docFile.Pos()),
End: pkgFile.ToPosition(docFile.Name.End()),
},
Confidence: 1,
Failure: "should have a package comment",
}}
}
return nil
}
func (l *lintPackageComments) Visit(_ ast.Node) ast.Visitor {
if l.file.IsTest() {
return nil
}
prefix := "Package " + l.fileAst.Name.Name + " "
// Look for a detached package comment.
// First, scan for the last comment that occurs before the "package" keyword.
var lastCG *ast.CommentGroup
for _, cg := range l.fileAst.Comments {
if cg.Pos() > l.fileAst.Package {
// Gone past "package" keyword.
break
}
lastCG = cg
}
if lastCG != nil && strings.HasPrefix(lastCG.Text(), prefix) {
endPos := l.file.ToPosition(lastCG.End())
pkgPos := l.file.ToPosition(l.fileAst.Package)
if endPos.Line+1 < pkgPos.Line {
// There isn't a great place to anchor this error;
// the start of the blank lines between the doc and the package statement
// is at least pointing at the location of the problem.
pos := token.Position{
Filename: endPos.Filename,
// Offset not set; it is non-trivial, and doesn't appear to be needed.
Line: endPos.Line + 1,
Column: 1,
}
l.onFailure(lint.Failure{
Category: "comments",
Position: lint.FailurePosition{
Start: pos,
End: pos,
},
Confidence: 0.9,
Failure: "package comment is detached; there should be no blank lines between it and the package statement",
})
return nil
}
}
if l.fileAst.Doc == nil {
for _, failure := range l.checkPackageComment() {
l.onFailure(failure)
}
return nil
}
s := l.fileAst.Doc.Text()
// Only non-main packages need to keep to this form.
if !l.file.Pkg.IsMain() && !strings.HasPrefix(s, prefix) && !isDirectiveComment(s) {
l.onFailure(lint.Failure{
Category: "comments",
Node: l.fileAst.Doc,
Confidence: 1,
Failure: fmt.Sprintf(`package comment should be of the form "%s..."`, prefix),
})
}
return nil
}