forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseverity_rules.go
104 lines (87 loc) · 2.49 KB
/
severity_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
package processors
import (
"regexp"
"github.com/golangci/golangci-lint/pkg/fsutils"
"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/result"
)
type severityRule struct {
baseRule
severity string
}
type SeverityRule struct {
BaseRule
Severity string
}
type SeverityRules struct {
defaultSeverity string
rules []severityRule
files *fsutils.Files
log logutils.Log
}
func NewSeverityRules(defaultSeverity string, rules []SeverityRule, files *fsutils.Files, log logutils.Log) *SeverityRules {
r := &SeverityRules{
files: files,
log: log,
defaultSeverity: defaultSeverity,
}
r.rules = createSeverityRules(rules, "(?i)")
return r
}
func createSeverityRules(rules []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)
}
parsedRules = append(parsedRules, parsedRule)
}
return parsedRules
}
func (p SeverityRules) Process(issues []result.Issue) ([]result.Issue, error) {
if len(p.rules) == 0 && p.defaultSeverity == "" {
return issues, nil
}
return transformIssues(issues, func(i *result.Issue) *result.Issue {
for _, rule := range p.rules {
rule := rule
ruleSeverity := p.defaultSeverity
if rule.severity != "" {
ruleSeverity = rule.severity
}
if rule.match(i, p.files, p.log) {
i.Severity = ruleSeverity
return i
}
}
i.Severity = p.defaultSeverity
return i
}), nil
}
func (SeverityRules) Name() string { return "severity-rules" }
func (SeverityRules) Finish() {}
var _ Processor = SeverityRules{}
type SeverityRulesCaseSensitive struct {
*SeverityRules
}
func NewSeverityRulesCaseSensitive(defaultSeverity string, rules []SeverityRule,
files *fsutils.Files, log logutils.Log) *SeverityRulesCaseSensitive {
r := &SeverityRules{
files: files,
log: log,
defaultSeverity: defaultSeverity,
}
r.rules = createSeverityRules(rules, "")
return &SeverityRulesCaseSensitive{r}
}
func (SeverityRulesCaseSensitive) Name() string { return "severity-rules-case-sensitive" }