github.com/elek/golangci-lint@v1.42.2-0.20211208090441-c05b7fcb3a9a/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/tools/go/analysis"
     8  
     9  	"github.com/elek/golangci-lint/pkg/config"
    10  	"github.com/elek/golangci-lint/pkg/golinters/goanalysis"
    11  )
    12  
    13  func NewThelper(cfg *config.ThelperSettings) *goanalysis.Linter {
    14  	a := analyzer.NewAnalyzer()
    15  
    16  	cfgMap := map[string]map[string]interface{}{}
    17  	if cfg != nil {
    18  		var opts []string
    19  
    20  		if cfg.Test.Name {
    21  			opts = append(opts, "t_name")
    22  		}
    23  		if cfg.Test.Begin {
    24  			opts = append(opts, "t_begin")
    25  		}
    26  		if cfg.Test.First {
    27  			opts = append(opts, "t_first")
    28  		}
    29  
    30  		if cfg.Benchmark.Name {
    31  			opts = append(opts, "b_name")
    32  		}
    33  		if cfg.Benchmark.Begin {
    34  			opts = append(opts, "b_begin")
    35  		}
    36  		if cfg.Benchmark.First {
    37  			opts = append(opts, "b_first")
    38  		}
    39  
    40  		if cfg.TB.Name {
    41  			opts = append(opts, "tb_name")
    42  		}
    43  		if cfg.TB.Begin {
    44  			opts = append(opts, "tb_begin")
    45  		}
    46  		if cfg.TB.First {
    47  			opts = append(opts, "tb_first")
    48  		}
    49  
    50  		cfgMap[a.Name] = map[string]interface{}{
    51  			"checks": strings.Join(opts, ","),
    52  		}
    53  	}
    54  
    55  	return goanalysis.NewLinter(
    56  		"thelper",
    57  		"thelper detects golang test helpers without t.Helper() call and checks the consistency of test helpers",
    58  		[]*analysis.Analyzer{a},
    59  		cfgMap,
    60  	).WithLoadMode(goanalysis.LoadModeTypesInfo)
    61  }