golangci-lint/pkg/result/processors/max_same_issues.go
Denis Isaev 3d78f64b60 fix #522: run misspell in text mode
Treat Go source files as plain text files by misspell: it allows detecting
issues in strings, variable names, etc. Also, it's the default mode of
a standalone misspell tool.

Also, implement richer and more stable auto-fix of misspell issues:
now it can fix multiple issues in one line.
2019-06-09 20:14:19 +03:00

83 lines
1.6 KiB
Go

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"
)
type textToCountMap map[string]int
type MaxSameIssues struct {
tc textToCountMap
limit int
log logutils.Log
cfg *config.Config
}
var _ Processor = &MaxSameIssues{}
func NewMaxSameIssues(limit int, log logutils.Log, cfg *config.Config) *MaxSameIssues {
return &MaxSameIssues{
tc: textToCountMap{},
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 filterIssues(issues, func(i *result.Issue) bool {
if i.Replacement != nil && p.cfg.Issues.NeedFix {
// we need to fix all issues at once => we need to return all of them
return true
}
p.tc[i.Text]++ // always inc for stat
return p.tc[i.Text] <= p.limit
}), nil
}
func (p MaxSameIssues) Finish() {
walkStringToIntMapSortedByValue(p.tc, 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)
}
}