forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmax_same_issues.go
76 lines (62 loc) · 1.6 KB
/
max_same_issues.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package processors
import (
"sort"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/result"
)
var _ Processor = (*MaxSameIssues)(nil)
// MaxSameIssues limits the number of reports with the same text.
type MaxSameIssues struct {
textCounter map[string]int
limit int
log logutils.Log
cfg *config.Config
}
func NewMaxSameIssues(limit int, log logutils.Log, cfg *config.Config) *MaxSameIssues {
return &MaxSameIssues{
textCounter: map[string]int{},
limit: limit,
log: log,
cfg: cfg,
}
}
func (*MaxSameIssues) Name() string {
return "max_same_issues"
}
func (p *MaxSameIssues) Process(issues []result.Issue) ([]result.Issue, error) {
if p.limit <= 0 { // no limit
return issues, nil
}
return filterIssuesUnsafe(issues, func(issue *result.Issue) bool {
p.textCounter[issue.Text]++ // always inc for stat
return p.textCounter[issue.Text] <= p.limit
}), nil
}
func (p *MaxSameIssues) Finish() {
walkStringToIntMapSortedByValue(p.textCounter, func(text string, count int) {
if count > p.limit {
p.log.Infof("%d/%d issues with text %q were hidden, use --max-same-issues",
count-p.limit, count, text)
}
})
}
type kv struct {
Key string
Value int
}
func walkStringToIntMapSortedByValue(m map[string]int, walk func(k string, v int)) {
var ss []kv
for k, v := range m {
ss = append(ss, kv{
Key: k,
Value: v,
})
}
sort.Slice(ss, func(i, j int) bool {
return ss[i].Value > ss[j].Value
})
for _, kv := range ss {
walk(kv.Key, kv.Value)
}
}