@@ -3,8 +3,6 @@ package tagalign
3
3
import (
4
4
"fmt"
5
5
"go/ast"
6
- "go/token"
7
- "log"
8
6
"reflect"
9
7
"sort"
10
8
"strconv"
@@ -15,13 +13,6 @@ import (
15
13
"golang.org/x/tools/go/analysis"
16
14
)
17
15
18
- type Mode int
19
-
20
- const (
21
- StandaloneMode Mode = iota
22
- GolangciLintMode
23
- )
24
-
25
16
type Style int
26
17
27
18
const (
@@ -44,11 +35,9 @@ func NewAnalyzer(options ...Option) *analysis.Analyzer {
44
35
}
45
36
}
46
37
47
- func Run (pass * analysis.Pass , options ... Option ) []Issue {
48
- var issues []Issue
38
+ func Run (pass * analysis.Pass , options ... Option ) {
49
39
for _ , f := range pass .Files {
50
40
h := & Helper {
51
- mode : StandaloneMode ,
52
41
style : DefaultStyle ,
53
42
align : true ,
54
43
}
@@ -63,22 +52,19 @@ func Run(pass *analysis.Pass, options ...Option) []Issue {
63
52
64
53
if ! h .align && ! h .sort {
65
54
// do nothing
66
- return nil
55
+ return
67
56
}
68
57
69
58
ast .Inspect (f , func (n ast.Node ) bool {
70
59
h .find (pass , n )
71
60
return true
72
61
})
62
+
73
63
h .Process (pass )
74
- issues = append (issues , h .issues ... )
75
64
}
76
- return issues
77
65
}
78
66
79
67
type Helper struct {
80
- mode Mode
81
-
82
68
style Style
83
69
84
70
align bool // whether enable tags align.
@@ -87,19 +73,6 @@ type Helper struct {
87
73
88
74
singleFields []* ast.Field
89
75
consecutiveFieldsGroups [][]* ast.Field // fields in this group, must be consecutive in struct.
90
- issues []Issue
91
- }
92
-
93
- // Issue is used to integrate with golangci-lint's inline auto fix.
94
- type Issue struct {
95
- Pos token.Position
96
- Message string
97
- InlineFix InlineFix
98
- }
99
- type InlineFix struct {
100
- StartCol int // zero-based
101
- Length int
102
- NewString string
103
76
}
104
77
105
78
func (w * Helper ) find (pass * analysis.Pass , n ast.Node ) {
@@ -159,39 +132,24 @@ func (w *Helper) find(pass *analysis.Pass, n ast.Node) {
159
132
split ()
160
133
}
161
134
162
- func (w * Helper ) report (pass * analysis.Pass , field * ast.Field , startCol int , msg , replaceStr string ) {
163
- if w .mode == GolangciLintMode {
164
- iss := Issue {
165
- Pos : pass .Fset .Position (field .Tag .Pos ()),
166
- Message : msg ,
167
- InlineFix : InlineFix {
168
- StartCol : startCol ,
169
- Length : len (field .Tag .Value ),
170
- NewString : replaceStr ,
171
- },
172
- }
173
- w .issues = append (w .issues , iss )
174
- }
175
-
176
- if w .mode == StandaloneMode {
177
- pass .Report (analysis.Diagnostic {
178
- Pos : field .Tag .Pos (),
179
- End : field .Tag .End (),
180
- Message : msg ,
181
- SuggestedFixes : []analysis.SuggestedFix {
182
- {
183
- Message : msg ,
184
- TextEdits : []analysis.TextEdit {
185
- {
186
- Pos : field .Tag .Pos (),
187
- End : field .Tag .End (),
188
- NewText : []byte (replaceStr ),
189
- },
135
+ func (w * Helper ) report (pass * analysis.Pass , field * ast.Field , msg , replaceStr string ) {
136
+ pass .Report (analysis.Diagnostic {
137
+ Pos : field .Tag .Pos (),
138
+ End : field .Tag .End (),
139
+ Message : msg ,
140
+ SuggestedFixes : []analysis.SuggestedFix {
141
+ {
142
+ Message : msg ,
143
+ TextEdits : []analysis.TextEdit {
144
+ {
145
+ Pos : field .Tag .Pos (),
146
+ End : field .Tag .End (),
147
+ NewText : []byte (replaceStr ),
190
148
},
191
149
},
192
150
},
193
- })
194
- }
151
+ },
152
+ })
195
153
}
196
154
197
155
func (w * Helper ) Process (pass * analysis.Pass ) { //nolint:gocognit
@@ -220,15 +178,15 @@ func (w *Helper) Process(pass *analysis.Pass) { //nolint:gocognit
220
178
tag , err := strconv .Unquote (field .Tag .Value )
221
179
if err != nil {
222
180
// if tag value is not a valid string, report it directly
223
- w .report (pass , field , column , errTagValueSyntax , field .Tag .Value )
181
+ w .report (pass , field , errTagValueSyntax , field .Tag .Value )
224
182
fields = removeField (fields , i )
225
183
continue
226
184
}
227
185
228
186
tags , err := structtag .Parse (tag )
229
187
if err != nil {
230
188
// if tag value is not a valid struct tag, report it directly
231
- w .report (pass , field , column , err .Error (), field .Tag .Value )
189
+ w .report (pass , field , err .Error (), field .Tag .Value )
232
190
fields = removeField (fields , i )
233
191
continue
234
192
}
@@ -340,22 +298,21 @@ func (w *Helper) Process(pass *analysis.Pass) { //nolint:gocognit
340
298
341
299
msg := "tag is not aligned, should be: " + unquoteTag
342
300
343
- w .report (pass , field , offsets [ i ], msg , newTagValue )
301
+ w .report (pass , field , msg , newTagValue )
344
302
}
345
303
}
346
304
347
305
// process single fields
348
306
for _ , field := range w .singleFields {
349
- column := pass .Fset .Position (field .Tag .Pos ()).Column - 1
350
307
tag , err := strconv .Unquote (field .Tag .Value )
351
308
if err != nil {
352
- w .report (pass , field , column , errTagValueSyntax , field .Tag .Value )
309
+ w .report (pass , field , errTagValueSyntax , field .Tag .Value )
353
310
continue
354
311
}
355
312
356
313
tags , err := structtag .Parse (tag )
357
314
if err != nil {
358
- w .report (pass , field , column , err .Error (), field .Tag .Value )
315
+ w .report (pass , field , err .Error (), field .Tag .Value )
359
316
continue
360
317
}
361
318
originalTags := append ([]* structtag.Tag (nil ), tags .Tags ()... )
@@ -371,17 +328,10 @@ func (w *Helper) Process(pass *analysis.Pass) { //nolint:gocognit
371
328
372
329
msg := "tag is not aligned , should be: " + tags .String ()
373
330
374
- w .report (pass , field , column , msg , newTagValue )
331
+ w .report (pass , field , msg , newTagValue )
375
332
}
376
333
}
377
334
378
- // Issues returns all issues found by the analyzer.
379
- // It is used to integrate with golangci-lint.
380
- func (w * Helper ) Issues () []Issue {
381
- log .Println ("tagalign 's Issues() should only be called in golangci-lint mode" )
382
- return w .issues
383
- }
384
-
385
335
// sortBy sorts tags by fixed order.
386
336
// If a tag is not in the fixed order, it will be sorted by name.
387
337
func sortBy (fixedOrder []string , tags * structtag.Tags ) {
@@ -443,13 +393,6 @@ func alignFormat(length int) string {
443
393
return "%" + fmt .Sprintf ("-%ds" , length )
444
394
}
445
395
446
- func max (a , b int ) int {
447
- if a > b {
448
- return a
449
- }
450
- return b
451
- }
452
-
453
396
func removeField (fields []* ast.Field , index int ) []* ast.Field {
454
397
if index < 0 || index >= len (fields ) {
455
398
return fields
0 commit comments