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

     1  package golinters
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/kulti/thelper/pkg/analyzer"
     7  	"golang.org/x/exp/maps"
     8  	"golang.org/x/tools/go/analysis"
     9  
    10  	"github.com/vanstinator/golangci-lint/pkg/config"
    11  	"github.com/vanstinator/golangci-lint/pkg/golinters/goanalysis"
    12  )
    13  
    14  func NewThelper(cfg *config.ThelperSettings) *goanalysis.Linter {
    15  	a := analyzer.NewAnalyzer()
    16  
    17  	opts := map[string]struct{}{
    18  		"t_name":  {},
    19  		"t_begin": {},
    20  		"t_first": {},
    21  
    22  		"f_name":  {},
    23  		"f_begin": {},
    24  		"f_first": {},
    25  
    26  		"b_name":  {},
    27  		"b_begin": {},
    28  		"b_first": {},
    29  
    30  		"tb_name":  {},
    31  		"tb_begin": {},
    32  		"tb_first": {},
    33  	}
    34  
    35  	if cfg != nil {
    36  		applyTHelperOptions(cfg.Test, "t_", opts)
    37  		applyTHelperOptions(cfg.Fuzz, "f_", opts)
    38  		applyTHelperOptions(cfg.Benchmark, "b_", opts)
    39  		applyTHelperOptions(cfg.TB, "tb_", opts)
    40  	}
    41  
    42  	if len(opts) == 0 {
    43  		linterLogger.Fatalf("thelper: at least one option must be enabled")
    44  	}
    45  
    46  	args := maps.Keys(opts)
    47  
    48  	cfgMap := map[string]map[string]any{
    49  		a.Name: {
    50  			"checks": strings.Join(args, ","),
    51  		},
    52  	}
    53  
    54  	return goanalysis.NewLinter(
    55  		a.Name,
    56  		a.Doc,
    57  		[]*analysis.Analyzer{a},
    58  		cfgMap,
    59  	).WithLoadMode(goanalysis.LoadModeTypesInfo)
    60  }
    61  
    62  func applyTHelperOptions(o config.ThelperOptions, prefix string, opts map[string]struct{}) {
    63  	if o.Name != nil {
    64  		if !*o.Name {
    65  			delete(opts, prefix+"name")
    66  		}
    67  	}
    68  
    69  	if o.Begin != nil {
    70  		if !*o.Begin {
    71  			delete(opts, prefix+"begin")
    72  		}
    73  	}
    74  
    75  	if o.First != nil {
    76  		if !*o.First {
    77  			delete(opts, prefix+"first")
    78  		}
    79  	}
    80  }