/
max_per_file_from_linter.go
59 lines (47 loc) · 1.48 KB
/
max_per_file_from_linter.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
package processors
import (
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/result"
)
type linterToCountMap map[string]int
type fileToLinterToCountMap map[string]linterToCountMap
type MaxPerFileFromLinter struct {
flc fileToLinterToCountMap
maxPerFileFromLinterConfig map[string]int
}
var _ Processor = &MaxPerFileFromLinter{}
func NewMaxPerFileFromLinter(cfg *config.Config) *MaxPerFileFromLinter {
maxPerFileFromLinterConfig := map[string]int{}
if !cfg.Issues.NeedFix {
// if we don't fix we do this limiting to not annoy user;
// otherwise we need to fix all issues in the file at once
maxPerFileFromLinterConfig["gofmt"] = 1
maxPerFileFromLinterConfig["goimports"] = 1
}
return &MaxPerFileFromLinter{
flc: fileToLinterToCountMap{},
maxPerFileFromLinterConfig: maxPerFileFromLinterConfig,
}
}
func (p MaxPerFileFromLinter) Name() string {
return "max_per_file_from_linter"
}
func (p *MaxPerFileFromLinter) Process(issues []result.Issue) ([]result.Issue, error) {
return filterIssues(issues, func(i *result.Issue) bool {
limit := p.maxPerFileFromLinterConfig[i.FromLinter]
if limit == 0 {
return true
}
lm := p.flc[i.FilePath()]
if lm == nil {
p.flc[i.FilePath()] = linterToCountMap{}
}
count := p.flc[i.FilePath()][i.FromLinter]
if count >= limit {
return false
}
p.flc[i.FilePath()][i.FromLinter]++
return true
}), nil
}
func (p MaxPerFileFromLinter) Finish() {}