add support for exclude rules
This commit is contained in:
parent
b607ea387e
commit
a3a04552bb
@ -227,9 +227,16 @@ type Linters struct {
|
|||||||
Presets []string
|
Presets []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ExcludeRule struct {
|
||||||
|
Linters []string
|
||||||
|
Path string
|
||||||
|
Text string
|
||||||
|
}
|
||||||
|
|
||||||
type Issues struct {
|
type Issues struct {
|
||||||
ExcludePatterns []string `mapstructure:"exclude"`
|
ExcludePatterns []string `mapstructure:"exclude"`
|
||||||
UseDefaultExcludes bool `mapstructure:"exclude-use-default"`
|
ExcludeRules []ExcludeRule `mapstructure:"exclude-rules"`
|
||||||
|
UseDefaultExcludes bool `mapstructure:"exclude-use-default"`
|
||||||
|
|
||||||
MaxIssuesPerLinter int `mapstructure:"max-issues-per-linter"`
|
MaxIssuesPerLinter int `mapstructure:"max-issues-per-linter"`
|
||||||
MaxSameIssues int `mapstructure:"max-same-issues"`
|
MaxSameIssues int `mapstructure:"max-same-issues"`
|
||||||
|
@ -49,6 +49,15 @@ func NewRunner(astCache *astcache.Cache, cfg *config.Config, log logutils.Log, g
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var excludeRules []processors.ExcludeRule
|
||||||
|
for _, r := range icfg.ExcludeRules {
|
||||||
|
excludeRules = append(excludeRules, processors.ExcludeRule{
|
||||||
|
Text: r.Text,
|
||||||
|
Path: r.Path,
|
||||||
|
Linters: r.Linters,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return &Runner{
|
return &Runner{
|
||||||
Processors: []processors.Processor{
|
Processors: []processors.Processor{
|
||||||
processors.NewPathPrettifier(), // must be before diff, nolint and exclude autogenerated processor at least
|
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
|
|||||||
|
|
||||||
processors.NewAutogeneratedExclude(astCache),
|
processors.NewAutogeneratedExclude(astCache),
|
||||||
processors.NewExclude(excludeTotalPattern),
|
processors.NewExclude(excludeTotalPattern),
|
||||||
|
processors.NewExcludeRules(excludeRules),
|
||||||
processors.NewNolint(astCache, log.Child("nolint")),
|
processors.NewNolint(astCache, log.Child("nolint")),
|
||||||
|
|
||||||
processors.NewUniqByLine(),
|
processors.NewUniqByLine(),
|
||||||
|
86
pkg/result/processors/exclude_rules.go
Normal file
86
pkg/result/processors/exclude_rules.go
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
package processors
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
|
||||||
|
"github.com/golangci/golangci-lint/pkg/result"
|
||||||
|
)
|
||||||
|
|
||||||
|
type excludeRule struct {
|
||||||
|
text *regexp.Regexp
|
||||||
|
path *regexp.Regexp
|
||||||
|
linters []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *excludeRule) isEmpty() bool {
|
||||||
|
return r.text == nil && r.path == nil && len(r.linters) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r excludeRule) Match(i *result.Issue) bool {
|
||||||
|
if r.isEmpty() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if r.text != nil && !r.text.MatchString(i.Text) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if r.path != nil && !r.path.MatchString(i.FilePath()) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if len(r.linters) == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
for _, l := range r.linters {
|
||||||
|
if l == i.FromLinter {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
type ExcludeRule struct {
|
||||||
|
Text string
|
||||||
|
Path string
|
||||||
|
Linters []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewExcludeRules(rules []ExcludeRule) *ExcludeRules {
|
||||||
|
r := new(ExcludeRules)
|
||||||
|
for _, rule := range rules {
|
||||||
|
parsedRule := excludeRule{
|
||||||
|
linters: rule.Linters,
|
||||||
|
}
|
||||||
|
if rule.Text != "" {
|
||||||
|
parsedRule.text = regexp.MustCompile("(?i)" + rule.Text)
|
||||||
|
}
|
||||||
|
if rule.Path != "" {
|
||||||
|
parsedRule.path = regexp.MustCompile(rule.Path)
|
||||||
|
}
|
||||||
|
// TODO: Forbid text-only, linter-only or path-only exclude rule.
|
||||||
|
r.rules = append(r.rules, parsedRule)
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
type ExcludeRules struct {
|
||||||
|
rules []excludeRule
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r ExcludeRules) Process(issues []result.Issue) ([]result.Issue, error) {
|
||||||
|
if len(r.rules) == 0 {
|
||||||
|
return issues, nil
|
||||||
|
}
|
||||||
|
// TODO: Concurrency?
|
||||||
|
return filterIssues(issues, func(i *result.Issue) bool {
|
||||||
|
for _, rule := range r.rules {
|
||||||
|
if rule.Match(i) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ExcludeRules) Name() string { return "exclude-rules" }
|
||||||
|
func (ExcludeRules) Finish() {}
|
||||||
|
|
||||||
|
var _ Processor = ExcludeRules{}
|
34
pkg/result/processors/exclude_rules_test.go
Normal file
34
pkg/result/processors/exclude_rules_test.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package processors
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
"github.com/golangci/golangci-lint/pkg/result"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestExcludeRules(t *testing.T) {
|
||||||
|
p := NewExcludeRules([]ExcludeRule{
|
||||||
|
{
|
||||||
|
Text: "^exclude$",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
texts := []string{"excLude", "1", "", "exclud", "notexclude"}
|
||||||
|
var issues []result.Issue
|
||||||
|
for _, t := range texts {
|
||||||
|
issues = append(issues, newTextIssue(t))
|
||||||
|
}
|
||||||
|
|
||||||
|
processedIssues := process(t, p, issues...)
|
||||||
|
assert.Len(t, processedIssues, len(issues)-1)
|
||||||
|
|
||||||
|
var processedTexts []string
|
||||||
|
for _, i := range processedIssues {
|
||||||
|
processedTexts = append(processedTexts, i.Text)
|
||||||
|
}
|
||||||
|
assert.Equal(t, texts[1:], processedTexts)
|
||||||
|
t.Run("Empty", func(t *testing.T) {
|
||||||
|
processAssertSame(t, NewExcludeRules(nil), newTextIssue("test"))
|
||||||
|
})
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user