make uniq by line configurable (#920)

This commit is contained in:
to6ka 2020-01-19 16:49:46 +03:00 committed by Isaev Denis
parent bff053ac46
commit d137c2e18c
6 changed files with 28 additions and 1 deletions

View File

@ -61,6 +61,9 @@ output:
# print linter name in the end of issue text, default is true # print linter name in the end of issue text, default is true
print-linter-name: true print-linter-name: true
# make issues output unique by line, default is true
uniq-by-line: true
# all available settings of specific linters # all available settings of specific linters
linters-settings: linters-settings:

View File

@ -511,6 +511,7 @@ Flags:
--out-format string Format of output: colored-line-number|line-number|json|tab|checkstyle|code-climate|junit-xml (default "colored-line-number") --out-format string Format of output: colored-line-number|line-number|json|tab|checkstyle|code-climate|junit-xml (default "colored-line-number")
--print-issued-lines Print lines of code with issue (default true) --print-issued-lines Print lines of code with issue (default true)
--print-linter-name Print linter name in issue line (default true) --print-linter-name Print linter name in issue line (default true)
--uniq-by-line Make issues output unique by line (default true)
--modules-download-mode string Modules download mode. If not empty, passed as -mod=<mode> to go tools --modules-download-mode string Modules download mode. If not empty, passed as -mod=<mode> to go tools
--issues-exit-code int Exit code when issues were found (default 1) --issues-exit-code int Exit code when issues were found (default 1)
--build-tags strings Build tags --build-tags strings Build tags
@ -669,6 +670,9 @@ output:
# print linter name in the end of issue text, default is true # print linter name in the end of issue text, default is true
print-linter-name: true print-linter-name: true
# make issues output unique by line, default is true
uniq-by-line: true
# all available settings of specific linters # all available settings of specific linters
linters-settings: linters-settings:

View File

@ -79,6 +79,7 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is
wh(fmt.Sprintf("Format of output: %s", strings.Join(config.OutFormats, "|")))) wh(fmt.Sprintf("Format of output: %s", strings.Join(config.OutFormats, "|"))))
fs.BoolVar(&oc.PrintIssuedLine, "print-issued-lines", true, wh("Print lines of code with issue")) fs.BoolVar(&oc.PrintIssuedLine, "print-issued-lines", true, wh("Print lines of code with issue"))
fs.BoolVar(&oc.PrintLinterName, "print-linter-name", true, wh("Print linter name in issue line")) fs.BoolVar(&oc.PrintLinterName, "print-linter-name", true, wh("Print linter name in issue line"))
fs.BoolVar(&oc.UniqByLine, "uniq-by-line", true, wh("Make issues output unique by line"))
fs.BoolVar(&oc.PrintWelcomeMessage, "print-welcome", false, wh("Print welcome message")) fs.BoolVar(&oc.PrintWelcomeMessage, "print-welcome", false, wh("Print welcome message"))
hideFlag("print-welcome") // no longer used hideFlag("print-welcome") // no longer used

View File

@ -389,6 +389,7 @@ type Config struct {
Color string Color string
PrintIssuedLine bool `mapstructure:"print-issued-lines"` PrintIssuedLine bool `mapstructure:"print-issued-lines"`
PrintLinterName bool `mapstructure:"print-linter-name"` PrintLinterName bool `mapstructure:"print-linter-name"`
UniqByLine bool `mapstructure:"uniq-by-line"`
PrintWelcomeMessage bool `mapstructure:"print-welcome"` PrintWelcomeMessage bool `mapstructure:"print-welcome"`
} }

View File

@ -27,6 +27,10 @@ func (p UniqByLine) Name() string {
} }
func (p *UniqByLine) Process(issues []result.Issue) ([]result.Issue, error) { func (p *UniqByLine) Process(issues []result.Issue) ([]result.Issue, error) {
if !p.cfg.Output.UniqByLine {
return issues, nil
}
return filterIssues(issues, func(i *result.Issue) bool { return filterIssues(issues, func(i *result.Issue) bool {
if i.Replacement != nil && p.cfg.Issues.NeedFix { if i.Replacement != nil && p.cfg.Issues.NeedFix {
// if issue will be auto-fixed we shouldn't collapse issues: // if issue will be auto-fixed we shouldn't collapse issues:

View File

@ -18,7 +18,10 @@ func newFLIssue(file string, line int) result.Issue {
} }
func TestUniqByLine(t *testing.T) { func TestUniqByLine(t *testing.T) {
p := NewUniqByLine(&config.Config{}) cfg := config.Config{}
cfg.Output.UniqByLine = true
p := NewUniqByLine(&cfg)
i1 := newFLIssue("f1", 1) i1 := newFLIssue("f1", 1)
processAssertSame(t, p, i1) processAssertSame(t, p, i1)
@ -28,3 +31,14 @@ func TestUniqByLine(t *testing.T) {
processAssertSame(t, p, newFLIssue("f1", 2)) // another line processAssertSame(t, p, newFLIssue("f1", 2)) // another line
processAssertSame(t, p, newFLIssue("f2", 1)) // another file processAssertSame(t, p, newFLIssue("f2", 1)) // another file
} }
func TestUniqByLineDisabled(t *testing.T) {
cfg := config.Config{}
cfg.Output.UniqByLine = false
p := NewUniqByLine(&cfg)
i1 := newFLIssue("f1", 1)
processAssertSame(t, p, i1)
processAssertSame(t, p, i1) // check the same issue passed twice
}