|
| 1 | +package golinters |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + |
| 6 | + "github.com/4meepo/tagalign" |
| 7 | + "golang.org/x/tools/go/analysis" |
| 8 | + |
| 9 | + "github.com/golangci/golangci-lint/pkg/config" |
| 10 | + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" |
| 11 | + "github.com/golangci/golangci-lint/pkg/lint/linter" |
| 12 | + "github.com/golangci/golangci-lint/pkg/result" |
| 13 | +) |
| 14 | + |
| 15 | +func NewTagAlign(settings *config.TagAlignSettings) *goanalysis.Linter { |
| 16 | + var mu sync.Mutex |
| 17 | + var resIssues []goanalysis.Issue |
| 18 | + |
| 19 | + options := []tagalign.Option{tagalign.WithMode(tagalign.GolangciLintMode)} |
| 20 | + |
| 21 | + if settings != nil { |
| 22 | + options = append(options, tagalign.WithAlign(settings.Align)) |
| 23 | + |
| 24 | + if settings.Sort || len(settings.Order) > 0 { |
| 25 | + options = append(options, tagalign.WithSort(settings.Order...)) |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + analyzer := tagalign.NewAnalyzer(options...) |
| 30 | + analyzer.Run = func(pass *analysis.Pass) (any, error) { |
| 31 | + taIssues := tagalign.Run(pass, options...) |
| 32 | + |
| 33 | + issues := make([]goanalysis.Issue, len(taIssues)) |
| 34 | + for i, issue := range taIssues { |
| 35 | + report := &result.Issue{ |
| 36 | + FromLinter: analyzer.Name, |
| 37 | + Pos: issue.Pos, |
| 38 | + Text: issue.Message, |
| 39 | + Replacement: &result.Replacement{ |
| 40 | + Inline: &result.InlineFix{ |
| 41 | + StartCol: issue.InlineFix.StartCol, |
| 42 | + Length: issue.InlineFix.Length, |
| 43 | + NewString: issue.InlineFix.NewString, |
| 44 | + }, |
| 45 | + }, |
| 46 | + } |
| 47 | + |
| 48 | + issues[i] = goanalysis.NewIssue(report, pass) |
| 49 | + } |
| 50 | + |
| 51 | + if len(issues) == 0 { |
| 52 | + return nil, nil |
| 53 | + } |
| 54 | + |
| 55 | + mu.Lock() |
| 56 | + resIssues = append(resIssues, issues...) |
| 57 | + mu.Unlock() |
| 58 | + |
| 59 | + return nil, nil |
| 60 | + } |
| 61 | + |
| 62 | + return goanalysis.NewLinter( |
| 63 | + analyzer.Name, |
| 64 | + analyzer.Doc, |
| 65 | + []*analysis.Analyzer{analyzer}, |
| 66 | + nil, |
| 67 | + ).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { |
| 68 | + return resIssues |
| 69 | + }).WithLoadMode(goanalysis.LoadModeSyntax) |
| 70 | +} |
0 commit comments