github.com/nozzle/golangci-lint@v1.49.0-nz3/pkg/result/processors/severity_rules.go (about) 1 package processors 2 3 import ( 4 "regexp" 5 6 "github.com/golangci/golangci-lint/pkg/fsutils" 7 "github.com/golangci/golangci-lint/pkg/logutils" 8 "github.com/golangci/golangci-lint/pkg/result" 9 ) 10 11 type severityRule struct { 12 baseRule 13 severity string 14 } 15 16 type SeverityRule struct { 17 BaseRule 18 Severity string 19 } 20 21 type SeverityRules struct { 22 defaultSeverity string 23 rules []severityRule 24 lineCache *fsutils.LineCache 25 log logutils.Log 26 } 27 28 func NewSeverityRules(defaultSeverity string, rules []SeverityRule, lineCache *fsutils.LineCache, log logutils.Log) *SeverityRules { 29 r := &SeverityRules{ 30 lineCache: lineCache, 31 log: log, 32 defaultSeverity: defaultSeverity, 33 } 34 r.rules = createSeverityRules(rules, "(?i)") 35 36 return r 37 } 38 39 func createSeverityRules(rules []SeverityRule, prefix string) []severityRule { 40 parsedRules := make([]severityRule, 0, len(rules)) 41 for _, rule := range rules { 42 parsedRule := severityRule{} 43 parsedRule.linters = rule.Linters 44 parsedRule.severity = rule.Severity 45 if rule.Text != "" { 46 parsedRule.text = regexp.MustCompile(prefix + rule.Text) 47 } 48 if rule.Source != "" { 49 parsedRule.source = regexp.MustCompile(prefix + rule.Source) 50 } 51 if rule.Path != "" { 52 path := normalizePathInRegex(rule.Path) 53 parsedRule.path = regexp.MustCompile(path) 54 } 55 parsedRules = append(parsedRules, parsedRule) 56 } 57 return parsedRules 58 } 59 60 func (p SeverityRules) Process(issues []result.Issue) ([]result.Issue, error) { 61 if len(p.rules) == 0 && p.defaultSeverity == "" { 62 return issues, nil 63 } 64 return transformIssues(issues, func(i *result.Issue) *result.Issue { 65 for _, rule := range p.rules { 66 rule := rule 67 68 ruleSeverity := p.defaultSeverity 69 if rule.severity != "" { 70 ruleSeverity = rule.severity 71 } 72 73 if rule.match(i, p.lineCache, p.log) { 74 i.Severity = ruleSeverity 75 return i 76 } 77 } 78 i.Severity = p.defaultSeverity 79 return i 80 }), nil 81 } 82 83 func (SeverityRules) Name() string { return "severity-rules" } 84 func (SeverityRules) Finish() {} 85 86 var _ Processor = SeverityRules{} 87 88 type SeverityRulesCaseSensitive struct { 89 *SeverityRules 90 } 91 92 func NewSeverityRulesCaseSensitive(defaultSeverity string, rules []SeverityRule, 93 lineCache *fsutils.LineCache, log logutils.Log) *SeverityRulesCaseSensitive { 94 r := &SeverityRules{ 95 lineCache: lineCache, 96 log: log, 97 defaultSeverity: defaultSeverity, 98 } 99 r.rules = createSeverityRules(rules, "") 100 101 return &SeverityRulesCaseSensitive{r} 102 } 103 104 func (SeverityRulesCaseSensitive) Name() string { return "severity-rules-case-sensitive" }