2018-06-02 11:36:50 +03:00
|
|
|
package linter
|
|
|
|
|
2019-09-17 08:42:16 +03:00
|
|
|
import (
|
|
|
|
"golang.org/x/tools/go/packages"
|
|
|
|
)
|
|
|
|
|
2018-06-02 11:36:50 +03:00
|
|
|
const (
|
2021-03-16 13:33:28 +01:00
|
|
|
PresetBugs = "bugs" // Related to bugs detection.
|
|
|
|
PresetComment = "comment" // Related to comments analysis.
|
|
|
|
PresetComplexity = "complexity" // Related to code complexity analysis.
|
|
|
|
PresetError = "error" // Related to error handling analysis.
|
|
|
|
PresetFormatting = "format" // Related to code formatting.
|
|
|
|
PresetImport = "import" // Related to imports analysis.
|
|
|
|
PresetMetaLinter = "metalinter" // Related to linter that contains multiple rules or multiple linters.
|
|
|
|
PresetModule = "module" // Related to Go modules analysis.
|
|
|
|
PresetPerformance = "performance" // Related to performance.
|
|
|
|
PresetSQL = "sql" // Related to SQL.
|
|
|
|
PresetStyle = "style" // Related to coding style.
|
|
|
|
PresetTest = "test" // Related to the analysis of the code of the tests.
|
|
|
|
PresetUnused = "unused" // Related to the detection of unused code.
|
2018-06-02 11:36:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Linter Linter
|
|
|
|
EnabledByDefault bool
|
2018-10-21 17:06:51 +03:00
|
|
|
|
2019-09-17 08:42:16 +03:00
|
|
|
LoadMode packages.LoadMode
|
|
|
|
|
2018-06-02 11:36:50 +03:00
|
|
|
InPresets []string
|
2018-09-01 14:16:30 +03:00
|
|
|
AlternativeNames []string
|
2018-06-02 11:36:50 +03:00
|
|
|
|
2021-02-20 18:55:11 -06:00
|
|
|
OriginalURL string // URL of original (not forked) repo, needed for autogenerated README
|
|
|
|
CanAutoFix bool
|
|
|
|
IsSlow bool
|
|
|
|
DoesChangeTypes bool
|
|
|
|
DeprecatedMessage string
|
2019-09-17 08:42:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (lc *Config) ConsiderSlow() *Config {
|
|
|
|
lc.IsSlow = true
|
|
|
|
return lc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lc *Config) IsSlowLinter() bool {
|
2021-03-15 08:13:54 +01:00
|
|
|
return lc.IsSlow
|
2019-09-17 08:42:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2021-03-15 08:13:54 +01:00
|
|
|
lc.IsSlow = true
|
2018-06-02 11:36:50 +03:00
|
|
|
return lc
|
|
|
|
}
|
|
|
|
|
2019-01-03 11:33:05 +01:00
|
|
|
func (lc *Config) WithPresets(presets ...string) *Config {
|
2018-06-02 11:36:50 +03:00
|
|
|
lc.InPresets = presets
|
|
|
|
return lc
|
|
|
|
}
|
|
|
|
|
2019-01-03 11:33:05 +01:00
|
|
|
func (lc *Config) WithURL(url string) *Config {
|
2018-06-02 11:36:50 +03:00
|
|
|
lc.OriginalURL = url
|
|
|
|
return lc
|
|
|
|
}
|
|
|
|
|
2019-01-03 11:33:05 +01:00
|
|
|
func (lc *Config) WithAlternativeNames(names ...string) *Config {
|
2018-09-01 14:16:30 +03:00
|
|
|
lc.AlternativeNames = names
|
|
|
|
return lc
|
|
|
|
}
|
|
|
|
|
2019-02-16 21:23:57 +03:00
|
|
|
func (lc *Config) WithAutoFix() *Config {
|
|
|
|
lc.CanAutoFix = true
|
|
|
|
return lc
|
|
|
|
}
|
|
|
|
|
2020-05-05 18:45:19 +03:00
|
|
|
func (lc *Config) WithChangeTypes() *Config {
|
|
|
|
lc.DoesChangeTypes = true
|
|
|
|
return lc
|
|
|
|
}
|
|
|
|
|
2021-02-20 18:55:11 -06:00
|
|
|
func (lc *Config) Deprecated(message string) *Config {
|
|
|
|
lc.DeprecatedMessage = message
|
|
|
|
return lc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lc *Config) IsDeprecated() bool {
|
|
|
|
return lc.DeprecatedMessage != ""
|
|
|
|
}
|
|
|
|
|
2019-01-03 11:33:05 +01:00
|
|
|
func (lc *Config) AllNames() []string {
|
2018-09-01 14:16:30 +03:00
|
|
|
return append([]string{lc.Name()}, lc.AlternativeNames...)
|
|
|
|
}
|
|
|
|
|
2019-01-03 11:33:05 +01:00
|
|
|
func (lc *Config) Name() string {
|
2018-09-01 14:16:30 +03:00
|
|
|
return lc.Linter.Name()
|
|
|
|
}
|
|
|
|
|
2018-06-02 11:36:50 +03:00
|
|
|
func NewConfig(linter Linter) *Config {
|
2019-09-17 08:42:16 +03:00
|
|
|
lc := &Config{
|
2018-06-02 11:36:50 +03:00
|
|
|
Linter: linter,
|
|
|
|
}
|
2019-09-17 08:42:16 +03:00
|
|
|
return lc.WithLoadFiles()
|
2018-06-02 11:36:50 +03:00
|
|
|
}
|