github.com/thrasher-corp/golangci-lint@v1.17.3/pkg/lint/lintersdb/manager.go (about)

     1  package lintersdb
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/golangci/golangci-lint/pkg/config"
     7  
     8  	"github.com/golangci/golangci-lint/pkg/golinters"
     9  	"github.com/golangci/golangci-lint/pkg/lint/linter"
    10  )
    11  
    12  type Manager struct {
    13  	nameToLC map[string]*linter.Config
    14  	cfg      *config.Config
    15  }
    16  
    17  func NewManager(cfg *config.Config) *Manager {
    18  	m := &Manager{cfg: cfg}
    19  	nameToLC := make(map[string]*linter.Config)
    20  	for _, lc := range m.GetAllSupportedLinterConfigs() {
    21  		for _, name := range lc.AllNames() {
    22  			nameToLC[name] = lc
    23  		}
    24  	}
    25  
    26  	m.nameToLC = nameToLC
    27  	return m
    28  }
    29  
    30  func (Manager) AllPresets() []string {
    31  	return []string{linter.PresetBugs, linter.PresetComplexity, linter.PresetFormatting,
    32  		linter.PresetPerformance, linter.PresetStyle, linter.PresetUnused}
    33  }
    34  
    35  func (m Manager) allPresetsSet() map[string]bool {
    36  	ret := map[string]bool{}
    37  	for _, p := range m.AllPresets() {
    38  		ret[p] = true
    39  	}
    40  	return ret
    41  }
    42  
    43  func (m Manager) GetMetaLinter(name string) linter.MetaLinter {
    44  	return m.GetMetaLinters()[name]
    45  }
    46  
    47  func (m Manager) GetLinterConfig(name string) *linter.Config {
    48  	lc, ok := m.nameToLC[name]
    49  	if !ok {
    50  		return nil
    51  	}
    52  
    53  	return lc
    54  }
    55  
    56  func enableLinterConfigs(lcs []*linter.Config, isEnabled func(lc *linter.Config) bool) []*linter.Config {
    57  	var ret []*linter.Config
    58  	for _, lc := range lcs {
    59  		lc := lc
    60  		lc.EnabledByDefault = isEnabled(lc)
    61  		ret = append(ret, lc)
    62  	}
    63  
    64  	return ret
    65  }
    66  
    67  func (Manager) GetMetaLinters() map[string]linter.MetaLinter {
    68  	metaLinters := []linter.MetaLinter{
    69  		golinters.MegacheckMetalinter{},
    70  	}
    71  
    72  	ret := map[string]linter.MetaLinter{}
    73  	for _, metaLinter := range metaLinters {
    74  		ret[metaLinter.Name()] = metaLinter
    75  	}
    76  
    77  	return ret
    78  }
    79  
    80  func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
    81  	var govetCfg *config.GovetSettings
    82  	if m.cfg != nil {
    83  		govetCfg = &m.cfg.LintersSettings.Govet
    84  	}
    85  	lcs := []*linter.Config{
    86  		linter.NewConfig(golinters.NewGovet(govetCfg)).
    87  			WithSSA(). // TODO: extract from the linter config and don't build SSA, just use LoadAllSyntax mode
    88  			WithPresets(linter.PresetBugs).
    89  			WithSpeed(4).
    90  			WithAlternativeNames("vet", "vetshadow").
    91  			WithURL("https://golang.org/cmd/vet/"),
    92  		linter.NewConfig(golinters.NewBodyclose()).
    93  			WithSSA().
    94  			WithPresets(linter.PresetPerformance, linter.PresetBugs).
    95  			WithSpeed(4).
    96  			WithURL("https://github.com/timakin/bodyclose"),
    97  		linter.NewConfig(golinters.Errcheck{}).
    98  			WithTypeInfo().
    99  			WithPresets(linter.PresetBugs).
   100  			WithSpeed(10).
   101  			WithURL("https://github.com/kisielk/errcheck"),
   102  		linter.NewConfig(golinters.Golint{}).
   103  			WithPresets(linter.PresetStyle).
   104  			WithSpeed(3).
   105  			WithURL("https://github.com/golang/lint"),
   106  
   107  		linter.NewConfig(golinters.NewStaticcheck()).
   108  			WithSSA().
   109  			WithPresets(linter.PresetBugs).
   110  			WithSpeed(2).
   111  			WithURL("https://staticcheck.io/"),
   112  		linter.NewConfig(golinters.NewUnused()).
   113  			WithSSA().
   114  			WithPresets(linter.PresetUnused).
   115  			WithSpeed(5).
   116  			WithURL("https://github.com/dominikh/go-tools/tree/master/cmd/unused"),
   117  		linter.NewConfig(golinters.NewGosimple()).
   118  			WithSSA().
   119  			WithPresets(linter.PresetStyle).
   120  			WithSpeed(5).
   121  			WithURL("https://github.com/dominikh/go-tools/tree/master/cmd/gosimple"),
   122  		linter.NewConfig(golinters.NewStylecheck()).
   123  			WithSSA().
   124  			WithPresets(linter.PresetStyle).
   125  			WithSpeed(5).
   126  			WithURL("https://github.com/dominikh/go-tools/tree/master/stylecheck"),
   127  
   128  		linter.NewConfig(golinters.Gosec{}).
   129  			WithTypeInfo().
   130  			WithPresets(linter.PresetBugs).
   131  			WithSpeed(8).
   132  			WithURL("https://github.com/securego/gosec").
   133  			WithAlternativeNames("gas"),
   134  		linter.NewConfig(golinters.Structcheck{}).
   135  			WithTypeInfo().
   136  			WithPresets(linter.PresetUnused).
   137  			WithSpeed(10).
   138  			WithURL("https://github.com/opennota/check"),
   139  		linter.NewConfig(golinters.Varcheck{}).
   140  			WithTypeInfo().
   141  			WithPresets(linter.PresetUnused).
   142  			WithSpeed(10).
   143  			WithURL("https://github.com/opennota/check"),
   144  		linter.NewConfig(golinters.Interfacer{}).
   145  			WithSSA().
   146  			WithPresets(linter.PresetStyle).
   147  			WithSpeed(6).
   148  			WithURL("https://github.com/mvdan/interfacer"),
   149  		linter.NewConfig(golinters.Unconvert{}).
   150  			WithTypeInfo().
   151  			WithPresets(linter.PresetStyle).
   152  			WithSpeed(10).
   153  			WithURL("https://github.com/mdempsky/unconvert"),
   154  		linter.NewConfig(golinters.Ineffassign{}).
   155  			WithPresets(linter.PresetUnused).
   156  			WithSpeed(9).
   157  			WithURL("https://github.com/gordonklaus/ineffassign"),
   158  		linter.NewConfig(golinters.Dupl{}).
   159  			WithPresets(linter.PresetStyle).
   160  			WithSpeed(7).
   161  			WithURL("https://github.com/mibk/dupl"),
   162  		linter.NewConfig(golinters.Goconst{}).
   163  			WithPresets(linter.PresetStyle).
   164  			WithSpeed(9).
   165  			WithURL("https://github.com/jgautheron/goconst"),
   166  		linter.NewConfig(golinters.Deadcode{}).
   167  			WithTypeInfo().
   168  			WithPresets(linter.PresetUnused).
   169  			WithSpeed(10).
   170  			WithURL("https://github.com/remyoudompheng/go-misc/tree/master/deadcode"),
   171  		linter.NewConfig(golinters.Gocyclo{}).
   172  			WithPresets(linter.PresetComplexity).
   173  			WithSpeed(8).
   174  			WithURL("https://github.com/alecthomas/gocyclo"),
   175  		linter.NewConfig(golinters.TypeCheck{}).
   176  			WithTypeInfo().
   177  			WithPresets(linter.PresetBugs).
   178  			WithSpeed(10).
   179  			WithURL(""),
   180  
   181  		linter.NewConfig(golinters.Gofmt{}).
   182  			WithPresets(linter.PresetFormatting).
   183  			WithSpeed(7).
   184  			WithAutoFix().
   185  			WithURL("https://golang.org/cmd/gofmt/"),
   186  		linter.NewConfig(golinters.Gofmt{UseGoimports: true}).
   187  			WithPresets(linter.PresetFormatting).
   188  			WithSpeed(5).
   189  			WithAutoFix().
   190  			WithURL("https://godoc.org/golang.org/x/tools/cmd/goimports"),
   191  		linter.NewConfig(golinters.Maligned{}).
   192  			WithTypeInfo().
   193  			WithPresets(linter.PresetPerformance).
   194  			WithSpeed(10).
   195  			WithURL("https://github.com/mdempsky/maligned"),
   196  		linter.NewConfig(golinters.Depguard{}).
   197  			WithTypeInfo().
   198  			WithPresets(linter.PresetStyle).
   199  			WithSpeed(6).
   200  			WithURL("https://github.com/OpenPeeDeeP/depguard"),
   201  		linter.NewConfig(golinters.Misspell{}).
   202  			WithPresets(linter.PresetStyle).
   203  			WithSpeed(7).
   204  			WithAutoFix().
   205  			WithURL("https://github.com/client9/misspell"),
   206  		linter.NewConfig(golinters.Lll{}).
   207  			WithPresets(linter.PresetStyle).
   208  			WithSpeed(10).
   209  			WithURL("https://github.com/walle/lll"),
   210  		linter.NewConfig(golinters.Unparam{}).
   211  			WithPresets(linter.PresetUnused).
   212  			WithSpeed(3).
   213  			WithSSA().
   214  			WithURL("https://github.com/mvdan/unparam"),
   215  		linter.NewConfig(golinters.Nakedret{}).
   216  			WithPresets(linter.PresetComplexity).
   217  			WithSpeed(10).
   218  			WithURL("https://github.com/alexkohler/nakedret"),
   219  		linter.NewConfig(golinters.Prealloc{}).
   220  			WithPresets(linter.PresetPerformance).
   221  			WithSpeed(8).
   222  			WithURL("https://github.com/alexkohler/prealloc"),
   223  		linter.NewConfig(golinters.Scopelint{}).
   224  			WithPresets(linter.PresetBugs).
   225  			WithSpeed(8).
   226  			WithURL("https://github.com/kyoh86/scopelint"),
   227  		linter.NewConfig(golinters.Gocritic{}).
   228  			WithPresets(linter.PresetStyle).
   229  			WithSpeed(5).
   230  			WithTypeInfo().
   231  			WithURL("https://github.com/go-critic/go-critic"),
   232  		linter.NewConfig(golinters.Gochecknoinits{}).
   233  			WithPresets(linter.PresetStyle).
   234  			WithSpeed(10).
   235  			WithURL("https://github.com/leighmcculloch/gochecknoinits"),
   236  		linter.NewConfig(golinters.Gochecknoglobals{}).
   237  			WithPresets(linter.PresetStyle).
   238  			WithSpeed(10).
   239  			WithURL("https://github.com/leighmcculloch/gochecknoglobals"),
   240  	}
   241  
   242  	isLocalRun := os.Getenv("GOLANGCI_COM_RUN") == ""
   243  	enabledByDefault := map[string]bool{
   244  		golinters.NewGovet(nil).Name(): true,
   245  		golinters.Errcheck{}.Name():    true,
   246  		golinters.Staticcheck{}.Name(): true,
   247  		golinters.Unused{}.Name():      true,
   248  		golinters.Gosimple{}.Name():    true,
   249  		golinters.Structcheck{}.Name(): true,
   250  		golinters.Varcheck{}.Name():    true,
   251  		golinters.Ineffassign{}.Name(): true,
   252  		golinters.Deadcode{}.Name():    true,
   253  
   254  		// don't typecheck for golangci.com: too many troubles
   255  		golinters.TypeCheck{}.Name(): isLocalRun,
   256  	}
   257  	return enableLinterConfigs(lcs, func(lc *linter.Config) bool {
   258  		return enabledByDefault[lc.Name()]
   259  	})
   260  }
   261  
   262  func (m Manager) GetAllEnabledByDefaultLinters() []*linter.Config {
   263  	var ret []*linter.Config
   264  	for _, lc := range m.GetAllSupportedLinterConfigs() {
   265  		if lc.EnabledByDefault {
   266  			ret = append(ret, lc)
   267  		}
   268  	}
   269  
   270  	return ret
   271  }
   272  
   273  func linterConfigsToMap(lcs []*linter.Config) map[string]*linter.Config {
   274  	ret := map[string]*linter.Config{}
   275  	for _, lc := range lcs {
   276  		lc := lc // local copy
   277  		ret[lc.Name()] = lc
   278  	}
   279  
   280  	return ret
   281  }
   282  
   283  func (m Manager) GetAllLinterConfigsForPreset(p string) []*linter.Config {
   284  	var ret []*linter.Config
   285  	for _, lc := range m.GetAllSupportedLinterConfigs() {
   286  		for _, ip := range lc.InPresets {
   287  			if p == ip {
   288  				ret = append(ret, lc)
   289  				break
   290  			}
   291  		}
   292  	}
   293  
   294  	return ret
   295  }