Skip to content

Commit 5f6395d

Browse files
committed
feature: custom issue reporter
1 parent 78286d2 commit 5f6395d

File tree

1 file changed

+51
-12
lines changed

1 file changed

+51
-12
lines changed

pkg/golinters/mirror.go

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,70 @@
11
package golinters
22

33
import (
4+
"sync"
5+
46
"github.com/butuzov/mirror"
57
"golang.org/x/tools/go/analysis"
68

79
"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"
812
)
913

1014
func NewMirror() *goanalysis.Linter {
15+
var ( // Issue reporter related.
16+
mu sync.Mutex
17+
issues []goanalysis.Issue
18+
)
19+
1120
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+
}
1233

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)
1836

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
2358
}
2459

25-
return goanalysis.NewLinter(
60+
analyzer := goanalysis.NewLinter(
2661
a.Name,
2762
a.Doc,
2863
[]*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
3170
}

0 commit comments

Comments
 (0)