-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcomment.go
169 lines (152 loc) · 4.37 KB
/
comment.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
165
166
167
168
169
package comment
import (
"go/ast"
"go/token"
"strings"
)
// Maps is slice of ast.CommentMap.
type Maps []ast.CommentMap
// New creates new a CommentMap slice from specified files.
func New(fset *token.FileSet, files []*ast.File) Maps {
maps := make(Maps, len(files))
for i := range files {
maps[i] = ast.NewCommentMap(fset, files[i], files[i].Comments)
}
return maps
}
// Comments returns correspond a CommentGroup slice to specified AST node.
func (maps Maps) Comments(n ast.Node) []*ast.CommentGroup {
for i := range maps {
if maps[i][n] != nil {
return maps[i][n]
}
}
return nil
}
// CommentsByPos returns correspond a CommentGroup slice to specified pos.
func (maps Maps) CommentsByPos(pos token.Pos) []*ast.CommentGroup {
for i := range maps {
for n, cgs := range maps[i] {
if n.Pos() == pos {
return cgs
}
}
}
return nil
}
// Annotated checks either specified AST node is annotated or not.
func (maps Maps) Annotated(n ast.Node, annotation string) bool {
for _, cg := range maps.Comments(n) {
if strings.HasPrefix(strings.TrimSpace(cg.Text()), annotation) {
return true
}
}
return false
}
// Ignore checks either specified AST node is ignored by the check.
// It follows staticcheck style as the below.
//
// //lint:ignore Check1[,Check2,...,CheckN] reason
func (maps Maps) Ignore(n ast.Node, check string) bool {
for _, cg := range maps.Comments(n) {
if hasIgnoreCheck(cg, check) {
return true
}
}
return false
}
// IgnorePos checks either specified postion of AST node is ignored by the check.
// It follows staticcheck style as the below.
//
// //lint:ignore Check1[,Check2,...,CheckN] reason
func (maps Maps) IgnorePos(pos token.Pos, check string) bool {
for _, cg := range maps.CommentsByPos(pos) {
if hasIgnoreCheck(cg, check) {
return true
}
}
return false
}
// Deprecated: This function does not work with multiple files.
// CommentsByPosLine can be used instead of CommentsByLine.
//
// CommentsByLine returns correspond a CommentGroup slice to specified line.
func (maps Maps) CommentsByLine(fset *token.FileSet, line int) []*ast.CommentGroup {
for i := range maps {
for n, cgs := range maps[i] {
l := fset.File(n.Pos()).Line(n.Pos())
if l == line {
return cgs
}
}
}
return nil
}
// CommentsByPosLine returns correspond a CommentGroup slice to specified line.
func (maps Maps) CommentsByPosLine(fset *token.FileSet, pos token.Pos) []*ast.CommentGroup {
f1 := fset.File(pos)
for i := range maps {
for n, cgs := range maps[i] {
f2 := fset.File(n.Pos())
if f1 != f2 {
// different file
continue
}
if f1.Line(pos) == f2.Line(n.Pos()) {
return cgs
}
}
}
return nil
}
// Deprecated: This function does not work with multiple files.
// IgnoreLine checks either specified lineof AST node is ignored by the check.
// It follows staticcheck style as the below.
//
// //lint:ignore Check1[,Check2,...,CheckN] reason
func (maps Maps) IgnoreLine(fset *token.FileSet, line int, check string) bool {
for _, cg := range maps.CommentsByLine(fset, line) {
if hasIgnoreCheck(cg, check) {
return true
}
}
return false
}
// IgnorePosLine checks either specified lineof AST node is ignored by the check.
// It follows staticcheck style as the below.
//
// //lint:ignore Check1[,Check2,...,CheckN] reason
func (maps Maps) IgnorePosLine(fset *token.FileSet, pos token.Pos, check string) bool {
for _, cg := range maps.CommentsByPosLine(fset, pos) {
if hasIgnoreCheck(cg, check) {
return true
}
}
return false
}
// hasIgnoreCheck returns true if the provided CommentGroup starts with a comment
// of the form "//lint:ignore Check1[,Check2,...,CheckN] reason" and one of the
// checks matches the provided check.
//
// The *ast.CommentGroup is checked directly rather than using "cg.Text()" because,
// starting in Go 1.15, the "cg.Text()" call no longer returns directive-style
// comments (see https://github.com/golang/go/issues/37974).
func hasIgnoreCheck(cg *ast.CommentGroup, check string) bool {
for _, list := range cg.List {
if !strings.HasPrefix(list.Text, "//") {
continue
}
s := strings.TrimSpace(list.Text[2:]) // list.Text[2:]: trim "//"
txt := strings.Split(s, " ")
if len(txt) < 3 || txt[0] != "lint:ignore" {
continue
}
checks := strings.Split(txt[1], ",") // txt[1]: trim "lint:ignore"
for i := range checks {
if check == checks[i] {
return true
}
}
}
return false
}