-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package golinters | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/4meepo/tagalign" | ||
"golang.org/x/tools/go/analysis" | ||
|
||
"github.com/golangci/golangci-lint/pkg/config" | ||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" | ||
"github.com/golangci/golangci-lint/pkg/lint/linter" | ||
"github.com/golangci/golangci-lint/pkg/result" | ||
) | ||
|
||
func NewTagAlign(settings *config.TagAlignSettings) *goanalysis.Linter { | ||
var mu sync.Mutex | ||
var resIssues []goanalysis.Issue | ||
|
||
options := []tagalign.Option{tagalign.WithMode(tagalign.GolangciLintMode)} | ||
|
||
if settings != nil { | ||
options = append(options, tagalign.WithAlign(settings.Align)) | ||
|
||
if settings.Sort || len(settings.Order) > 0 { | ||
options = append(options, tagalign.WithSort(settings.Order...)) | ||
} | ||
} | ||
|
||
analyzer := tagalign.NewAnalyzer(options...) | ||
analyzer.Run = func(pass *analysis.Pass) (any, error) { | ||
taIssues := tagalign.Run(pass, options...) | ||
|
||
issues := make([]goanalysis.Issue, len(taIssues)) | ||
for i, issue := range taIssues { | ||
report := &result.Issue{ | ||
FromLinter: analyzer.Name, | ||
Pos: issue.Pos, | ||
Text: issue.Message, | ||
Replacement: &result.Replacement{ | ||
Inline: &result.InlineFix{ | ||
StartCol: issue.InlineFix.StartCol, | ||
Length: issue.InlineFix.Length, | ||
NewString: issue.InlineFix.NewString, | ||
}, | ||
}, | ||
} | ||
|
||
issues[i] = goanalysis.NewIssue(report, pass) | ||
} | ||
|
||
if len(issues) == 0 { | ||
return nil, nil | ||
} | ||
|
||
mu.Lock() | ||
resIssues = append(resIssues, issues...) | ||
mu.Unlock() | ||
|
||
return nil, nil | ||
} | ||
|
||
return goanalysis.NewLinter( | ||
analyzer.Name, | ||
analyzer.Doc, | ||
[]*analysis.Analyzer{analyzer}, | ||
nil, | ||
).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { | ||
return resIssues | ||
}).WithLoadMode(goanalysis.LoadModeSyntax) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
linters-settings: | ||
tagalign: | ||
sort: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
linters-settings: | ||
tagalign: | ||
align: false | ||
order: | ||
- "xml" | ||
- "json" | ||
- "yaml" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
linters-settings: | ||
tagalign: | ||
align: false | ||
sort: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//golangcitest:args -Etagalign | ||
package testdata | ||
|
||
import "time" | ||
|
||
type TagAlignExampleAlignSort struct { | ||
Foo time.Duration `json:"foo,omitempty" yaml:"foo" xml:"foo" binding:"required" gorm:"column:foo" zip:"foo" validate:"required"` // want `binding:"required" gorm:"column:foo" json:"foo,omitempty" validate:"required" xml:"foo" yaml:"foo" zip:"foo"` | ||
Bar int `validate:"required" yaml:"bar" xml:"bar" binding:"required" json:"bar,omitempty" gorm:"column:bar" zip:"bar" ` // want `binding:"required" gorm:"column:bar" json:"bar,omitempty" validate:"required" xml:"bar" yaml:"bar" zip:"bar"` | ||
FooBar int `gorm:"column:fooBar" validate:"required" xml:"fooBar" binding:"required" json:"fooBar,omitempty" zip:"fooBar" yaml:"fooBar"` // want `binding:"required" gorm:"column:fooBar" json:"fooBar,omitempty" validate:"required" xml:"fooBar" yaml:"fooBar" zip:"fooBar"` | ||
} | ||
|
||
type TagAlignExampleAlignSort2 struct { | ||
Foo int ` xml:"foo" json:"foo,omitempty" yaml:"foo" zip:"foo" binding:"required" gorm:"column:foo" validate:"required"` // want `binding:"required" gorm:"column:foo" json:"foo,omitempty" validate:"required" xml:"foo" yaml:"foo" zip:"foo"` | ||
Bar int `validate:"required" gorm:"column:bar" yaml:"bar" xml:"bar" binding:"required" json:"bar" zip:"bar" ` // want `binding:"required" gorm:"column:bar" json:"bar" validate:"required" xml:"bar" yaml:"bar" zip:"bar"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//golangcitest:args -Etagalign | ||
//golangcitest:config_path testdata/configs/tagalign_align_only.yml | ||
package testdata | ||
|
||
import "time" | ||
|
||
type TagAlignExampleAlignOnlyKO struct { | ||
Foo time.Time `gorm:"column:foo" json:"foo,omitempty" xml:"foo" yaml:"foo" zip:"foo"` // want `gorm:"column:foo" json:"foo,omitempty" xml:"foo" yaml:"foo" zip:"foo"` | ||
FooBar struct{} `gorm:"column:fooBar" zip:"fooBar" json:"fooBar,omitempty" xml:"fooBar" yaml:"fooBar"` // want `gorm:"column:fooBar" zip:"fooBar" json:"fooBar,omitempty" xml:"fooBar" yaml:"fooBar"` | ||
FooFoo struct { | ||
Foo int `json:"foo" yaml:"foo"` // want `json:"foo" yaml:"foo"` | ||
Bar int `yaml:"bar" json:"bar"` // want `yaml:"bar" json:"bar"` | ||
BarBar string `json:"barBar" yaml:"barBar"` | ||
} `xml:"fooFoo" json:"fooFoo"` | ||
NoTag struct{} | ||
BarBar struct{} `json:"barBar,omitempty" gorm:"column:barBar" yaml:"barBar" xml:"barBar" zip:"barBar"` | ||
Boo struct{} `gorm:"column:boo" json:"boo,omitempty" xml:"boo" yaml:"boo" zip:"boo"` // want `gorm:"column:boo" json:"boo,omitempty" xml:"boo" yaml:"boo" zip:"boo"` | ||
} | ||
|
||
type TagAlignExampleAlignOnlyOK struct { | ||
Foo time.Time `gorm:"column:foo" json:"foo,omitempty" xml:"foo" yaml:"foo" zip:"foo"` | ||
FooBar struct{} `gorm:"column:fooBar" zip:"fooBar" json:"fooBar,omitempty" xml:"fooBar" yaml:"fooBar"` | ||
FooFoo struct { | ||
Foo int `json:"foo" yaml:"foo"` | ||
Bar int `yaml:"bar" json:"bar"` | ||
BarBar string `json:"barBar" yaml:"barBar"` | ||
} `xml:"fooFoo" json:"fooFoo"` | ||
NoTag struct{} | ||
BarBar struct{} `json:"barBar,omitempty" gorm:"column:barBar" yaml:"barBar" xml:"barBar" zip:"barBar"` | ||
Boo struct{} `gorm:"column:boo" json:"boo,omitempty" xml:"boo" yaml:"boo" zip:"boo"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//golangcitest:args -Etagalign | ||
//golangcitest:config_path testdata/configs/tagalign_order_only.yml | ||
package testdata | ||
|
||
import "time" | ||
|
||
type TagAlignExampleOrderOnlyKO struct { | ||
Foo time.Time `xml:"foo" json:"foo,omitempty" yaml:"foo" zip:"foo" gorm:"column:foo" validate:"required"` // want `xml:"foo" json:"foo,omitempty" yaml:"foo" gorm:"column:foo" validate:"required" zip:"foo"` | ||
FooBar struct{} `gorm:"column:fooBar" validate:"required" zip:"fooBar" xml:"fooBar" json:"fooBar,omitempty" yaml:"fooBar"` // want `xml:"fooBar" json:"fooBar,omitempty" yaml:"fooBar" gorm:"column:fooBar" validate:"required" zip:"fooBar"` | ||
} | ||
|
||
type TagAlignExampleOrderOnlyOK struct { | ||
Foo time.Time `xml:"foo" json:"foo,omitempty" yaml:"foo" gorm:"column:foo" validate:"required" zip:"foo"` | ||
FooBar struct{} `xml:"fooBar" json:"fooBar,omitempty" yaml:"fooBar" gorm:"column:fooBar" validate:"required" zip:"fooBar"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//golangcitest:args -Etagalign | ||
//golangcitest:config_path testdata/configs/tagalign_sort_only.yml | ||
package testdata | ||
|
||
import "time" | ||
|
||
type TagAlignExampleSortOnlyKO struct { | ||
Foo time.Time `xml:"foo" json:"foo,omitempty" yaml:"foo" gorm:"column:foo" validate:"required" zip:"foo"` // want `gorm:"column:foo" json:"foo,omitempty" validate:"required" xml:"foo" yaml:"foo" zip:"foo"` | ||
FooBar struct{} `gorm:"column:fooBar" validate:"required" zip:"fooBar" xml:"fooBar" json:"fooBar,omitempty" yaml:"fooBar"` // want `gorm:"column:fooBar" json:"fooBar,omitempty" validate:"required" xml:"fooBar" yaml:"fooBar" zip:"fooBar"` | ||
} | ||
|
||
type TagAlignExampleSortOnlyOK struct { | ||
Foo time.Time `gorm:"column:foo" json:"foo,omitempty" validate:"required" xml:"foo" yaml:"foo" zip:"foo"` | ||
FooBar struct{} `gorm:"column:fooBar" json:"fooBar,omitempty" validate:"required" xml:"fooBar" yaml:"fooBar" zip:"fooBar"` | ||
} |