@@ -9,53 +9,8 @@ import (
9
9
"golang.org/x/tools/go/analysis"
10
10
)
11
11
12
- // MessageType describes what should happen to fix the warning.
13
- type MessageType uint8
14
-
15
- // List of MessageTypes.
16
- const (
17
- MessageTypeRemove MessageType = iota + 1
18
- MessageTypeAdd
19
- )
20
-
21
- // RunningMode describes the mode the linter is run in. This can be either
22
- // native or golangci-lint.
23
- type RunningMode uint8
24
-
25
- const (
26
- RunningModeNative RunningMode = iota
27
- RunningModeGolangCI
28
- )
29
-
30
- // Message contains a message and diagnostic information.
31
- type Message struct {
32
- // Diagnostic is what position the diagnostic should be put at. This isn't
33
- // always the same as the fix start, f.ex. when we fix trailing newlines we
34
- // put the diagnostic at the right bracket but we fix between the end of the
35
- // last statement and the bracket.
36
- Diagnostic token.Pos
37
-
38
- // FixStart is the span start of the fix.
39
- FixStart token.Pos
40
-
41
- // FixEnd is the span end of the fix.
42
- FixEnd token.Pos
43
-
44
- // LineNumbers represent the actual line numbers in the file. This is set
45
- // when finding the diagnostic to make it easier to suggest fixes in
46
- // golangci-lint.
47
- LineNumbers []int
48
-
49
- // MessageType represents the type of message it is.
50
- MessageType MessageType
51
-
52
- // Message is the diagnostic to show.
53
- Message string
54
- }
55
-
56
12
// Settings contains settings for edge-cases.
57
13
type Settings struct {
58
- Mode RunningMode
59
14
MultiIf bool
60
15
MultiFunc bool
61
16
}
@@ -86,47 +41,24 @@ func flags(settings *Settings) flag.FlagSet {
86
41
return * flags
87
42
}
88
43
89
- func Run (pass * analysis.Pass , settings * Settings ) []Message {
90
- messages := []Message {}
91
-
44
+ func Run (pass * analysis.Pass , settings * Settings ) {
92
45
for _ , file := range pass .Files {
93
46
filename := pass .Fset .Position (file .Pos ()).Filename
47
+
94
48
if ! strings .HasSuffix (filename , ".go" ) {
95
49
continue
96
50
}
97
51
98
52
fileMessages := runFile (file , pass .Fset , * settings )
99
53
100
- if settings .Mode == RunningModeGolangCI {
101
- messages = append (messages , fileMessages ... )
102
- continue
103
- }
104
-
105
54
for _ , message := range fileMessages {
106
- pass .Report (analysis.Diagnostic {
107
- Pos : message .Diagnostic ,
108
- Category : "whitespace" ,
109
- Message : message .Message ,
110
- SuggestedFixes : []analysis.SuggestedFix {
111
- {
112
- TextEdits : []analysis.TextEdit {
113
- {
114
- Pos : message .FixStart ,
115
- End : message .FixEnd ,
116
- NewText : []byte ("\n " ),
117
- },
118
- },
119
- },
120
- },
121
- })
55
+ pass .Report (message )
122
56
}
123
57
}
124
-
125
- return messages
126
58
}
127
59
128
- func runFile (file * ast.File , fset * token.FileSet , settings Settings ) []Message {
129
- var messages []Message
60
+ func runFile (file * ast.File , fset * token.FileSet , settings Settings ) []analysis. Diagnostic {
61
+ var messages []analysis. Diagnostic
130
62
131
63
for _ , f := range file .Decls {
132
64
decl , ok := f .(* ast.FuncDecl )
@@ -146,7 +78,7 @@ func runFile(file *ast.File, fset *token.FileSet, settings Settings) []Message {
146
78
type visitor struct {
147
79
comments []* ast.CommentGroup
148
80
fset * token.FileSet
149
- messages []Message
81
+ messages []analysis. Diagnostic
150
82
wantNewline map [* ast.BlockStmt ]bool
151
83
settings Settings
152
84
}
@@ -180,13 +112,16 @@ func (v *visitor) Visit(node ast.Node) ast.Visitor {
180
112
startMsg := checkStart (v .fset , opening , first )
181
113
182
114
if wantNewline && startMsg == nil && len (stmt .List ) >= 1 {
183
- v .messages = append (v .messages , Message {
184
- Diagnostic : opening ,
185
- FixStart : stmt .List [0 ].Pos (),
186
- FixEnd : stmt .List [0 ].Pos (),
187
- LineNumbers : []int {v .fset .PositionFor (stmt .List [0 ].Pos (), false ).Line },
188
- MessageType : MessageTypeAdd ,
189
- Message : "multi-line statement should be followed by a newline" ,
115
+ v .messages = append (v .messages , analysis.Diagnostic {
116
+ Pos : opening ,
117
+ Message : "multi-line statement should be followed by a newline" ,
118
+ SuggestedFixes : []analysis.SuggestedFix {{
119
+ TextEdits : []analysis.TextEdit {{
120
+ Pos : stmt .List [0 ].Pos (),
121
+ End : stmt .List [0 ].Pos (),
122
+ NewText : []byte ("\n " ),
123
+ }},
124
+ }},
190
125
})
191
126
} else if ! wantNewline && startMsg != nil {
192
127
v .messages = append (v .messages , * startMsg )
@@ -209,7 +144,7 @@ func checkMultiLine(v *visitor, body *ast.BlockStmt, stmtStart ast.Node) {
209
144
}
210
145
211
146
func posLine (fset * token.FileSet , pos token.Pos ) int {
212
- return fset .PositionFor (pos , false ).Line
147
+ return fset .Position (pos ).Line
213
148
}
214
149
215
150
func firstAndLast (comments []* ast.CommentGroup , fset * token.FileSet , stmt * ast.BlockStmt ) (token.Pos , ast.Node , ast.Node ) {
@@ -256,52 +191,46 @@ func firstAndLast(comments []*ast.CommentGroup, fset *token.FileSet, stmt *ast.B
256
191
return openingPos , first , last
257
192
}
258
193
259
- func checkStart (fset * token.FileSet , start token.Pos , first ast.Node ) * Message {
194
+ func checkStart (fset * token.FileSet , start token.Pos , first ast.Node ) * analysis. Diagnostic {
260
195
if first == nil {
261
196
return nil
262
197
}
263
198
264
199
if posLine (fset , start )+ 1 < posLine (fset , first .Pos ()) {
265
- return & Message {
266
- Diagnostic : start ,
267
- FixStart : start ,
268
- FixEnd : first .Pos (),
269
- LineNumbers : linesBetween (fset , start , first .Pos ()),
270
- MessageType : MessageTypeRemove ,
271
- Message : "unnecessary leading newline" ,
200
+ return & analysis.Diagnostic {
201
+ Pos : start ,
202
+ Message : "unnecessary leading newline" ,
203
+ SuggestedFixes : []analysis.SuggestedFix {{
204
+ TextEdits : []analysis.TextEdit {{
205
+ Pos : start ,
206
+ End : first .Pos (),
207
+ NewText : []byte ("\n " ),
208
+ }},
209
+ }},
272
210
}
273
211
}
274
212
275
213
return nil
276
214
}
277
215
278
- func checkEnd (fset * token.FileSet , end token.Pos , last ast.Node ) * Message {
216
+ func checkEnd (fset * token.FileSet , end token.Pos , last ast.Node ) * analysis. Diagnostic {
279
217
if last == nil {
280
218
return nil
281
219
}
282
220
283
221
if posLine (fset , end )- 1 > posLine (fset , last .End ()) {
284
- return & Message {
285
- Diagnostic : end ,
286
- FixStart : last .End (),
287
- FixEnd : end ,
288
- LineNumbers : linesBetween (fset , last .End (), end ),
289
- MessageType : MessageTypeRemove ,
290
- Message : "unnecessary trailing newline" ,
222
+ return & analysis.Diagnostic {
223
+ Pos : end ,
224
+ Message : "unnecessary trailing newline" ,
225
+ SuggestedFixes : []analysis.SuggestedFix {{
226
+ TextEdits : []analysis.TextEdit {{
227
+ Pos : last .End (),
228
+ End : end ,
229
+ NewText : []byte ("\n " ),
230
+ }},
231
+ }},
291
232
}
292
233
}
293
234
294
235
return nil
295
236
}
296
-
297
- func linesBetween (fset * token.FileSet , a , b token.Pos ) []int {
298
- lines := []int {}
299
- aPosition := fset .PositionFor (a , false )
300
- bPosition := fset .PositionFor (b , false )
301
-
302
- for i := aPosition .Line + 1 ; i < bPosition .Line ; i ++ {
303
- lines = append (lines , i )
304
- }
305
-
306
- return lines
307
- }
0 commit comments