golangci-lint/pkg/lint/linter/config.go

120 lines
2.6 KiB
Go
Raw Normal View History

2018-06-02 11:36:50 +03:00
package linter
import (
"golang.org/x/tools/go/packages"
)
2018-06-02 11:36:50 +03:00
const (
PresetFormatting = "format"
PresetComplexity = "complexity"
PresetStyle = "style"
PresetBugs = "bugs"
PresetUnused = "unused"
PresetPerformance = "performance"
)
type Config struct {
Linter Linter
EnabledByDefault bool
LoadMode packages.LoadMode
NeedsSSARepr bool
2018-06-02 11:36:50 +03:00
InPresets []string
Speed int // more value means faster execution of linter
AlternativeNames []string
2018-06-02 11:36:50 +03:00
OriginalURL string // URL of original (not forked) repo, needed for autogenerated README
ParentLinterName string // used only for megacheck's children now
CanAutoFix bool
IsSlow bool
}
func (lc *Config) ConsiderSlow() *Config {
lc.IsSlow = true
return lc
}
func (lc *Config) IsSlowLinter() bool {
return lc.IsSlow || (lc.LoadMode&packages.NeedTypesInfo != 0 && lc.LoadMode&packages.NeedDeps != 0)
}
func (lc *Config) WithLoadFiles() *Config {
lc.LoadMode |= packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles
return lc
}
func (lc *Config) WithLoadForGoAnalysis() *Config {
lc = lc.WithLoadFiles()
lc.LoadMode |= packages.NeedImports | packages.NeedDeps | packages.NeedExportsFile | packages.NeedTypesSizes
return lc.ConsiderSlow()
2018-06-02 11:36:50 +03:00
}
func (lc *Config) WithLoadTypeInfo() *Config {
lc = lc.WithLoadFiles()
lc.LoadMode |= packages.NeedImports | packages.NeedTypes | packages.NeedTypesSizes | packages.NeedTypesInfo | packages.NeedSyntax
2018-06-02 11:36:50 +03:00
return lc
}
func (lc *Config) WithLoadDepsTypeInfo() *Config {
lc = lc.WithLoadTypeInfo()
lc.LoadMode |= packages.NeedDeps
return lc
}
func (lc *Config) WithSSA() *Config {
lc = lc.WithLoadDepsTypeInfo()
2018-06-02 11:36:50 +03:00
lc.NeedsSSARepr = true
return lc
}
func (lc *Config) WithPresets(presets ...string) *Config {
2018-06-02 11:36:50 +03:00
lc.InPresets = presets
return lc
}
func (lc *Config) WithSpeed(speed int) *Config {
2018-06-02 11:36:50 +03:00
lc.Speed = speed
return lc
}
func (lc *Config) WithURL(url string) *Config {
2018-06-02 11:36:50 +03:00
lc.OriginalURL = url
return lc
}
func (lc *Config) WithAlternativeNames(names ...string) *Config {
lc.AlternativeNames = names
return lc
}
func (lc *Config) WithParent(parentLinterName string) *Config {
lc.ParentLinterName = parentLinterName
return lc
}
func (lc *Config) WithAutoFix() *Config {
lc.CanAutoFix = true
return lc
}
func (lc *Config) GetSpeed() int {
2018-06-02 11:36:50 +03:00
return lc.Speed
}
func (lc *Config) AllNames() []string {
return append([]string{lc.Name()}, lc.AlternativeNames...)
}
func (lc *Config) Name() string {
return lc.Linter.Name()
}
2018-06-02 11:36:50 +03:00
func NewConfig(linter Linter) *Config {
lc := &Config{
2018-06-02 11:36:50 +03:00
Linter: linter,
}
return lc.WithLoadFiles()
2018-06-02 11:36:50 +03:00
}