forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexclusion_rules.go
123 lines (94 loc) · 2.69 KB
/
exclusion_rules.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package processors
import (
"fmt"
"slices"
"strings"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/fsutils"
"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/result"
)
var _ Processor = (*ExclusionRules)(nil)
type ExclusionRules struct {
log logutils.Log
lines *fsutils.LineCache
warnUnused bool
skippedCounter map[string]int
rules []excludeRule
}
func NewExclusionRules(log logutils.Log, lines *fsutils.LineCache, cfg *config.LinterExclusions) *ExclusionRules {
p := &ExclusionRules{
log: log,
lines: lines,
warnUnused: cfg.WarnUnused,
skippedCounter: map[string]int{},
}
excludeRules := slices.Concat(slices.Clone(cfg.Rules), getLinterExclusionPresets(cfg.Presets))
p.rules = parseRules(excludeRules, "", newExcludeRule)
for _, rule := range p.rules {
if rule.internalReference == "" {
p.skippedCounter[rule.String()] = 0
}
}
return p
}
func (*ExclusionRules) Name() string {
return "exclusion_rules"
}
func (p *ExclusionRules) Process(issues []result.Issue) ([]result.Issue, error) {
if len(p.rules) == 0 {
return issues, nil
}
return filterIssues(issues, func(issue *result.Issue) bool {
for _, rule := range p.rules {
if !rule.match(issue, p.lines, p.log) {
continue
}
// Ignore default rules.
if rule.internalReference == "" {
p.skippedCounter[rule.String()]++
}
return false
}
return true
}), nil
}
func (p *ExclusionRules) Finish() {
for rule, count := range p.skippedCounter {
if p.warnUnused && count == 0 {
p.log.Warnf("Skipped %d issues by rules: [%s]", count, rule)
} else {
p.log.Infof("Skipped %d issues by rules: [%s]", count, rule)
}
}
}
type excludeRule struct {
baseRule
// For compatibility with exclude-use-default/include.
internalReference string `mapstructure:"-"`
}
func newExcludeRule(rule *config.ExcludeRule, prefix string) excludeRule {
return excludeRule{
baseRule: newBaseRule(&rule.BaseRule, prefix),
internalReference: rule.InternalReference,
}
}
func (e excludeRule) String() string {
var msg []string
if e.text != nil && e.text.String() != "" {
msg = append(msg, fmt.Sprintf("Text: %q", e.text))
}
if e.source != nil && e.source.String() != "" {
msg = append(msg, fmt.Sprintf("Source: %q", e.source))
}
if e.path != nil && e.path.String() != "" {
msg = append(msg, fmt.Sprintf("Path: %q", e.path))
}
if e.pathExcept != nil && e.pathExcept.String() != "" {
msg = append(msg, fmt.Sprintf("Path Except: %q", e.pathExcept))
}
if len(e.linters) > 0 {
msg = append(msg, fmt.Sprintf("Linters: %q", strings.Join(e.linters, ", ")))
}
return strings.Join(msg, ", ")
}