|
| 1 | +package golinters |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + |
| 6 | + "github.com/ghostiam/protogetter" |
| 7 | + "golang.org/x/tools/go/analysis" |
| 8 | + |
| 9 | + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" |
| 10 | + "github.com/golangci/golangci-lint/pkg/lint/linter" |
| 11 | + "github.com/golangci/golangci-lint/pkg/result" |
| 12 | +) |
| 13 | + |
| 14 | +func NewProtoGetter() *goanalysis.Linter { |
| 15 | + var mu sync.Mutex |
| 16 | + var resIssues []goanalysis.Issue |
| 17 | + |
| 18 | + a := protogetter.NewAnalyzer() |
| 19 | + a.Run = func(pass *analysis.Pass) (any, error) { |
| 20 | + pgIssues := protogetter.Run(pass, protogetter.GolangciLintMode) |
| 21 | + |
| 22 | + issues := make([]goanalysis.Issue, len(pgIssues)) |
| 23 | + for i, issue := range pgIssues { |
| 24 | + report := &result.Issue{ |
| 25 | + FromLinter: a.Name, |
| 26 | + Pos: issue.Pos, |
| 27 | + Text: issue.Message, |
| 28 | + Replacement: &result.Replacement{ |
| 29 | + Inline: &result.InlineFix{ |
| 30 | + StartCol: issue.InlineFix.StartCol, |
| 31 | + Length: issue.InlineFix.Length, |
| 32 | + NewString: issue.InlineFix.NewString, |
| 33 | + }, |
| 34 | + }, |
| 35 | + } |
| 36 | + |
| 37 | + issues[i] = goanalysis.NewIssue(report, pass) |
| 38 | + } |
| 39 | + |
| 40 | + if len(issues) == 0 { |
| 41 | + return nil, nil |
| 42 | + } |
| 43 | + |
| 44 | + mu.Lock() |
| 45 | + resIssues = append(resIssues, issues...) |
| 46 | + mu.Unlock() |
| 47 | + |
| 48 | + return nil, nil |
| 49 | + } |
| 50 | + |
| 51 | + return goanalysis.NewLinter( |
| 52 | + a.Name, |
| 53 | + a.Doc, |
| 54 | + []*analysis.Analyzer{a}, |
| 55 | + nil, |
| 56 | + ).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { |
| 57 | + return resIssues |
| 58 | + }).WithLoadMode(goanalysis.LoadModeTypesInfo) |
| 59 | +} |
0 commit comments