golangci-lint/pkg/result/processors/max_per_file_from_linter.go
Ludovic Fernandez 9aea4aee1c
Some checks failed
Extra / Vulnerability scanner (push) Has been cancelled
CI / go-mod (push) Has been cancelled
CI / golangci-lint (push) Has been cancelled
Release a tag / release (push) Has been cancelled
CI / tests-on-windows (push) Has been cancelled
CI / tests-on-macos (push) Has been cancelled
CI / tests-on-unix (1.14) (push) Has been cancelled
CI / tests-on-unix (1.15) (push) Has been cancelled
CI / tests-on-unix (1.16) (push) Has been cancelled
CI / check_generated (push) Has been cancelled
Release a tag / docker-release (map[Dockerfile:build/Dockerfile.alpine]) (push) Has been cancelled
Release a tag / docker-release (map[Dockerfile:build/Dockerfile]) (push) Has been cancelled
typecheck: display compilation errors as report instead of error (#1861)
2021-03-25 23:52:55 +01:00

60 lines
1.5 KiB
Go

package processors
import (
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/result"
)
type linterToCountMap map[string]int
type fileToLinterToCountMap map[string]linterToCountMap
type MaxPerFileFromLinter struct {
flc fileToLinterToCountMap
maxPerFileFromLinterConfig map[string]int
}
var _ Processor = &MaxPerFileFromLinter{}
func NewMaxPerFileFromLinter(cfg *config.Config) *MaxPerFileFromLinter {
maxPerFileFromLinterConfig := map[string]int{}
if !cfg.Issues.NeedFix {
// if we don't fix we do this limiting to not annoy user;
// otherwise we need to fix all issues in the file at once
maxPerFileFromLinterConfig["gofmt"] = 1
maxPerFileFromLinterConfig["goimports"] = 1
}
return &MaxPerFileFromLinter{
flc: fileToLinterToCountMap{},
maxPerFileFromLinterConfig: maxPerFileFromLinterConfig,
}
}
func (p MaxPerFileFromLinter) Name() string {
return "max_per_file_from_linter"
}
func (p *MaxPerFileFromLinter) Process(issues []result.Issue) ([]result.Issue, error) {
return filterIssues(issues, func(i *result.Issue) bool {
limit := p.maxPerFileFromLinterConfig[i.FromLinter]
if limit == 0 {
return true
}
lm := p.flc[i.FilePath()]
if lm == nil {
p.flc[i.FilePath()] = linterToCountMap{}
}
count := p.flc[i.FilePath()][i.FromLinter]
if count >= limit {
return false
}
p.flc[i.FilePath()][i.FromLinter]++
return true
}), nil
}
func (p MaxPerFileFromLinter) Finish() {}