Skip to content

Commit 15a92de

Browse files
authored
dev: factorize base rule parsing (#5360)
1 parent 87da074 commit 15a92de

File tree

3 files changed

+63
-77
lines changed

3 files changed

+63
-77
lines changed

Diff for: pkg/result/processors/base_rule.go

+40-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package processors
33
import (
44
"regexp"
55

6+
"github.com/golangci/golangci-lint/pkg/config"
67
"github.com/golangci/golangci-lint/pkg/fsutils"
78
"github.com/golangci/golangci-lint/pkg/logutils"
89
"github.com/golangci/golangci-lint/pkg/result"
@@ -16,9 +17,32 @@ type baseRule struct {
1617
path *regexp.Regexp
1718
pathExcept *regexp.Regexp
1819
linters []string
20+
}
21+
22+
// The usage of `regexp.MustCompile()` is safe here,
23+
// because the regular expressions are checked before inside [config.BaseRule.Validate].
24+
func newBaseRule(rule *config.BaseRule, prefix string) baseRule {
25+
base := baseRule{
26+
linters: rule.Linters,
27+
}
28+
29+
if rule.Text != "" {
30+
base.text = regexp.MustCompile(prefix + rule.Text)
31+
}
32+
33+
if rule.Source != "" {
34+
base.source = regexp.MustCompile(prefix + rule.Source)
35+
}
36+
37+
if rule.Path != "" {
38+
base.path = regexp.MustCompile(fsutils.NormalizePathInRegex(rule.Path))
39+
}
1940

20-
// For compatibility with exclude-use-default/include.
21-
internalReference string `mapstructure:"-"`
41+
if rule.PathExcept != "" {
42+
base.pathExcept = regexp.MustCompile(fsutils.NormalizePathInRegex(rule.PathExcept))
43+
}
44+
45+
return base
2246
}
2347

2448
func (r *baseRule) isEmpty() bool {
@@ -69,3 +93,17 @@ func (r *baseRule) matchSource(issue *result.Issue, lineCache *fsutils.LineCache
6993

7094
return r.source.MatchString(sourceLine)
7195
}
96+
97+
func parseRules[T, V any](rules []T, prefix string, newFn func(*T, string) V) []V {
98+
if len(rules) == 0 {
99+
return nil
100+
}
101+
102+
parsedRules := make([]V, 0, len(rules))
103+
104+
for _, r := range rules {
105+
parsedRules = append(parsedRules, newFn(&r, prefix))
106+
}
107+
108+
return parsedRules
109+
}

Diff for: pkg/result/processors/exclusion_rules.go

+14-40
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package processors
22

33
import (
44
"fmt"
5-
"regexp"
65
"slices"
76
"strings"
87

@@ -24,7 +23,8 @@ type ExclusionRules struct {
2423
rules []excludeRule
2524
}
2625

27-
func NewExclusionRules(log logutils.Log, files *fsutils.Files, cfg *config.LinterExclusions, oldCfg *config.Issues) *ExclusionRules {
26+
func NewExclusionRules(log logutils.Log, files *fsutils.Files,
27+
cfg *config.LinterExclusions, oldCfg *config.Issues) *ExclusionRules {
2828
p := &ExclusionRules{
2929
log: log,
3030
files: files,
@@ -41,20 +41,22 @@ func NewExclusionRules(log logutils.Log, files *fsutils.Files, cfg *config.Linte
4141
excludeRules := slices.Concat(slices.Clone(cfg.Rules),
4242
filterInclude(getLinterExclusionPresets(cfg.Presets), oldCfg.IncludeDefaultExcludes))
4343

44-
p.rules = createExcludeRules(excludeRules, prefix)
44+
p.rules = parseRules(excludeRules, prefix, newExcludeRule)
4545

4646
// TODO(ldez): should be removed in v2.
4747
for _, pattern := range oldCfg.ExcludePatterns {
4848
if pattern == "" {
4949
continue
5050
}
5151

52-
rule := newRule(&config.ExcludeRule{
52+
r := &config.ExcludeRule{
5353
BaseRule: config.BaseRule{
5454
Path: `.+\.go`,
5555
Text: pattern,
5656
},
57-
}, prefix)
57+
}
58+
59+
rule := newExcludeRule(r, prefix)
5860

5961
p.rules = append(p.rules, rule)
6062
}
@@ -107,30 +109,16 @@ func (p *ExclusionRules) Finish() {
107109

108110
type excludeRule struct {
109111
baseRule
110-
}
111-
112-
func newRule(rule *config.ExcludeRule, prefix string) excludeRule {
113-
parsedRule := excludeRule{}
114-
parsedRule.linters = rule.Linters
115-
parsedRule.internalReference = rule.InternalReference
116-
117-
if rule.Text != "" {
118-
parsedRule.text = regexp.MustCompile(prefix + rule.Text)
119-
}
120-
121-
if rule.Source != "" {
122-
parsedRule.source = regexp.MustCompile(prefix + rule.Source)
123-
}
124112

125-
if rule.Path != "" {
126-
parsedRule.path = regexp.MustCompile(fsutils.NormalizePathInRegex(rule.Path))
127-
}
113+
// For compatibility with exclude-use-default/include.
114+
internalReference string `mapstructure:"-"`
115+
}
128116

129-
if rule.PathExcept != "" {
130-
parsedRule.pathExcept = regexp.MustCompile(fsutils.NormalizePathInRegex(rule.PathExcept))
117+
func newExcludeRule(rule *config.ExcludeRule, prefix string) excludeRule {
118+
return excludeRule{
119+
baseRule: newBaseRule(&rule.BaseRule, prefix),
120+
internalReference: rule.InternalReference,
131121
}
132-
133-
return parsedRule
134122
}
135123

136124
func (e excludeRule) String() string {
@@ -159,20 +147,6 @@ func (e excludeRule) String() string {
159147
return strings.Join(msg, ", ")
160148
}
161149

162-
func createExcludeRules(rules []config.ExcludeRule, prefix string) []excludeRule {
163-
if len(rules) == 0 {
164-
return nil
165-
}
166-
167-
parsedRules := make([]excludeRule, 0, len(rules))
168-
169-
for _, rule := range rules {
170-
parsedRules = append(parsedRules, newRule(&rule, prefix))
171-
}
172-
173-
return parsedRules
174-
}
175-
176150
// TODO(ldez): must be removed in v2, only for compatibility with exclude-use-default/include.
177151
func filterInclude(rules []config.ExcludeRule, refs []string) []config.ExcludeRule {
178152
if len(refs) == 0 {

Diff for: pkg/result/processors/severity.go

+9-35
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package processors
22

33
import (
44
"cmp"
5-
"regexp"
65

76
"github.com/golangci/golangci-lint/pkg/config"
87
"github.com/golangci/golangci-lint/pkg/fsutils"
@@ -14,11 +13,6 @@ const severityFromLinter = "@linter"
1413

1514
var _ Processor = (*Severity)(nil)
1615

17-
type severityRule struct {
18-
baseRule
19-
severity string
20-
}
21-
2216
// Severity modifies report severity.
2317
// It uses the same `baseRule` structure as [ExcludeRules] processor.
2418
//
@@ -48,7 +42,7 @@ func NewSeverity(log logutils.Log, files *fsutils.Files, cfg *config.Severity) *
4842
p.name = "severity-rules-case-sensitive"
4943
}
5044

51-
p.rules = createSeverityRules(cfg.Rules, prefix)
45+
p.rules = parseRules(cfg.Rules, prefix, newSeverityRule)
5246

5347
return p
5448
}
@@ -85,34 +79,14 @@ func (p *Severity) transform(issue *result.Issue) *result.Issue {
8579
return issue
8680
}
8781

88-
func createSeverityRules(rules []config.SeverityRule, prefix string) []severityRule {
89-
parsedRules := make([]severityRule, 0, len(rules))
90-
91-
for _, rule := range rules {
92-
parsedRule := severityRule{}
93-
parsedRule.linters = rule.Linters
94-
parsedRule.severity = rule.Severity
95-
96-
if rule.Text != "" {
97-
parsedRule.text = regexp.MustCompile(prefix + rule.Text)
98-
}
99-
100-
if rule.Source != "" {
101-
parsedRule.source = regexp.MustCompile(prefix + rule.Source)
102-
}
103-
104-
if rule.Path != "" {
105-
path := fsutils.NormalizePathInRegex(rule.Path)
106-
parsedRule.path = regexp.MustCompile(path)
107-
}
108-
109-
if rule.PathExcept != "" {
110-
pathExcept := fsutils.NormalizePathInRegex(rule.PathExcept)
111-
parsedRule.pathExcept = regexp.MustCompile(pathExcept)
112-
}
82+
type severityRule struct {
83+
baseRule
84+
severity string
85+
}
11386

114-
parsedRules = append(parsedRules, parsedRule)
87+
func newSeverityRule(rule *config.SeverityRule, prefix string) severityRule {
88+
return severityRule{
89+
baseRule: newBaseRule(&rule.BaseRule, prefix),
90+
severity: rule.Severity,
11591
}
116-
117-
return parsedRules
11892
}

0 commit comments

Comments
 (0)