@@ -9,53 +9,8 @@ import (
99 "golang.org/x/tools/go/analysis"
1010)
1111
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-
5612// Settings contains settings for edge-cases.
5713type Settings struct {
58- Mode RunningMode
5914 MultiIf bool
6015 MultiFunc bool
6116}
@@ -86,47 +41,24 @@ func flags(settings *Settings) flag.FlagSet {
8641 return * flags
8742}
8843
89- func Run (pass * analysis.Pass , settings * Settings ) []Message {
90- messages := []Message {}
91-
44+ func Run (pass * analysis.Pass , settings * Settings ) {
9245 for _ , file := range pass .Files {
9346 filename := pass .Fset .Position (file .Pos ()).Filename
47+
9448 if ! strings .HasSuffix (filename , ".go" ) {
9549 continue
9650 }
9751
9852 fileMessages := runFile (file , pass .Fset , * settings )
9953
100- if settings .Mode == RunningModeGolangCI {
101- messages = append (messages , fileMessages ... )
102- continue
103- }
104-
10554 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 )
12256 }
12357 }
124-
125- return messages
12658}
12759
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
13062
13163 for _ , f := range file .Decls {
13264 decl , ok := f .(* ast.FuncDecl )
@@ -146,7 +78,7 @@ func runFile(file *ast.File, fset *token.FileSet, settings Settings) []Message {
14678type visitor struct {
14779 comments []* ast.CommentGroup
14880 fset * token.FileSet
149- messages []Message
81+ messages []analysis. Diagnostic
15082 wantNewline map [* ast.BlockStmt ]bool
15183 settings Settings
15284}
@@ -180,13 +112,16 @@ func (v *visitor) Visit(node ast.Node) ast.Visitor {
180112 startMsg := checkStart (v .fset , opening , first )
181113
182114 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+ }},
190125 })
191126 } else if ! wantNewline && startMsg != nil {
192127 v .messages = append (v .messages , * startMsg )
@@ -209,7 +144,7 @@ func checkMultiLine(v *visitor, body *ast.BlockStmt, stmtStart ast.Node) {
209144}
210145
211146func posLine (fset * token.FileSet , pos token.Pos ) int {
212- return fset .PositionFor (pos , false ).Line
147+ return fset .Position (pos ).Line
213148}
214149
215150func 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
256191 return openingPos , first , last
257192}
258193
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 {
260195 if first == nil {
261196 return nil
262197 }
263198
264199 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+ }},
272210 }
273211 }
274212
275213 return nil
276214}
277215
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 {
279217 if last == nil {
280218 return nil
281219 }
282220
283221 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+ }},
291232 }
292233 }
293234
294235 return nil
295236}
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