Skip to content

Commit a3a0455

Browse files
ernadojirfag
authored andcommitted
add support for exclude rules
1 parent b607ea3 commit a3a0455

File tree

4 files changed

+139
-2
lines changed

4 files changed

+139
-2
lines changed

pkg/config/config.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,16 @@ type Linters struct {
227227
Presets []string
228228
}
229229

230+
type ExcludeRule struct {
231+
Linters []string
232+
Path string
233+
Text string
234+
}
235+
230236
type Issues struct {
231-
ExcludePatterns []string `mapstructure:"exclude"`
232-
UseDefaultExcludes bool `mapstructure:"exclude-use-default"`
237+
ExcludePatterns []string `mapstructure:"exclude"`
238+
ExcludeRules []ExcludeRule `mapstructure:"exclude-rules"`
239+
UseDefaultExcludes bool `mapstructure:"exclude-use-default"`
233240

234241
MaxIssuesPerLinter int `mapstructure:"max-issues-per-linter"`
235242
MaxSameIssues int `mapstructure:"max-same-issues"`

pkg/lint/runner.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ func NewRunner(astCache *astcache.Cache, cfg *config.Config, log logutils.Log, g
4949
return nil, err
5050
}
5151

52+
var excludeRules []processors.ExcludeRule
53+
for _, r := range icfg.ExcludeRules {
54+
excludeRules = append(excludeRules, processors.ExcludeRule{
55+
Text: r.Text,
56+
Path: r.Path,
57+
Linters: r.Linters,
58+
})
59+
}
60+
5261
return &Runner{
5362
Processors: []processors.Processor{
5463
processors.NewPathPrettifier(), // must be before diff, nolint and exclude autogenerated processor at least
@@ -58,6 +67,7 @@ func NewRunner(astCache *astcache.Cache, cfg *config.Config, log logutils.Log, g
5867

5968
processors.NewAutogeneratedExclude(astCache),
6069
processors.NewExclude(excludeTotalPattern),
70+
processors.NewExcludeRules(excludeRules),
6171
processors.NewNolint(astCache, log.Child("nolint")),
6272

6373
processors.NewUniqByLine(),
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package processors
2+
3+
import (
4+
"regexp"
5+
6+
"github.com/golangci/golangci-lint/pkg/result"
7+
)
8+
9+
type excludeRule struct {
10+
text *regexp.Regexp
11+
path *regexp.Regexp
12+
linters []string
13+
}
14+
15+
func (r *excludeRule) isEmpty() bool {
16+
return r.text == nil && r.path == nil && len(r.linters) == 0
17+
}
18+
19+
func (r excludeRule) Match(i *result.Issue) bool {
20+
if r.isEmpty() {
21+
return false
22+
}
23+
if r.text != nil && !r.text.MatchString(i.Text) {
24+
return false
25+
}
26+
if r.path != nil && !r.path.MatchString(i.FilePath()) {
27+
return false
28+
}
29+
if len(r.linters) == 0 {
30+
return true
31+
}
32+
for _, l := range r.linters {
33+
if l == i.FromLinter {
34+
return true
35+
}
36+
}
37+
return false
38+
}
39+
40+
type ExcludeRule struct {
41+
Text string
42+
Path string
43+
Linters []string
44+
}
45+
46+
func NewExcludeRules(rules []ExcludeRule) *ExcludeRules {
47+
r := new(ExcludeRules)
48+
for _, rule := range rules {
49+
parsedRule := excludeRule{
50+
linters: rule.Linters,
51+
}
52+
if rule.Text != "" {
53+
parsedRule.text = regexp.MustCompile("(?i)" + rule.Text)
54+
}
55+
if rule.Path != "" {
56+
parsedRule.path = regexp.MustCompile(rule.Path)
57+
}
58+
// TODO: Forbid text-only, linter-only or path-only exclude rule.
59+
r.rules = append(r.rules, parsedRule)
60+
}
61+
return r
62+
}
63+
64+
type ExcludeRules struct {
65+
rules []excludeRule
66+
}
67+
68+
func (r ExcludeRules) Process(issues []result.Issue) ([]result.Issue, error) {
69+
if len(r.rules) == 0 {
70+
return issues, nil
71+
}
72+
// TODO: Concurrency?
73+
return filterIssues(issues, func(i *result.Issue) bool {
74+
for _, rule := range r.rules {
75+
if rule.Match(i) {
76+
return false
77+
}
78+
}
79+
return true
80+
}), nil
81+
}
82+
83+
func (ExcludeRules) Name() string { return "exclude-rules" }
84+
func (ExcludeRules) Finish() {}
85+
86+
var _ Processor = ExcludeRules{}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package processors
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
"github.com/golangci/golangci-lint/pkg/result"
9+
)
10+
11+
func TestExcludeRules(t *testing.T) {
12+
p := NewExcludeRules([]ExcludeRule{
13+
{
14+
Text: "^exclude$",
15+
},
16+
})
17+
texts := []string{"excLude", "1", "", "exclud", "notexclude"}
18+
var issues []result.Issue
19+
for _, t := range texts {
20+
issues = append(issues, newTextIssue(t))
21+
}
22+
23+
processedIssues := process(t, p, issues...)
24+
assert.Len(t, processedIssues, len(issues)-1)
25+
26+
var processedTexts []string
27+
for _, i := range processedIssues {
28+
processedTexts = append(processedTexts, i.Text)
29+
}
30+
assert.Equal(t, texts[1:], processedTexts)
31+
t.Run("Empty", func(t *testing.T) {
32+
processAssertSame(t, NewExcludeRules(nil), newTextIssue("test"))
33+
})
34+
}

0 commit comments

Comments
 (0)