Skip to content

Commit 8a15bce

Browse files
committed
optimize code and fix error offset in golangci-lint mode
1 parent 79876a7 commit 8a15bce

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

tagalign.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -211,25 +211,28 @@ func (w *Helper) Process(pass *analysis.Pass) { //nolint:gocognit
211211
}
212212
uniqueKeys = append(uniqueKeys, k)
213213
}
214-
mark := make(map[int]struct{})
215214

216-
for i, field := range fields {
215+
for i := 0; i < len(fields); {
216+
field := fields[i]
217217
column := pass.Fset.Position(field.Tag.Pos()).Column - 1
218+
offsets[i] = column
219+
218220
tag, err := strconv.Unquote(field.Tag.Value)
219221
if err != nil {
220-
mark[i] = struct{}{}
222+
// if tag value is not a valid string, report it directly
221223
w.report(pass, field, column, errTagValueSyntax, field.Tag.Value)
224+
fields = append(fields[:i], fields[i+1:]...)
222225
continue
223226
}
224227

225228
tags, err := structtag.Parse(tag)
226229
if err != nil {
227-
mark[i] = struct{}{}
230+
// if tag value is not a valid struct tag, report it directly
228231
w.report(pass, field, column, err.Error(), field.Tag.Value)
232+
fields = append(fields[:i], fields[i+1:]...)
229233
continue
230234
}
231235

232-
offsets = append(offsets, column)
233236
maxTagNum = max(maxTagNum, tags.Len())
234237

235238
if w.sort {
@@ -244,18 +247,9 @@ func (w *Helper) Process(pass *analysis.Pass) { //nolint:gocognit
244247
addKey(t.Key)
245248
}
246249
tagsGroup = append(tagsGroup, tags.Tags())
247-
}
248-
249-
offset := 0
250-
for i := range fields {
251-
if _, exist := mark[i]; exist {
252-
continue
253-
}
254250

255-
fields[offset] = fields[i]
256-
offset++
251+
i++
257252
}
258-
fields = fields[:offset]
259253

260254
if w.sort && StrictStyle == w.style {
261255
sortAllKeys(w.fixedTagOrder, uniqueKeys)

tagalign_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ func Test_alignSingleField(t *testing.T) {
7474
analysistest.Run(t, unsort, a)
7575
}
7676

77-
func Test_issues6(t *testing.T) {
77+
func Test_badSyntaxTag(t *testing.T) {
7878
// only align
7979
a := NewAnalyzer()
80-
unsort, err := filepath.Abs("testdata/issues6")
80+
unsort, err := filepath.Abs("testdata/bad_syntax_tag")
8181
assert.NoError(t, err)
8282
analysistest.Run(t, unsort, a)
8383
}

testdata/issues6/example.go renamed to testdata/bad_syntax_tag/example.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,12 @@ type FooBar struct {
66
FooFoo int8 `json:"foo_foo" validate:"required"`
77
BarBar int `json:"bar_bar" validate:"required"`
88
}
9+
10+
type FooBar2 struct {
11+
Foo int `json:"foo" validate:"required"`
12+
13+
Bar string `json:bar` // want `bad syntax for struct tag value`
14+
15+
FooFoo int8 `json:"foo_foo"`
16+
BarBar int `json:"bar_bar" validate:"required"`
17+
}

0 commit comments

Comments
 (0)