dev: removes BaseRule, ExcludeRule, SeverityRule duplications (#4676)
This commit is contained in:
parent
b2df2f4877
commit
55b2f5ddeb
@ -80,8 +80,8 @@ func NewRunner(log logutils.Log, cfg *config.Config, args []string, goenv *gouti
|
|||||||
// Must be before exclude because users see already marked output and configure excluding by it.
|
// Must be before exclude because users see already marked output and configure excluding by it.
|
||||||
processors.NewIdentifierMarker(),
|
processors.NewIdentifierMarker(),
|
||||||
|
|
||||||
getExcludeProcessor(&cfg.Issues),
|
processors.NewExclude(&cfg.Issues),
|
||||||
getExcludeRulesProcessor(&cfg.Issues, log, files),
|
processors.NewExcludeRules(log.Child(logutils.DebugKeyExcludeRules), files, &cfg.Issues),
|
||||||
processors.NewNolint(log.Child(logutils.DebugKeyNolint), dbManager, enabledLinters),
|
processors.NewNolint(log.Child(logutils.DebugKeyNolint), dbManager, enabledLinters),
|
||||||
|
|
||||||
processors.NewUniqByLine(cfg),
|
processors.NewUniqByLine(cfg),
|
||||||
@ -91,7 +91,7 @@ func NewRunner(log logutils.Log, cfg *config.Config, args []string, goenv *gouti
|
|||||||
processors.NewMaxFromLinter(cfg.Issues.MaxIssuesPerLinter, log.Child(logutils.DebugKeyMaxFromLinter), cfg),
|
processors.NewMaxFromLinter(cfg.Issues.MaxIssuesPerLinter, log.Child(logutils.DebugKeyMaxFromLinter), cfg),
|
||||||
processors.NewSourceCode(lineCache, log.Child(logutils.DebugKeySourceCode)),
|
processors.NewSourceCode(lineCache, log.Child(logutils.DebugKeySourceCode)),
|
||||||
processors.NewPathShortener(),
|
processors.NewPathShortener(),
|
||||||
getSeverityRulesProcessor(&cfg.Severity, log, files),
|
processors.NewSeverity(log.Child(logutils.DebugKeySeverityRules), files, &cfg.Severity),
|
||||||
|
|
||||||
// The fixer still needs to see paths for the issues that are relative to the current directory.
|
// The fixer still needs to see paths for the issues that are relative to the current directory.
|
||||||
processors.NewFixer(cfg, log, fileCache),
|
processors.NewFixer(cfg, log, fileCache),
|
||||||
@ -242,72 +242,3 @@ func (r *Runner) processIssues(issues []result.Issue, sw *timeutils.Stopwatch, s
|
|||||||
|
|
||||||
return issues
|
return issues
|
||||||
}
|
}
|
||||||
|
|
||||||
func getExcludeProcessor(cfg *config.Issues) processors.Processor {
|
|
||||||
opts := processors.ExcludeOptions{
|
|
||||||
CaseSensitive: cfg.ExcludeCaseSensitive,
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(cfg.ExcludePatterns) != 0 {
|
|
||||||
opts.Pattern = fmt.Sprintf("(%s)", strings.Join(cfg.ExcludePatterns, "|"))
|
|
||||||
}
|
|
||||||
|
|
||||||
return processors.NewExclude(opts)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getExcludeRulesProcessor(cfg *config.Issues, log logutils.Log, files *fsutils.Files) processors.Processor {
|
|
||||||
var excludeRules []processors.ExcludeRule
|
|
||||||
for _, r := range cfg.ExcludeRules {
|
|
||||||
excludeRules = append(excludeRules, processors.ExcludeRule{
|
|
||||||
BaseRule: processors.BaseRule{
|
|
||||||
Text: r.Text,
|
|
||||||
Source: r.Source,
|
|
||||||
Path: r.Path,
|
|
||||||
PathExcept: r.PathExcept,
|
|
||||||
Linters: r.Linters,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if cfg.UseDefaultExcludes {
|
|
||||||
for _, r := range config.GetExcludePatterns(cfg.IncludeDefaultExcludes) {
|
|
||||||
excludeRules = append(excludeRules, processors.ExcludeRule{
|
|
||||||
BaseRule: processors.BaseRule{
|
|
||||||
Text: r.Pattern,
|
|
||||||
Linters: []string{r.Linter},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
opts := processors.ExcludeRulesOptions{
|
|
||||||
Rules: excludeRules,
|
|
||||||
CaseSensitive: cfg.ExcludeCaseSensitive,
|
|
||||||
}
|
|
||||||
|
|
||||||
return processors.NewExcludeRules(log.Child(logutils.DebugKeyExcludeRules), files, opts)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getSeverityRulesProcessor(cfg *config.Severity, log logutils.Log, files *fsutils.Files) processors.Processor {
|
|
||||||
var severityRules []processors.SeverityRule
|
|
||||||
for _, r := range cfg.Rules {
|
|
||||||
severityRules = append(severityRules, processors.SeverityRule{
|
|
||||||
Severity: r.Severity,
|
|
||||||
BaseRule: processors.BaseRule{
|
|
||||||
Text: r.Text,
|
|
||||||
Source: r.Source,
|
|
||||||
Path: r.Path,
|
|
||||||
PathExcept: r.PathExcept,
|
|
||||||
Linters: r.Linters,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
severityOpts := processors.SeverityOptions{
|
|
||||||
Default: cfg.Default,
|
|
||||||
Rules: severityRules,
|
|
||||||
CaseSensitive: cfg.CaseSensitive,
|
|
||||||
}
|
|
||||||
|
|
||||||
return processors.NewSeverity(log.Child(logutils.DebugKeySeverityRules), files, severityOpts)
|
|
||||||
}
|
|
||||||
|
@ -10,14 +10,6 @@ import (
|
|||||||
|
|
||||||
const caseInsensitivePrefix = "(?i)"
|
const caseInsensitivePrefix = "(?i)"
|
||||||
|
|
||||||
type BaseRule struct {
|
|
||||||
Text string
|
|
||||||
Source string
|
|
||||||
Path string
|
|
||||||
PathExcept string
|
|
||||||
Linters []string
|
|
||||||
}
|
|
||||||
|
|
||||||
type baseRule struct {
|
type baseRule struct {
|
||||||
text *regexp.Regexp
|
text *regexp.Regexp
|
||||||
source *regexp.Regexp
|
source *regexp.Regexp
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
package processors
|
package processors
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/golangci/golangci-lint/pkg/config"
|
||||||
"github.com/golangci/golangci-lint/pkg/result"
|
"github.com/golangci/golangci-lint/pkg/result"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -14,22 +17,22 @@ type Exclude struct {
|
|||||||
pattern *regexp.Regexp
|
pattern *regexp.Regexp
|
||||||
}
|
}
|
||||||
|
|
||||||
type ExcludeOptions struct {
|
func NewExclude(cfg *config.Issues) *Exclude {
|
||||||
Pattern string
|
|
||||||
CaseSensitive bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewExclude(opts ExcludeOptions) *Exclude {
|
|
||||||
p := &Exclude{name: "exclude"}
|
p := &Exclude{name: "exclude"}
|
||||||
|
|
||||||
|
var pattern string
|
||||||
|
if len(cfg.ExcludePatterns) != 0 {
|
||||||
|
pattern = fmt.Sprintf("(%s)", strings.Join(cfg.ExcludePatterns, "|"))
|
||||||
|
}
|
||||||
|
|
||||||
prefix := caseInsensitivePrefix
|
prefix := caseInsensitivePrefix
|
||||||
if opts.CaseSensitive {
|
if cfg.ExcludeCaseSensitive {
|
||||||
p.name = "exclude-case-sensitive"
|
p.name = "exclude-case-sensitive"
|
||||||
prefix = ""
|
prefix = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.Pattern != "" {
|
if pattern != "" {
|
||||||
p.pattern = regexp.MustCompile(prefix + opts.Pattern)
|
p.pattern = regexp.MustCompile(prefix + pattern)
|
||||||
}
|
}
|
||||||
|
|
||||||
return p
|
return p
|
||||||
|
@ -3,6 +3,7 @@ package processors
|
|||||||
import (
|
import (
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
|
"github.com/golangci/golangci-lint/pkg/config"
|
||||||
"github.com/golangci/golangci-lint/pkg/fsutils"
|
"github.com/golangci/golangci-lint/pkg/fsutils"
|
||||||
"github.com/golangci/golangci-lint/pkg/logutils"
|
"github.com/golangci/golangci-lint/pkg/logutils"
|
||||||
"github.com/golangci/golangci-lint/pkg/result"
|
"github.com/golangci/golangci-lint/pkg/result"
|
||||||
@ -14,10 +15,6 @@ type excludeRule struct {
|
|||||||
baseRule
|
baseRule
|
||||||
}
|
}
|
||||||
|
|
||||||
type ExcludeRule struct {
|
|
||||||
BaseRule
|
|
||||||
}
|
|
||||||
|
|
||||||
type ExcludeRules struct {
|
type ExcludeRules struct {
|
||||||
name string
|
name string
|
||||||
|
|
||||||
@ -27,12 +24,7 @@ type ExcludeRules struct {
|
|||||||
rules []excludeRule
|
rules []excludeRule
|
||||||
}
|
}
|
||||||
|
|
||||||
type ExcludeRulesOptions struct {
|
func NewExcludeRules(log logutils.Log, files *fsutils.Files, cfg *config.Issues) *ExcludeRules {
|
||||||
Rules []ExcludeRule
|
|
||||||
CaseSensitive bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewExcludeRules(log logutils.Log, files *fsutils.Files, opts ExcludeRulesOptions) *ExcludeRules {
|
|
||||||
p := &ExcludeRules{
|
p := &ExcludeRules{
|
||||||
name: "exclude-rules",
|
name: "exclude-rules",
|
||||||
files: files,
|
files: files,
|
||||||
@ -40,12 +32,25 @@ func NewExcludeRules(log logutils.Log, files *fsutils.Files, opts ExcludeRulesOp
|
|||||||
}
|
}
|
||||||
|
|
||||||
prefix := caseInsensitivePrefix
|
prefix := caseInsensitivePrefix
|
||||||
if opts.CaseSensitive {
|
if cfg.ExcludeCaseSensitive {
|
||||||
prefix = ""
|
prefix = ""
|
||||||
p.name = "exclude-rules-case-sensitive"
|
p.name = "exclude-rules-case-sensitive"
|
||||||
}
|
}
|
||||||
|
|
||||||
p.rules = createRules(opts.Rules, prefix)
|
excludeRules := cfg.ExcludeRules
|
||||||
|
|
||||||
|
if cfg.UseDefaultExcludes {
|
||||||
|
for _, r := range config.GetExcludePatterns(cfg.IncludeDefaultExcludes) {
|
||||||
|
excludeRules = append(excludeRules, config.ExcludeRule{
|
||||||
|
BaseRule: config.BaseRule{
|
||||||
|
Text: r.Pattern,
|
||||||
|
Linters: []string{r.Linter},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
p.rules = createRules(excludeRules, prefix)
|
||||||
|
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
@ -71,7 +76,7 @@ func (p ExcludeRules) Process(issues []result.Issue) ([]result.Issue, error) {
|
|||||||
|
|
||||||
func (ExcludeRules) Finish() {}
|
func (ExcludeRules) Finish() {}
|
||||||
|
|
||||||
func createRules(rules []ExcludeRule, prefix string) []excludeRule {
|
func createRules(rules []config.ExcludeRule, prefix string) []excludeRule {
|
||||||
parsedRules := make([]excludeRule, 0, len(rules))
|
parsedRules := make([]excludeRule, 0, len(rules))
|
||||||
|
|
||||||
for _, rule := range rules {
|
for _, rule := range rules {
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
"github.com/golangci/golangci-lint/pkg/config"
|
||||||
"github.com/golangci/golangci-lint/pkg/fsutils"
|
"github.com/golangci/golangci-lint/pkg/fsutils"
|
||||||
"github.com/golangci/golangci-lint/pkg/result"
|
"github.com/golangci/golangci-lint/pkg/result"
|
||||||
)
|
)
|
||||||
@ -15,33 +16,33 @@ func TestExcludeRules_multiple(t *testing.T) {
|
|||||||
lineCache := fsutils.NewLineCache(fsutils.NewFileCache())
|
lineCache := fsutils.NewLineCache(fsutils.NewFileCache())
|
||||||
files := fsutils.NewFiles(lineCache, "")
|
files := fsutils.NewFiles(lineCache, "")
|
||||||
|
|
||||||
opts := ExcludeRulesOptions{Rules: []ExcludeRule{
|
opts := &config.Issues{ExcludeRules: []config.ExcludeRule{
|
||||||
{
|
{
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Text: "^exclude$",
|
Text: "^exclude$",
|
||||||
Linters: []string{"linter"},
|
Linters: []string{"linter"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Linters: []string{"testlinter"},
|
Linters: []string{"testlinter"},
|
||||||
Path: `_test\.go`,
|
Path: `_test\.go`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Text: "^testonly$",
|
Text: "^testonly$",
|
||||||
Path: `_test\.go`,
|
Path: `_test\.go`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Text: "^nontestonly$",
|
Text: "^nontestonly$",
|
||||||
PathExcept: `_test\.go`,
|
PathExcept: `_test\.go`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Source: "^//go:generate ",
|
Source: "^//go:generate ",
|
||||||
Linters: []string{"lll"},
|
Linters: []string{"lll"},
|
||||||
},
|
},
|
||||||
@ -94,10 +95,10 @@ func TestExcludeRules_pathPrefix(t *testing.T) {
|
|||||||
pathPrefix := path.Join("some", "dir")
|
pathPrefix := path.Join("some", "dir")
|
||||||
files := fsutils.NewFiles(lineCache, pathPrefix)
|
files := fsutils.NewFiles(lineCache, pathPrefix)
|
||||||
|
|
||||||
opts := ExcludeRulesOptions{
|
opts := &config.Issues{
|
||||||
Rules: []ExcludeRule{
|
ExcludeRules: []config.ExcludeRule{
|
||||||
{
|
{
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Path: `some/dir/e\.go`,
|
Path: `some/dir/e\.go`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -136,10 +137,10 @@ func TestExcludeRules_pathPrefix(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestExcludeRules_text(t *testing.T) {
|
func TestExcludeRules_text(t *testing.T) {
|
||||||
opts := ExcludeRulesOptions{
|
opts := &config.Issues{
|
||||||
Rules: []ExcludeRule{
|
ExcludeRules: []config.ExcludeRule{
|
||||||
{
|
{
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Text: "^exclude$",
|
Text: "^exclude$",
|
||||||
Linters: []string{"linter"},
|
Linters: []string{"linter"},
|
||||||
},
|
},
|
||||||
@ -170,36 +171,36 @@ func TestExcludeRules_text(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestExcludeRules_empty(t *testing.T) {
|
func TestExcludeRules_empty(t *testing.T) {
|
||||||
processAssertSame(t, NewExcludeRules(nil, nil, ExcludeRulesOptions{}), newIssueFromTextTestCase("test"))
|
processAssertSame(t, NewExcludeRules(nil, nil, &config.Issues{}), newIssueFromTextTestCase("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExcludeRules_caseSensitive_multiple(t *testing.T) {
|
func TestExcludeRules_caseSensitive_multiple(t *testing.T) {
|
||||||
lineCache := fsutils.NewLineCache(fsutils.NewFileCache())
|
lineCache := fsutils.NewLineCache(fsutils.NewFileCache())
|
||||||
files := fsutils.NewFiles(lineCache, "")
|
files := fsutils.NewFiles(lineCache, "")
|
||||||
|
|
||||||
opts := ExcludeRulesOptions{
|
opts := &config.Issues{
|
||||||
CaseSensitive: true,
|
ExcludeCaseSensitive: true,
|
||||||
Rules: []ExcludeRule{
|
ExcludeRules: []config.ExcludeRule{
|
||||||
{
|
{
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Text: "^exclude$",
|
Text: "^exclude$",
|
||||||
Linters: []string{"linter"},
|
Linters: []string{"linter"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Linters: []string{"testlinter"},
|
Linters: []string{"testlinter"},
|
||||||
Path: `_test\.go`,
|
Path: `_test\.go`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Text: "^testonly$",
|
Text: "^testonly$",
|
||||||
Path: `_test\.go`,
|
Path: `_test\.go`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Source: "^//go:generate ",
|
Source: "^//go:generate ",
|
||||||
Linters: []string{"lll"},
|
Linters: []string{"lll"},
|
||||||
},
|
},
|
||||||
@ -251,11 +252,11 @@ func TestExcludeRules_caseSensitive_multiple(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestExcludeRules_caseSensitive_text(t *testing.T) {
|
func TestExcludeRules_caseSensitive_text(t *testing.T) {
|
||||||
opts := ExcludeRulesOptions{
|
opts := &config.Issues{
|
||||||
CaseSensitive: true,
|
ExcludeCaseSensitive: true,
|
||||||
Rules: []ExcludeRule{
|
ExcludeRules: []config.ExcludeRule{
|
||||||
{
|
{
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Text: "^exclude$",
|
Text: "^exclude$",
|
||||||
Linters: []string{"linter"},
|
Linters: []string{"linter"},
|
||||||
},
|
},
|
||||||
@ -287,5 +288,5 @@ func TestExcludeRules_caseSensitive_text(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestExcludeRules_caseSensitive_empty(t *testing.T) {
|
func TestExcludeRules_caseSensitive_empty(t *testing.T) {
|
||||||
processAssertSame(t, NewExcludeRules(nil, nil, ExcludeRulesOptions{CaseSensitive: true}), newIssueFromTextTestCase("test"))
|
processAssertSame(t, NewExcludeRules(nil, nil, &config.Issues{ExcludeCaseSensitive: true}), newIssueFromTextTestCase("test"))
|
||||||
}
|
}
|
||||||
|
@ -5,11 +5,12 @@ import (
|
|||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
"github.com/golangci/golangci-lint/pkg/config"
|
||||||
"github.com/golangci/golangci-lint/pkg/result"
|
"github.com/golangci/golangci-lint/pkg/result"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestExclude(t *testing.T) {
|
func TestExclude(t *testing.T) {
|
||||||
p := NewExclude(ExcludeOptions{Pattern: "^exclude$"})
|
p := NewExclude(&config.Issues{ExcludePatterns: []string{"^exclude$"}})
|
||||||
|
|
||||||
texts := []string{"excLude", "1", "", "exclud", "notexclude"}
|
texts := []string{"excLude", "1", "", "exclud", "notexclude"}
|
||||||
|
|
||||||
@ -30,11 +31,11 @@ func TestExclude(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestExclude_empty(t *testing.T) {
|
func TestExclude_empty(t *testing.T) {
|
||||||
processAssertSame(t, NewExclude(ExcludeOptions{}), newIssueFromTextTestCase("test"))
|
processAssertSame(t, NewExclude(&config.Issues{}), newIssueFromTextTestCase("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExclude_caseSensitive(t *testing.T) {
|
func TestExclude_caseSensitive(t *testing.T) {
|
||||||
p := NewExclude(ExcludeOptions{Pattern: "^exclude$", CaseSensitive: true})
|
p := NewExclude(&config.Issues{ExcludePatterns: []string{"^exclude$"}, ExcludeCaseSensitive: true})
|
||||||
|
|
||||||
texts := []string{"excLude", "1", "", "exclud", "exclude"}
|
texts := []string{"excLude", "1", "", "exclud", "exclude"}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ package processors
|
|||||||
import (
|
import (
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
|
"github.com/golangci/golangci-lint/pkg/config"
|
||||||
"github.com/golangci/golangci-lint/pkg/fsutils"
|
"github.com/golangci/golangci-lint/pkg/fsutils"
|
||||||
"github.com/golangci/golangci-lint/pkg/logutils"
|
"github.com/golangci/golangci-lint/pkg/logutils"
|
||||||
"github.com/golangci/golangci-lint/pkg/result"
|
"github.com/golangci/golangci-lint/pkg/result"
|
||||||
@ -17,17 +18,6 @@ type severityRule struct {
|
|||||||
severity string
|
severity string
|
||||||
}
|
}
|
||||||
|
|
||||||
type SeverityRule struct {
|
|
||||||
BaseRule
|
|
||||||
Severity string
|
|
||||||
}
|
|
||||||
|
|
||||||
type SeverityOptions struct {
|
|
||||||
Default string
|
|
||||||
Rules []SeverityRule
|
|
||||||
CaseSensitive bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type Severity struct {
|
type Severity struct {
|
||||||
name string
|
name string
|
||||||
|
|
||||||
@ -39,21 +29,21 @@ type Severity struct {
|
|||||||
rules []severityRule
|
rules []severityRule
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSeverity(log logutils.Log, files *fsutils.Files, opts SeverityOptions) *Severity {
|
func NewSeverity(log logutils.Log, files *fsutils.Files, cfg *config.Severity) *Severity {
|
||||||
p := &Severity{
|
p := &Severity{
|
||||||
name: "severity-rules",
|
name: "severity-rules",
|
||||||
files: files,
|
files: files,
|
||||||
log: log,
|
log: log,
|
||||||
defaultSeverity: opts.Default,
|
defaultSeverity: cfg.Default,
|
||||||
}
|
}
|
||||||
|
|
||||||
prefix := caseInsensitivePrefix
|
prefix := caseInsensitivePrefix
|
||||||
if opts.CaseSensitive {
|
if cfg.CaseSensitive {
|
||||||
prefix = ""
|
prefix = ""
|
||||||
p.name = "severity-rules-case-sensitive"
|
p.name = "severity-rules-case-sensitive"
|
||||||
}
|
}
|
||||||
|
|
||||||
p.rules = createSeverityRules(opts.Rules, prefix)
|
p.rules = createSeverityRules(cfg.Rules, prefix)
|
||||||
|
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
@ -93,7 +83,7 @@ func (p *Severity) transform(issue *result.Issue) *result.Issue {
|
|||||||
return issue
|
return issue
|
||||||
}
|
}
|
||||||
|
|
||||||
func createSeverityRules(rules []SeverityRule, prefix string) []severityRule {
|
func createSeverityRules(rules []config.SeverityRule, prefix string) []severityRule {
|
||||||
parsedRules := make([]severityRule, 0, len(rules))
|
parsedRules := make([]severityRule, 0, len(rules))
|
||||||
|
|
||||||
for _, rule := range rules {
|
for _, rule := range rules {
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
"github.com/golangci/golangci-lint/pkg/config"
|
||||||
"github.com/golangci/golangci-lint/pkg/fsutils"
|
"github.com/golangci/golangci-lint/pkg/fsutils"
|
||||||
"github.com/golangci/golangci-lint/pkg/logutils"
|
"github.com/golangci/golangci-lint/pkg/logutils"
|
||||||
"github.com/golangci/golangci-lint/pkg/result"
|
"github.com/golangci/golangci-lint/pkg/result"
|
||||||
@ -17,58 +18,58 @@ func TestSeverity_multiple(t *testing.T) {
|
|||||||
files := fsutils.NewFiles(lineCache, "")
|
files := fsutils.NewFiles(lineCache, "")
|
||||||
log := logutils.NewStderrLog(logutils.DebugKeyEmpty)
|
log := logutils.NewStderrLog(logutils.DebugKeyEmpty)
|
||||||
|
|
||||||
opts := SeverityOptions{
|
opts := &config.Severity{
|
||||||
Default: "error",
|
Default: "error",
|
||||||
Rules: []SeverityRule{
|
Rules: []config.SeverityRule{
|
||||||
{
|
{
|
||||||
Severity: "info",
|
Severity: "info",
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Text: "^ssl$",
|
Text: "^ssl$",
|
||||||
Linters: []string{"gosec"},
|
Linters: []string{"gosec"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Severity: "info",
|
Severity: "info",
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Linters: []string{"linter"},
|
Linters: []string{"linter"},
|
||||||
Path: "e.go",
|
Path: "e.go",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Severity: "info",
|
Severity: "info",
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Text: "^testonly$",
|
Text: "^testonly$",
|
||||||
Path: `_test\.go`,
|
Path: `_test\.go`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Severity: "info",
|
Severity: "info",
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Text: "^nontestonly$",
|
Text: "^nontestonly$",
|
||||||
PathExcept: `_test\.go`,
|
PathExcept: `_test\.go`,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Source: "^//go:generate ",
|
Source: "^//go:generate ",
|
||||||
Linters: []string{"lll"},
|
Linters: []string{"lll"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Severity: "info",
|
Severity: "info",
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Source: "^//go:dosomething",
|
Source: "^//go:dosomething",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Severity: "info",
|
Severity: "info",
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Linters: []string{"someotherlinter"},
|
Linters: []string{"someotherlinter"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Severity: "info",
|
Severity: "info",
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Linters: []string{"somelinter"},
|
Linters: []string{"somelinter"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -133,12 +134,12 @@ func TestSeverity_pathPrefix(t *testing.T) {
|
|||||||
files := fsutils.NewFiles(lineCache, pathPrefix)
|
files := fsutils.NewFiles(lineCache, pathPrefix)
|
||||||
log := logutils.NewStderrLog(logutils.DebugKeyEmpty)
|
log := logutils.NewStderrLog(logutils.DebugKeyEmpty)
|
||||||
|
|
||||||
opts := SeverityOptions{
|
opts := &config.Severity{
|
||||||
Default: "error",
|
Default: "error",
|
||||||
Rules: []SeverityRule{
|
Rules: []config.SeverityRule{
|
||||||
{
|
{
|
||||||
Severity: "info",
|
Severity: "info",
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Text: "some",
|
Text: "some",
|
||||||
Path: `some/dir/e\.go`,
|
Path: `some/dir/e\.go`,
|
||||||
},
|
},
|
||||||
@ -180,10 +181,10 @@ func TestSeverity_pathPrefix(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSeverity_text(t *testing.T) {
|
func TestSeverity_text(t *testing.T) {
|
||||||
opts := SeverityOptions{
|
opts := &config.Severity{
|
||||||
Rules: []SeverityRule{
|
Rules: []config.SeverityRule{
|
||||||
{
|
{
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Text: "^severity$",
|
Text: "^severity$",
|
||||||
Linters: []string{"linter"},
|
Linters: []string{"linter"},
|
||||||
},
|
},
|
||||||
@ -218,12 +219,12 @@ func TestSeverity_onlyDefault(t *testing.T) {
|
|||||||
files := fsutils.NewFiles(lineCache, "")
|
files := fsutils.NewFiles(lineCache, "")
|
||||||
log := logutils.NewStderrLog(logutils.DebugKeyEmpty)
|
log := logutils.NewStderrLog(logutils.DebugKeyEmpty)
|
||||||
|
|
||||||
opts := SeverityOptions{
|
opts := config.Severity{
|
||||||
Default: "info",
|
Default: "info",
|
||||||
Rules: []SeverityRule{},
|
Rules: []config.SeverityRule{},
|
||||||
}
|
}
|
||||||
|
|
||||||
p := NewSeverity(log, files, opts)
|
p := NewSeverity(log, files, &opts)
|
||||||
|
|
||||||
cases := []issueTestCase{
|
cases := []issueTestCase{
|
||||||
{Path: "ssl.go", Text: "ssl", Linter: "gosec"},
|
{Path: "ssl.go", Text: "ssl", Linter: "gosec"},
|
||||||
@ -257,7 +258,7 @@ func TestSeverity_onlyDefault(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSeverity_empty(t *testing.T) {
|
func TestSeverity_empty(t *testing.T) {
|
||||||
p := NewSeverity(nil, nil, SeverityOptions{})
|
p := NewSeverity(nil, nil, &config.Severity{})
|
||||||
|
|
||||||
processAssertSame(t, p, newIssueFromTextTestCase("test"))
|
processAssertSame(t, p, newIssueFromTextTestCase("test"))
|
||||||
}
|
}
|
||||||
@ -266,12 +267,12 @@ func TestSeverity_caseSensitive(t *testing.T) {
|
|||||||
lineCache := fsutils.NewLineCache(fsutils.NewFileCache())
|
lineCache := fsutils.NewLineCache(fsutils.NewFileCache())
|
||||||
files := fsutils.NewFiles(lineCache, "")
|
files := fsutils.NewFiles(lineCache, "")
|
||||||
|
|
||||||
opts := SeverityOptions{
|
opts := &config.Severity{
|
||||||
Default: "error",
|
Default: "error",
|
||||||
Rules: []SeverityRule{
|
Rules: []config.SeverityRule{
|
||||||
{
|
{
|
||||||
Severity: "info",
|
Severity: "info",
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Text: "^ssl$",
|
Text: "^ssl$",
|
||||||
Linters: []string{"gosec", "someotherlinter"},
|
Linters: []string{"gosec", "someotherlinter"},
|
||||||
},
|
},
|
||||||
@ -317,18 +318,18 @@ func TestSeverity_transform(t *testing.T) {
|
|||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
desc string
|
desc string
|
||||||
opts SeverityOptions
|
opts *config.Severity
|
||||||
issue *result.Issue
|
issue *result.Issue
|
||||||
expected *result.Issue
|
expected *result.Issue
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
desc: "apply severity from rule",
|
desc: "apply severity from rule",
|
||||||
opts: SeverityOptions{
|
opts: &config.Severity{
|
||||||
Default: "error",
|
Default: "error",
|
||||||
Rules: []SeverityRule{
|
Rules: []config.SeverityRule{
|
||||||
{
|
{
|
||||||
Severity: "info",
|
Severity: "info",
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Linters: []string{"linter1"},
|
Linters: []string{"linter1"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -346,12 +347,12 @@ func TestSeverity_transform(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "apply severity from default",
|
desc: "apply severity from default",
|
||||||
opts: SeverityOptions{
|
opts: &config.Severity{
|
||||||
Default: "error",
|
Default: "error",
|
||||||
Rules: []SeverityRule{
|
Rules: []config.SeverityRule{
|
||||||
{
|
{
|
||||||
Severity: "info",
|
Severity: "info",
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Linters: []string{"linter1"},
|
Linters: []string{"linter1"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -369,12 +370,12 @@ func TestSeverity_transform(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "severity from rule override severity from linter",
|
desc: "severity from rule override severity from linter",
|
||||||
opts: SeverityOptions{
|
opts: &config.Severity{
|
||||||
Default: "error",
|
Default: "error",
|
||||||
Rules: []SeverityRule{
|
Rules: []config.SeverityRule{
|
||||||
{
|
{
|
||||||
Severity: "info",
|
Severity: "info",
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Linters: []string{"linter1"},
|
Linters: []string{"linter1"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -393,12 +394,12 @@ func TestSeverity_transform(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "severity from default override severity from linter",
|
desc: "severity from default override severity from linter",
|
||||||
opts: SeverityOptions{
|
opts: &config.Severity{
|
||||||
Default: "error",
|
Default: "error",
|
||||||
Rules: []SeverityRule{
|
Rules: []config.SeverityRule{
|
||||||
{
|
{
|
||||||
Severity: "info",
|
Severity: "info",
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Linters: []string{"linter1"},
|
Linters: []string{"linter1"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -417,12 +418,12 @@ func TestSeverity_transform(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "keep severity from linter as rule",
|
desc: "keep severity from linter as rule",
|
||||||
opts: SeverityOptions{
|
opts: &config.Severity{
|
||||||
Default: "error",
|
Default: "error",
|
||||||
Rules: []SeverityRule{
|
Rules: []config.SeverityRule{
|
||||||
{
|
{
|
||||||
Severity: severityFromLinter,
|
Severity: severityFromLinter,
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Linters: []string{"linter1"},
|
Linters: []string{"linter1"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -441,12 +442,12 @@ func TestSeverity_transform(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "keep severity from linter as default",
|
desc: "keep severity from linter as default",
|
||||||
opts: SeverityOptions{
|
opts: &config.Severity{
|
||||||
Default: severityFromLinter,
|
Default: severityFromLinter,
|
||||||
Rules: []SeverityRule{
|
Rules: []config.SeverityRule{
|
||||||
{
|
{
|
||||||
Severity: "info",
|
Severity: "info",
|
||||||
BaseRule: BaseRule{
|
BaseRule: config.BaseRule{
|
||||||
Linters: []string{"linter1"},
|
Linters: []string{"linter1"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -465,7 +466,7 @@ func TestSeverity_transform(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "keep severity from linter as default (without rule)",
|
desc: "keep severity from linter as default (without rule)",
|
||||||
opts: SeverityOptions{
|
opts: &config.Severity{
|
||||||
Default: severityFromLinter,
|
Default: severityFromLinter,
|
||||||
},
|
},
|
||||||
issue: &result.Issue{
|
issue: &result.Issue{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user