@@ -29,6 +29,10 @@ const (
29
29
StrictStyle
30
30
)
31
31
32
+ const (
33
+ errTagValueSyntax = "bad syntax for struct tag value"
34
+ )
35
+
32
36
func NewAnalyzer (options ... Option ) * analysis.Analyzer {
33
37
return & analysis.Analyzer {
34
38
Name : "tagalign" ,
@@ -208,16 +212,25 @@ func (w *Helper) Process(pass *analysis.Pass) { //nolint:gocognit
208
212
uniqueKeys = append (uniqueKeys , k )
209
213
}
210
214
211
- for i , field := range fields {
212
- offsets [i ] = pass .Fset .Position (field .Tag .Pos ()).Column
215
+ for i := 0 ; i < len (fields ); {
216
+ field := fields [i ]
217
+ column := pass .Fset .Position (field .Tag .Pos ()).Column - 1
218
+ offsets [i ] = column
219
+
213
220
tag , err := strconv .Unquote (field .Tag .Value )
214
221
if err != nil {
215
- break
222
+ // if tag value is not a valid string, report it directly
223
+ w .report (pass , field , column , errTagValueSyntax , field .Tag .Value )
224
+ fields = removeField (fields , i )
225
+ continue
216
226
}
217
227
218
228
tags , err := structtag .Parse (tag )
219
229
if err != nil {
220
- break
230
+ // if tag value is not a valid struct tag, report it directly
231
+ w .report (pass , field , column , err .Error (), field .Tag .Value )
232
+ fields = removeField (fields , i )
233
+ continue
221
234
}
222
235
223
236
maxTagNum = max (maxTagNum , tags .Len ())
@@ -234,6 +247,8 @@ func (w *Helper) Process(pass *analysis.Pass) { //nolint:gocognit
234
247
addKey (t .Key )
235
248
}
236
249
tagsGroup = append (tagsGroup , tags .Tags ())
250
+
251
+ i ++
237
252
}
238
253
239
254
if w .sort && StrictStyle == w .style {
@@ -325,19 +340,22 @@ func (w *Helper) Process(pass *analysis.Pass) { //nolint:gocognit
325
340
326
341
msg := "tag is not aligned, should be: " + unquoteTag
327
342
328
- w .report (pass , field , offsets [i ]- 1 , msg , newTagValue )
343
+ w .report (pass , field , offsets [i ], msg , newTagValue )
329
344
}
330
345
}
331
346
332
347
// process single fields
333
348
for _ , field := range w .singleFields {
349
+ column := pass .Fset .Position (field .Tag .Pos ()).Column - 1
334
350
tag , err := strconv .Unquote (field .Tag .Value )
335
351
if err != nil {
352
+ w .report (pass , field , column , errTagValueSyntax , field .Tag .Value )
336
353
continue
337
354
}
338
355
339
356
tags , err := structtag .Parse (tag )
340
357
if err != nil {
358
+ w .report (pass , field , column , err .Error (), field .Tag .Value )
341
359
continue
342
360
}
343
361
originalTags := append ([]* structtag.Tag (nil ), tags .Tags ()... )
@@ -353,7 +371,7 @@ func (w *Helper) Process(pass *analysis.Pass) { //nolint:gocognit
353
371
354
372
msg := "tag is not aligned , should be: " + tags .String ()
355
373
356
- w .report (pass , field , pass . Fset . Position ( field . Tag . Pos ()). Column - 1 , msg , newTagValue )
374
+ w .report (pass , field , column , msg , newTagValue )
357
375
}
358
376
}
359
377
@@ -431,3 +449,11 @@ func max(a, b int) int {
431
449
}
432
450
return b
433
451
}
452
+
453
+ func removeField (fields []* ast.Field , index int ) []* ast.Field {
454
+ if index < 0 || index >= len (fields ) {
455
+ return fields
456
+ }
457
+
458
+ return append (fields [:index ], fields [index + 1 :]... )
459
+ }
0 commit comments