forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseverity.go
118 lines (88 loc) · 2.59 KB
/
severity.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
package processors
import (
"cmp"
"regexp"
"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"
)
const severityFromLinter = "@linter"
var _ Processor = (*Severity)(nil)
type severityRule struct {
baseRule
severity string
}
// Severity modifies report severity.
// It uses the same `baseRule` structure as [ExcludeRules] processor.
//
// Warning: it doesn't use `path-prefix` option.
type Severity struct {
name string
log logutils.Log
files *fsutils.Files
defaultSeverity string
rules []severityRule
}
func NewSeverity(log logutils.Log, files *fsutils.Files, cfg *config.Severity) *Severity {
p := &Severity{
name: "severity-rules",
files: files,
log: log,
defaultSeverity: cfg.Default,
}
prefix := caseInsensitivePrefix
if cfg.CaseSensitive {
prefix = ""
p.name = "severity-rules-case-sensitive"
}
p.rules = createSeverityRules(cfg.Rules, prefix)
return p
}
func (p *Severity) Name() string { return p.name }
func (p *Severity) Process(issues []result.Issue) ([]result.Issue, error) {
if len(p.rules) == 0 && p.defaultSeverity == "" {
return issues, nil
}
return transformIssues(issues, p.transform), nil
}
func (*Severity) Finish() {}
func (p *Severity) transform(issue *result.Issue) *result.Issue {
for _, rule := range p.rules {
if rule.match(issue, p.files, p.log) {
if rule.severity == severityFromLinter || (rule.severity == "" && p.defaultSeverity == severityFromLinter) {
return issue
}
issue.Severity = cmp.Or(rule.severity, p.defaultSeverity)
return issue
}
}
if p.defaultSeverity != severityFromLinter {
issue.Severity = p.defaultSeverity
}
return issue
}
func createSeverityRules(rules []config.SeverityRule, prefix string) []severityRule {
parsedRules := make([]severityRule, 0, len(rules))
for _, rule := range rules {
parsedRule := severityRule{}
parsedRule.linters = rule.Linters
parsedRule.severity = rule.Severity
if rule.Text != "" {
parsedRule.text = regexp.MustCompile(prefix + rule.Text)
}
if rule.Source != "" {
parsedRule.source = regexp.MustCompile(prefix + rule.Source)
}
if rule.Path != "" {
path := fsutils.NormalizePathInRegex(rule.Path)
parsedRule.path = regexp.MustCompile(path)
}
if rule.PathExcept != "" {
pathExcept := fsutils.NormalizePathInRegex(rule.PathExcept)
parsedRule.pathExcept = regexp.MustCompile(pathExcept)
}
parsedRules = append(parsedRules, parsedRule)
}
return parsedRules
}