|
1 | 1 | package golinters
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "sync" |
| 5 | + |
4 | 6 | "github.com/butuzov/mirror"
|
5 | 7 | "golang.org/x/tools/go/analysis"
|
6 | 8 |
|
7 | 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" |
8 | 12 | )
|
9 | 13 |
|
10 | 14 | func NewMirror() *goanalysis.Linter {
|
| 15 | + var ( // Issue reporter related. |
| 16 | + mu sync.Mutex |
| 17 | + issues []goanalysis.Issue |
| 18 | + ) |
| 19 | + |
11 | 20 | a := mirror.NewAnalyzer()
|
| 21 | + a.Run = func(pass *analysis.Pass) (any, error) { |
| 22 | + // mirror only lints test files if the `--with-tests` flag is passed, so we |
| 23 | + // pass the `with-tests` flag as true to the analyzer before running it. This |
| 24 | + // can be turned off by using the regular golangci-lint flags such as `--tests` |
| 25 | + // or `--skip-files` or can be disabled per linter via exclude rules (see |
| 26 | + // https://github.com/golangci/golangci-lint/issues/2527#issuecomment-1023707262) |
| 27 | + withTests := true |
| 28 | + violations := mirror.Run(pass, withTests) |
| 29 | + |
| 30 | + if len(violations) == 0 { |
| 31 | + return nil, nil |
| 32 | + } |
12 | 33 |
|
13 |
| - // mirror only lints test files if the `--with-tests` flag is passed, so we |
14 |
| - // pass the `with-tests` flag as true to the analyzer before running it. This |
15 |
| - // can be turned off by using the regular golangci-lint flags such as `--tests` |
16 |
| - // or `--skip-files` or can be disabled per linter via exclude rules (see |
17 |
| - // https://github.com/golangci/golangci-lint/issues/2527#issuecomment-1023707262) |
| 34 | + for i := range violations { |
| 35 | + tmp := violations[i].Issue(pass) |
18 | 36 |
|
19 |
| - cfg := map[string]map[string]any{ |
20 |
| - a.Name: { |
21 |
| - "with-tests": true, |
22 |
| - }, |
| 37 | + issue := result.Issue{ |
| 38 | + FromLinter: a.Name, |
| 39 | + Text: tmp.Message, |
| 40 | + Pos: tmp.Start, |
| 41 | + } |
| 42 | + |
| 43 | + if len(tmp.InlineFix) > 0 { |
| 44 | + issue.Replacement = &result.Replacement{ |
| 45 | + Inline: &result.InlineFix{ |
| 46 | + StartCol: tmp.Start.Column - 1, |
| 47 | + Length: len(tmp.Original), |
| 48 | + NewString: tmp.InlineFix, |
| 49 | + }, |
| 50 | + } |
| 51 | + } |
| 52 | + mu.Lock() |
| 53 | + issues = append(issues, goanalysis.NewIssue(&issue, pass)) |
| 54 | + mu.Unlock() |
| 55 | + } |
| 56 | + |
| 57 | + return nil, nil |
23 | 58 | }
|
24 | 59 |
|
25 |
| - return goanalysis.NewLinter( |
| 60 | + analyzer := goanalysis.NewLinter( |
26 | 61 | a.Name,
|
27 | 62 | a.Doc,
|
28 | 63 | []*analysis.Analyzer{a},
|
29 |
| - cfg, |
30 |
| - ).WithLoadMode(goanalysis.LoadModeWholeProgram) |
| 64 | + nil, |
| 65 | + ).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { |
| 66 | + return issues |
| 67 | + }).WithLoadMode(goanalysis.LoadModeTypesInfo) |
| 68 | + |
| 69 | + return analyzer |
31 | 70 | }
|
0 commit comments