github.com/vanstinator/golangci-lint@v0.0.0-20240223191551-cc572f00d9d1/pkg/golinters/tagalign.go (about) 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/vanstinator/golangci-lint/pkg/config" 10 "github.com/vanstinator/golangci-lint/pkg/golinters/goanalysis" 11 "github.com/vanstinator/golangci-lint/pkg/lint/linter" 12 "github.com/vanstinator/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 // Strict style will be applied only if Align and Sort are enabled together. 29 if settings.Strict && settings.Align && settings.Sort { 30 options = append(options, tagalign.WithStrictStyle()) 31 } 32 } 33 34 analyzer := tagalign.NewAnalyzer(options...) 35 analyzer.Run = func(pass *analysis.Pass) (any, error) { 36 taIssues := tagalign.Run(pass, options...) 37 38 issues := make([]goanalysis.Issue, len(taIssues)) 39 for i, issue := range taIssues { 40 report := &result.Issue{ 41 FromLinter: analyzer.Name, 42 Pos: issue.Pos, 43 Text: issue.Message, 44 Replacement: &result.Replacement{ 45 Inline: &result.InlineFix{ 46 StartCol: issue.InlineFix.StartCol, 47 Length: issue.InlineFix.Length, 48 NewString: issue.InlineFix.NewString, 49 }, 50 }, 51 } 52 53 issues[i] = goanalysis.NewIssue(report, pass) 54 } 55 56 if len(issues) == 0 { 57 return nil, nil 58 } 59 60 mu.Lock() 61 resIssues = append(resIssues, issues...) 62 mu.Unlock() 63 64 return nil, nil 65 } 66 67 return goanalysis.NewLinter( 68 analyzer.Name, 69 analyzer.Doc, 70 []*analysis.Analyzer{analyzer}, 71 nil, 72 ).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { 73 return resIssues 74 }).WithLoadMode(goanalysis.LoadModeSyntax) 75 }