2018-06-02 11:36:50 +03:00
|
|
|
package lintersdb
|
2018-05-06 13:25:50 +03:00
|
|
|
|
|
|
|
import (
|
2018-08-08 23:25:34 +03:00
|
|
|
"os"
|
2018-05-06 13:25:50 +03:00
|
|
|
|
2019-03-17 22:47:29 +03:00
|
|
|
"github.com/golangci/golangci-lint/pkg/config"
|
|
|
|
|
2018-05-06 19:08:34 +03:00
|
|
|
"github.com/golangci/golangci-lint/pkg/golinters"
|
2018-06-02 11:36:50 +03:00
|
|
|
"github.com/golangci/golangci-lint/pkg/lint/linter"
|
2018-05-06 13:25:50 +03:00
|
|
|
)
|
|
|
|
|
2018-09-01 11:01:17 +03:00
|
|
|
type Manager struct {
|
2019-01-03 11:33:05 +01:00
|
|
|
nameToLC map[string]*linter.Config
|
2019-03-17 22:47:29 +03:00
|
|
|
cfg *config.Config
|
2018-09-01 11:01:17 +03:00
|
|
|
}
|
|
|
|
|
2019-03-17 22:47:29 +03:00
|
|
|
func NewManager(cfg *config.Config) *Manager {
|
|
|
|
m := &Manager{cfg: cfg}
|
2019-01-03 11:33:05 +01:00
|
|
|
nameToLC := make(map[string]*linter.Config)
|
2018-09-01 11:01:17 +03:00
|
|
|
for _, lc := range m.GetAllSupportedLinterConfigs() {
|
2018-09-01 14:16:30 +03:00
|
|
|
for _, name := range lc.AllNames() {
|
|
|
|
nameToLC[name] = lc
|
|
|
|
}
|
2018-09-01 11:01:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
m.nameToLC = nameToLC
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Manager) AllPresets() []string {
|
2019-03-18 08:37:16 +03:00
|
|
|
return []string{linter.PresetBugs, linter.PresetComplexity, linter.PresetFormatting,
|
|
|
|
linter.PresetPerformance, linter.PresetStyle, linter.PresetUnused}
|
2018-05-08 17:13:16 +03:00
|
|
|
}
|
|
|
|
|
2018-09-01 11:01:17 +03:00
|
|
|
func (m Manager) allPresetsSet() map[string]bool {
|
2018-05-08 17:13:16 +03:00
|
|
|
ret := map[string]bool{}
|
2018-09-01 11:01:17 +03:00
|
|
|
for _, p := range m.AllPresets() {
|
2018-05-08 17:13:16 +03:00
|
|
|
ret[p] = true
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2019-01-08 14:31:04 +03:00
|
|
|
func (m Manager) GetMetaLinter(name string) linter.MetaLinter {
|
|
|
|
return m.GetMetaLinters()[name]
|
|
|
|
}
|
|
|
|
|
2018-09-01 14:16:30 +03:00
|
|
|
func (m Manager) GetLinterConfig(name string) *linter.Config {
|
2018-09-01 11:01:17 +03:00
|
|
|
lc, ok := m.nameToLC[name]
|
2018-05-06 19:08:34 +03:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-03 11:33:05 +01:00
|
|
|
return lc
|
2018-05-06 19:08:34 +03:00
|
|
|
}
|
|
|
|
|
2019-01-03 11:33:05 +01:00
|
|
|
func enableLinterConfigs(lcs []*linter.Config, isEnabled func(lc *linter.Config) bool) []*linter.Config {
|
|
|
|
var ret []*linter.Config
|
2018-05-13 09:30:17 +03:00
|
|
|
for _, lc := range lcs {
|
2018-11-05 22:29:45 +03:00
|
|
|
lc := lc
|
2019-01-03 11:33:05 +01:00
|
|
|
lc.EnabledByDefault = isEnabled(lc)
|
2018-05-13 09:30:17 +03:00
|
|
|
ret = append(ret, lc)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2019-01-08 14:31:04 +03:00
|
|
|
func (Manager) GetMetaLinters() map[string]linter.MetaLinter {
|
|
|
|
metaLinters := []linter.MetaLinter{
|
|
|
|
golinters.MegacheckMetalinter{},
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := map[string]linter.MetaLinter{}
|
|
|
|
for _, metaLinter := range metaLinters {
|
|
|
|
ret[metaLinter.Name()] = metaLinter
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2019-09-09 18:07:09 +03:00
|
|
|
//nolint:funlen
|
2019-03-17 22:47:29 +03:00
|
|
|
func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
|
|
|
|
var govetCfg *config.GovetSettings
|
|
|
|
if m.cfg != nil {
|
|
|
|
govetCfg = &m.cfg.LintersSettings.Govet
|
|
|
|
}
|
2019-01-03 11:33:05 +01:00
|
|
|
lcs := []*linter.Config{
|
2019-03-17 22:47:29 +03:00
|
|
|
linter.NewConfig(golinters.NewGovet(govetCfg)).
|
2019-09-17 08:42:16 +03:00
|
|
|
WithLoadForGoAnalysis().
|
2018-06-02 11:36:50 +03:00
|
|
|
WithPresets(linter.PresetBugs).
|
|
|
|
WithSpeed(4).
|
2019-01-08 14:31:04 +03:00
|
|
|
WithAlternativeNames("vet", "vetshadow").
|
2018-06-02 11:36:50 +03:00
|
|
|
WithURL("https://golang.org/cmd/vet/"),
|
2019-05-11 00:21:15 +02:00
|
|
|
linter.NewConfig(golinters.NewBodyclose()).
|
2019-09-17 08:42:16 +03:00
|
|
|
WithLoadForGoAnalysis().
|
2019-05-11 00:21:15 +02:00
|
|
|
WithPresets(linter.PresetPerformance, linter.PresetBugs).
|
|
|
|
WithSpeed(4).
|
|
|
|
WithURL("https://github.com/timakin/bodyclose"),
|
2018-06-02 11:36:50 +03:00
|
|
|
linter.NewConfig(golinters.Errcheck{}).
|
2019-09-17 08:42:16 +03:00
|
|
|
WithLoadTypeInfo().
|
2018-06-02 11:36:50 +03:00
|
|
|
WithPresets(linter.PresetBugs).
|
|
|
|
WithSpeed(10).
|
|
|
|
WithURL("https://github.com/kisielk/errcheck"),
|
|
|
|
linter.NewConfig(golinters.Golint{}).
|
|
|
|
WithPresets(linter.PresetStyle).
|
|
|
|
WithSpeed(3).
|
|
|
|
WithURL("https://github.com/golang/lint"),
|
|
|
|
|
2019-01-08 14:31:04 +03:00
|
|
|
linter.NewConfig(golinters.NewStaticcheck()).
|
2019-09-17 08:42:16 +03:00
|
|
|
WithLoadForGoAnalysis().
|
2018-06-02 11:36:50 +03:00
|
|
|
WithPresets(linter.PresetBugs).
|
|
|
|
WithSpeed(2).
|
|
|
|
WithURL("https://staticcheck.io/"),
|
2019-01-08 14:31:04 +03:00
|
|
|
linter.NewConfig(golinters.NewUnused()).
|
2019-09-17 08:42:16 +03:00
|
|
|
WithLoadDepsTypeInfo().
|
2018-06-02 11:36:50 +03:00
|
|
|
WithPresets(linter.PresetUnused).
|
|
|
|
WithSpeed(5).
|
2019-09-25 11:25:58 -04:00
|
|
|
WithURL("https://github.com/dominikh/go-tools/tree/master/unused"),
|
2019-01-08 14:31:04 +03:00
|
|
|
linter.NewConfig(golinters.NewGosimple()).
|
2019-09-17 08:42:16 +03:00
|
|
|
WithLoadForGoAnalysis().
|
2018-06-02 11:36:50 +03:00
|
|
|
WithPresets(linter.PresetStyle).
|
|
|
|
WithSpeed(5).
|
2019-09-25 11:25:58 -04:00
|
|
|
WithURL("https://github.com/dominikh/go-tools/tree/master/simple"),
|
2019-01-08 14:31:04 +03:00
|
|
|
linter.NewConfig(golinters.NewStylecheck()).
|
2019-09-17 08:42:16 +03:00
|
|
|
WithLoadForGoAnalysis().
|
2019-01-08 14:31:04 +03:00
|
|
|
WithPresets(linter.PresetStyle).
|
|
|
|
WithSpeed(5).
|
|
|
|
WithURL("https://github.com/dominikh/go-tools/tree/master/stylecheck"),
|
2018-06-02 11:36:50 +03:00
|
|
|
|
2018-09-01 14:16:30 +03:00
|
|
|
linter.NewConfig(golinters.Gosec{}).
|
2019-09-17 08:42:16 +03:00
|
|
|
WithLoadTypeInfo().
|
2018-06-02 11:36:50 +03:00
|
|
|
WithPresets(linter.PresetBugs).
|
|
|
|
WithSpeed(8).
|
2018-09-01 14:16:30 +03:00
|
|
|
WithURL("https://github.com/securego/gosec").
|
|
|
|
WithAlternativeNames("gas"),
|
2018-06-02 11:36:50 +03:00
|
|
|
linter.NewConfig(golinters.Structcheck{}).
|
2019-09-17 08:42:16 +03:00
|
|
|
WithLoadTypeInfo().
|
2018-06-02 11:36:50 +03:00
|
|
|
WithPresets(linter.PresetUnused).
|
|
|
|
WithSpeed(10).
|
|
|
|
WithURL("https://github.com/opennota/check"),
|
|
|
|
linter.NewConfig(golinters.Varcheck{}).
|
2019-09-17 08:42:16 +03:00
|
|
|
WithLoadTypeInfo().
|
2018-06-02 11:36:50 +03:00
|
|
|
WithPresets(linter.PresetUnused).
|
|
|
|
WithSpeed(10).
|
|
|
|
WithURL("https://github.com/opennota/check"),
|
|
|
|
linter.NewConfig(golinters.Interfacer{}).
|
2019-09-17 08:42:16 +03:00
|
|
|
WithLoadDepsTypeInfo().
|
2018-06-02 11:36:50 +03:00
|
|
|
WithSSA().
|
|
|
|
WithPresets(linter.PresetStyle).
|
|
|
|
WithSpeed(6).
|
|
|
|
WithURL("https://github.com/mvdan/interfacer"),
|
|
|
|
linter.NewConfig(golinters.Unconvert{}).
|
2019-09-17 08:42:16 +03:00
|
|
|
WithLoadTypeInfo().
|
2018-06-02 11:36:50 +03:00
|
|
|
WithPresets(linter.PresetStyle).
|
|
|
|
WithSpeed(10).
|
|
|
|
WithURL("https://github.com/mdempsky/unconvert"),
|
|
|
|
linter.NewConfig(golinters.Ineffassign{}).
|
|
|
|
WithPresets(linter.PresetUnused).
|
|
|
|
WithSpeed(9).
|
|
|
|
WithURL("https://github.com/gordonklaus/ineffassign"),
|
|
|
|
linter.NewConfig(golinters.Dupl{}).
|
|
|
|
WithPresets(linter.PresetStyle).
|
|
|
|
WithSpeed(7).
|
|
|
|
WithURL("https://github.com/mibk/dupl"),
|
|
|
|
linter.NewConfig(golinters.Goconst{}).
|
|
|
|
WithPresets(linter.PresetStyle).
|
|
|
|
WithSpeed(9).
|
|
|
|
WithURL("https://github.com/jgautheron/goconst"),
|
|
|
|
linter.NewConfig(golinters.Deadcode{}).
|
2019-09-17 08:42:16 +03:00
|
|
|
WithLoadTypeInfo().
|
2018-06-02 11:36:50 +03:00
|
|
|
WithPresets(linter.PresetUnused).
|
|
|
|
WithSpeed(10).
|
|
|
|
WithURL("https://github.com/remyoudompheng/go-misc/tree/master/deadcode"),
|
|
|
|
linter.NewConfig(golinters.Gocyclo{}).
|
|
|
|
WithPresets(linter.PresetComplexity).
|
|
|
|
WithSpeed(8).
|
|
|
|
WithURL("https://github.com/alecthomas/gocyclo"),
|
|
|
|
linter.NewConfig(golinters.TypeCheck{}).
|
2019-09-17 08:42:16 +03:00
|
|
|
WithLoadTypeInfo().
|
2018-06-02 11:36:50 +03:00
|
|
|
WithPresets(linter.PresetBugs).
|
|
|
|
WithSpeed(10).
|
|
|
|
WithURL(""),
|
|
|
|
|
|
|
|
linter.NewConfig(golinters.Gofmt{}).
|
|
|
|
WithPresets(linter.PresetFormatting).
|
|
|
|
WithSpeed(7).
|
2019-02-16 21:23:57 +03:00
|
|
|
WithAutoFix().
|
2018-06-02 11:36:50 +03:00
|
|
|
WithURL("https://golang.org/cmd/gofmt/"),
|
|
|
|
linter.NewConfig(golinters.Gofmt{UseGoimports: true}).
|
|
|
|
WithPresets(linter.PresetFormatting).
|
|
|
|
WithSpeed(5).
|
2019-02-16 21:23:57 +03:00
|
|
|
WithAutoFix().
|
2018-06-02 11:36:50 +03:00
|
|
|
WithURL("https://godoc.org/golang.org/x/tools/cmd/goimports"),
|
|
|
|
linter.NewConfig(golinters.Maligned{}).
|
2019-09-17 08:42:16 +03:00
|
|
|
WithLoadTypeInfo().
|
2018-06-02 11:36:50 +03:00
|
|
|
WithPresets(linter.PresetPerformance).
|
|
|
|
WithSpeed(10).
|
|
|
|
WithURL("https://github.com/mdempsky/maligned"),
|
|
|
|
linter.NewConfig(golinters.Depguard{}).
|
2019-09-17 08:42:16 +03:00
|
|
|
WithLoadTypeInfo().
|
2018-06-02 11:36:50 +03:00
|
|
|
WithPresets(linter.PresetStyle).
|
|
|
|
WithSpeed(6).
|
|
|
|
WithURL("https://github.com/OpenPeeDeeP/depguard"),
|
2018-06-28 21:27:07 +03:00
|
|
|
linter.NewConfig(golinters.Misspell{}).
|
|
|
|
WithPresets(linter.PresetStyle).
|
|
|
|
WithSpeed(7).
|
2019-02-16 21:23:57 +03:00
|
|
|
WithAutoFix().
|
2018-06-28 21:27:07 +03:00
|
|
|
WithURL("https://github.com/client9/misspell"),
|
2018-06-28 22:39:23 +03:00
|
|
|
linter.NewConfig(golinters.Lll{}).
|
|
|
|
WithPresets(linter.PresetStyle).
|
|
|
|
WithSpeed(10).
|
|
|
|
WithURL("https://github.com/walle/lll"),
|
2018-06-29 00:12:30 +03:00
|
|
|
linter.NewConfig(golinters.Unparam{}).
|
|
|
|
WithPresets(linter.PresetUnused).
|
|
|
|
WithSpeed(3).
|
2019-09-17 08:42:16 +03:00
|
|
|
WithLoadDepsTypeInfo().
|
2018-06-29 00:12:30 +03:00
|
|
|
WithSSA().
|
|
|
|
WithURL("https://github.com/mvdan/unparam"),
|
2019-09-17 01:44:53 -04:00
|
|
|
linter.NewConfig(golinters.Dogsled{}).
|
|
|
|
WithPresets(linter.PresetStyle).
|
|
|
|
WithSpeed(10).
|
|
|
|
WithURL("https://github.com/alexkohler/dogsled"),
|
2018-06-30 13:55:47 +03:00
|
|
|
linter.NewConfig(golinters.Nakedret{}).
|
|
|
|
WithPresets(linter.PresetComplexity).
|
|
|
|
WithSpeed(10).
|
|
|
|
WithURL("https://github.com/alexkohler/nakedret"),
|
2018-06-30 21:02:41 +03:00
|
|
|
linter.NewConfig(golinters.Prealloc{}).
|
|
|
|
WithPresets(linter.PresetPerformance).
|
|
|
|
WithSpeed(8).
|
|
|
|
WithURL("https://github.com/alexkohler/prealloc"),
|
2018-11-05 22:29:45 +03:00
|
|
|
linter.NewConfig(golinters.Scopelint{}).
|
|
|
|
WithPresets(linter.PresetBugs).
|
|
|
|
WithSpeed(8).
|
|
|
|
WithURL("https://github.com/kyoh86/scopelint"),
|
2018-11-05 18:20:28 +03:00
|
|
|
linter.NewConfig(golinters.Gocritic{}).
|
|
|
|
WithPresets(linter.PresetStyle).
|
|
|
|
WithSpeed(5).
|
2019-09-17 08:42:16 +03:00
|
|
|
WithLoadTypeInfo().
|
2018-11-05 18:20:28 +03:00
|
|
|
WithURL("https://github.com/go-critic/go-critic"),
|
2018-11-07 10:31:55 +03:00
|
|
|
linter.NewConfig(golinters.Gochecknoinits{}).
|
|
|
|
WithPresets(linter.PresetStyle).
|
|
|
|
WithSpeed(10).
|
|
|
|
WithURL("https://github.com/leighmcculloch/gochecknoinits"),
|
|
|
|
linter.NewConfig(golinters.Gochecknoglobals{}).
|
|
|
|
WithPresets(linter.PresetStyle).
|
|
|
|
WithSpeed(10).
|
|
|
|
WithURL("https://github.com/leighmcculloch/gochecknoglobals"),
|
2019-09-11 15:53:40 +02:00
|
|
|
linter.NewConfig(golinters.Godox{}).
|
|
|
|
WithPresets(linter.PresetStyle).
|
|
|
|
WithSpeed(10).
|
|
|
|
WithURL("https://github.com/matoous/godox"),
|
2019-09-09 15:56:30 +02:00
|
|
|
linter.NewConfig(golinters.Funlen{}).
|
|
|
|
WithPresets(linter.PresetStyle).
|
|
|
|
WithSpeed(10).
|
|
|
|
WithURL("https://github.com/ultraware/funlen"),
|
2019-09-10 12:25:01 +02:00
|
|
|
linter.NewConfig(golinters.Whitespace{}).
|
|
|
|
WithPresets(linter.PresetStyle).
|
|
|
|
WithSpeed(10).
|
2019-09-10 16:56:44 +03:00
|
|
|
WithAutoFix().
|
2019-09-10 12:25:01 +02:00
|
|
|
WithURL("https://github.com/ultraware/whitespace"),
|
2018-05-13 09:30:17 +03:00
|
|
|
}
|
|
|
|
|
2018-08-12 22:11:21 +03:00
|
|
|
isLocalRun := os.Getenv("GOLANGCI_COM_RUN") == ""
|
2019-01-08 14:31:04 +03:00
|
|
|
enabledByDefault := map[string]bool{
|
2019-03-17 22:47:29 +03:00
|
|
|
golinters.NewGovet(nil).Name(): true,
|
2019-01-08 14:31:04 +03:00
|
|
|
golinters.Errcheck{}.Name(): true,
|
|
|
|
golinters.Staticcheck{}.Name(): true,
|
|
|
|
golinters.Unused{}.Name(): true,
|
|
|
|
golinters.Gosimple{}.Name(): true,
|
|
|
|
golinters.Structcheck{}.Name(): true,
|
|
|
|
golinters.Varcheck{}.Name(): true,
|
|
|
|
golinters.Ineffassign{}.Name(): true,
|
|
|
|
golinters.Deadcode{}.Name(): true,
|
2018-08-08 23:25:34 +03:00
|
|
|
|
|
|
|
// don't typecheck for golangci.com: too many troubles
|
2018-08-12 22:11:21 +03:00
|
|
|
golinters.TypeCheck{}.Name(): isLocalRun,
|
2018-05-13 09:30:17 +03:00
|
|
|
}
|
2018-06-02 11:36:50 +03:00
|
|
|
return enableLinterConfigs(lcs, func(lc *linter.Config) bool {
|
2019-01-08 14:31:04 +03:00
|
|
|
return enabledByDefault[lc.Name()]
|
2018-05-13 09:30:17 +03:00
|
|
|
})
|
2018-05-06 13:25:50 +03:00
|
|
|
}
|
|
|
|
|
2019-01-03 11:33:05 +01:00
|
|
|
func (m Manager) GetAllEnabledByDefaultLinters() []*linter.Config {
|
|
|
|
var ret []*linter.Config
|
2018-09-01 11:01:17 +03:00
|
|
|
for _, lc := range m.GetAllSupportedLinterConfigs() {
|
2018-05-06 13:25:50 +03:00
|
|
|
if lc.EnabledByDefault {
|
2018-05-31 23:53:01 +03:00
|
|
|
ret = append(ret, lc)
|
2018-05-06 13:25:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2019-01-03 11:33:05 +01:00
|
|
|
func linterConfigsToMap(lcs []*linter.Config) map[string]*linter.Config {
|
2018-06-02 11:36:50 +03:00
|
|
|
ret := map[string]*linter.Config{}
|
2018-05-31 23:53:01 +03:00
|
|
|
for _, lc := range lcs {
|
|
|
|
lc := lc // local copy
|
2019-01-03 11:33:05 +01:00
|
|
|
ret[lc.Name()] = lc
|
2018-05-06 13:25:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2019-01-03 11:33:05 +01:00
|
|
|
func (m Manager) GetAllLinterConfigsForPreset(p string) []*linter.Config {
|
|
|
|
var ret []*linter.Config
|
2018-09-01 11:01:17 +03:00
|
|
|
for _, lc := range m.GetAllSupportedLinterConfigs() {
|
2018-05-08 17:13:16 +03:00
|
|
|
for _, ip := range lc.InPresets {
|
|
|
|
if p == ip {
|
2018-05-31 23:53:01 +03:00
|
|
|
ret = append(ret, lc)
|
2018-05-08 17:13:16 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|