forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseverity.go
86 lines (64 loc) · 1.86 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
package processors
import (
"cmp"
"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)
// 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
lines *fsutils.LineCache
defaultSeverity string
rules []severityRule
}
func NewSeverity(log logutils.Log, lines *fsutils.LineCache, cfg *config.Severity) *Severity {
p := &Severity{
name: "severity-rules",
lines: lines,
log: log,
defaultSeverity: cfg.Default,
}
p.rules = parseRules(cfg.Rules, "", newSeverityRule)
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.lines, 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
}
type severityRule struct {
baseRule
severity string
}
func newSeverityRule(rule *config.SeverityRule, prefix string) severityRule {
return severityRule{
baseRule: newBaseRule(&rule.BaseRule, prefix),
severity: rule.Severity,
}
}