github.com/vanstinator/golangci-lint@v0.0.0-20240223191551-cc572f00d9d1/pkg/golinters/protogetter.go (about)

     1  package golinters
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/ghostiam/protogetter"
     7  	"golang.org/x/tools/go/analysis"
     8  
     9  	"github.com/vanstinator/golangci-lint/pkg/config"
    10  	"github.com/vanstinator/golangci-lint/pkg/golinters/goanalysis"
    11  	"github.com/vanstinator/golangci-lint/pkg/lint/linter"
    12  	"github.com/vanstinator/golangci-lint/pkg/result"
    13  )
    14  
    15  func NewProtoGetter(settings *config.ProtoGetterSettings) *goanalysis.Linter {
    16  	var mu sync.Mutex
    17  	var resIssues []goanalysis.Issue
    18  
    19  	var cfg protogetter.Config
    20  	if settings != nil {
    21  		cfg = protogetter.Config{
    22  			SkipGeneratedBy:         settings.SkipGeneratedBy,
    23  			SkipFiles:               settings.SkipFiles,
    24  			SkipAnyGenerated:        settings.SkipAnyGenerated,
    25  			ReplaceFirstArgInAppend: settings.ReplaceFirstArgInAppend,
    26  		}
    27  	}
    28  	cfg.Mode = protogetter.GolangciLintMode
    29  
    30  	a := protogetter.NewAnalyzer(&cfg)
    31  	a.Run = func(pass *analysis.Pass) (any, error) {
    32  		pgIssues, err := protogetter.Run(pass, &cfg)
    33  		if err != nil {
    34  			return nil, err
    35  		}
    36  
    37  		issues := make([]goanalysis.Issue, len(pgIssues))
    38  		for i, issue := range pgIssues {
    39  			report := &result.Issue{
    40  				FromLinter: a.Name,
    41  				Pos:        issue.Pos,
    42  				Text:       issue.Message,
    43  				Replacement: &result.Replacement{
    44  					Inline: &result.InlineFix{
    45  						StartCol:  issue.InlineFix.StartCol,
    46  						Length:    issue.InlineFix.Length,
    47  						NewString: issue.InlineFix.NewString,
    48  					},
    49  				},
    50  			}
    51  
    52  			issues[i] = goanalysis.NewIssue(report, pass)
    53  		}
    54  
    55  		if len(issues) == 0 {
    56  			return nil, nil
    57  		}
    58  
    59  		mu.Lock()
    60  		resIssues = append(resIssues, issues...)
    61  		mu.Unlock()
    62  
    63  		return nil, nil
    64  	}
    65  
    66  	return goanalysis.NewLinter(
    67  		a.Name,
    68  		a.Doc,
    69  		[]*analysis.Analyzer{a},
    70  		nil,
    71  	).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
    72  		return resIssues
    73  	}).WithLoadMode(goanalysis.LoadModeTypesInfo)
    74  }