github.com/vanstinator/golangci-lint@v0.0.0-20240223191551-cc572f00d9d1/pkg/result/processors/uniq_by_line.go (about)

     1  package processors
     2  
     3  import (
     4  	"github.com/vanstinator/golangci-lint/pkg/config"
     5  	"github.com/vanstinator/golangci-lint/pkg/result"
     6  )
     7  
     8  type lineToCount map[int]int
     9  type fileToLineToCount map[string]lineToCount
    10  
    11  type UniqByLine struct {
    12  	flc fileToLineToCount
    13  	cfg *config.Config
    14  }
    15  
    16  func NewUniqByLine(cfg *config.Config) *UniqByLine {
    17  	return &UniqByLine{
    18  		flc: fileToLineToCount{},
    19  		cfg: cfg,
    20  	}
    21  }
    22  
    23  var _ Processor = &UniqByLine{}
    24  
    25  func (p *UniqByLine) Name() string {
    26  	return "uniq_by_line"
    27  }
    28  
    29  func (p *UniqByLine) Process(issues []result.Issue) ([]result.Issue, error) {
    30  	if !p.cfg.Output.UniqByLine {
    31  		return issues, nil
    32  	}
    33  
    34  	return filterIssues(issues, func(i *result.Issue) bool {
    35  		if i.Replacement != nil && p.cfg.Issues.NeedFix {
    36  			// if issue will be auto-fixed we shouldn't collapse issues:
    37  			// e.g. one line can contain 2 misspellings, they will be in 2 issues and misspell should fix both of them.
    38  			return true
    39  		}
    40  
    41  		lc := p.flc[i.FilePath()]
    42  		if lc == nil {
    43  			lc = lineToCount{}
    44  			p.flc[i.FilePath()] = lc
    45  		}
    46  
    47  		const limit = 1
    48  		count := lc[i.Line()]
    49  		if count == limit {
    50  			return false
    51  		}
    52  
    53  		lc[i.Line()]++
    54  		return true
    55  	}), nil
    56  }
    57  
    58  func (p *UniqByLine) Finish() {}