diff --git a/tagalign.go b/tagalign.go index 8b9d9ef..1f61486 100644 --- a/tagalign.go +++ b/tagalign.go @@ -340,14 +340,9 @@ func (w *Helper) Process(pass *analysis.Pass) { //nolint:gocognit sortBy(w.fixedTagOrder, tags) } - if reflect.DeepEqual(originalTags, tags.Tags()) { - // if tags order not changed, do nothing - continue - } - newTagValue := fmt.Sprintf("`%s`", tags.String()) - if field.Tag.Value == newTagValue { - // nothing changed + if reflect.DeepEqual(originalTags, tags.Tags()) && field.Tag.Value == newTagValue { + // if tags order not changed, do nothing continue } diff --git a/tagalign_test.go b/tagalign_test.go index 0c3cdbb..abf6945 100644 --- a/tagalign_test.go +++ b/tagalign_test.go @@ -64,3 +64,11 @@ func Test_strictStyle(t *testing.T) { assert.NoError(t, err) analysistest.Run(t, sort, a) } + +func Test_alignSingleField(t *testing.T) { + // only align + a := NewAnalyzer() + unsort, err := filepath.Abs("testdata/single_field") + assert.NoError(t, err) + analysistest.Run(t, unsort, a) +} diff --git a/testdata/single_field/example.go b/testdata/single_field/example.go new file mode 100644 index 0000000..df830df --- /dev/null +++ b/testdata/single_field/example.go @@ -0,0 +1,10 @@ +package singlefield + +type FooBar struct { + Foo int `json:"foo" validate:"required"` + Bar string `json:"bar" validate:"required"` + + FooFoo int8 `json:"foo_foo" validate:"required"` // want `json:"foo_foo" validate:"required"` + + BarBar int `json:"bar_bar" validate:"required"` +} diff --git a/testdata/sortorder/example.go b/testdata/sortorder/example.go index 7befbf1..3d8eedf 100644 --- a/testdata/sortorder/example.go +++ b/testdata/sortorder/example.go @@ -7,6 +7,6 @@ type SortWithOrderExample struct { FooBar int `xml:"bar" json:"bar,omitempty" yaml:"foo" gorm:"column:bar" ` // aligned but not sorted, should be reported BarFoo int `xml:"bar" yaml:"foo" json:"bar,omitempty" gorm:"column:bar" validate:"required" zip:"bar"` // want `xml:"bar" json:"bar,omitempty" yaml:"foo" gorm:"column:bar" validate:"required" zip:"bar"` - // not aligned but sorted, should not be reported - FooBarFoo int `xml:"bar" json:"bar,omitempty" yaml:"foo" gorm:"column:bar" validate:"required" zip:"bar"` + // not aligned but sorted, should trim spaces between tags + FooBarFoo int `xml:"bar" json:"bar,omitempty" yaml:"foo" gorm:"column:bar" validate:"required" zip:"bar"` // want `xml:"bar" json:"bar,omitempty" yaml:"foo" gorm:"column:bar" validate:"required" zip:"bar"` }