github.com/nozzle/golangci-lint@v1.49.0-nz3/pkg/golinters/goconst.go (about)

     1  package golinters
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  
     7  	goconstAPI "github.com/jgautheron/goconst"
     8  	"golang.org/x/tools/go/analysis"
     9  
    10  	"github.com/golangci/golangci-lint/pkg/config"
    11  	"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
    12  	"github.com/golangci/golangci-lint/pkg/lint/linter"
    13  	"github.com/golangci/golangci-lint/pkg/result"
    14  )
    15  
    16  const goconstName = "goconst"
    17  
    18  //nolint:dupl
    19  func NewGoconst(settings *config.GoConstSettings) *goanalysis.Linter {
    20  	var mu sync.Mutex
    21  	var resIssues []goanalysis.Issue
    22  
    23  	analyzer := &analysis.Analyzer{
    24  		Name: goconstName,
    25  		Doc:  goanalysis.TheOnlyanalyzerDoc,
    26  		Run: func(pass *analysis.Pass) (interface{}, error) {
    27  			issues, err := runGoconst(pass, settings)
    28  			if err != nil {
    29  				return nil, err
    30  			}
    31  
    32  			if len(issues) == 0 {
    33  				return nil, nil
    34  			}
    35  
    36  			mu.Lock()
    37  			resIssues = append(resIssues, issues...)
    38  			mu.Unlock()
    39  
    40  			return nil, nil
    41  		},
    42  	}
    43  
    44  	return goanalysis.NewLinter(
    45  		goconstName,
    46  		"Finds repeated strings that could be replaced by a constant",
    47  		[]*analysis.Analyzer{analyzer},
    48  		nil,
    49  	).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
    50  		return resIssues
    51  	}).WithLoadMode(goanalysis.LoadModeSyntax)
    52  }
    53  
    54  func runGoconst(pass *analysis.Pass, settings *config.GoConstSettings) ([]goanalysis.Issue, error) {
    55  	cfg := goconstAPI.Config{
    56  		IgnoreTests:        settings.IgnoreTests,
    57  		MatchWithConstants: settings.MatchWithConstants,
    58  		MinStringLength:    settings.MinStringLen,
    59  		MinOccurrences:     settings.MinOccurrencesCount,
    60  		ParseNumbers:       settings.ParseNumbers,
    61  		NumberMin:          settings.NumberMin,
    62  		NumberMax:          settings.NumberMax,
    63  		ExcludeTypes:       map[goconstAPI.Type]bool{},
    64  	}
    65  
    66  	if settings.IgnoreCalls {
    67  		cfg.ExcludeTypes[goconstAPI.Call] = true
    68  	}
    69  
    70  	lintIssues, err := goconstAPI.Run(pass.Files, pass.Fset, &cfg)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  
    75  	if len(lintIssues) == 0 {
    76  		return nil, nil
    77  	}
    78  
    79  	res := make([]goanalysis.Issue, 0, len(lintIssues))
    80  	for _, i := range lintIssues {
    81  		text := fmt.Sprintf("string %s has %d occurrences", formatCode(i.Str, nil), i.OccurrencesCount)
    82  
    83  		if i.MatchingConst == "" {
    84  			text += ", make it a constant"
    85  		} else {
    86  			text += fmt.Sprintf(", but such constant %s already exists", formatCode(i.MatchingConst, nil))
    87  		}
    88  
    89  		res = append(res, goanalysis.NewIssue(&result.Issue{
    90  			Pos:        i.Pos,
    91  			Text:       text,
    92  			FromLinter: goconstName,
    93  		}, pass))
    94  	}
    95  
    96  	return res, nil
    97  }