1
0
mirror of https://github.com/mgechev/revive.git synced 2025-01-26 03:52:12 +02:00
revive/config/config.go

226 lines
5.2 KiB
Go
Raw Normal View History

2020-10-29 14:11:52 +01:00
package config
2018-01-27 16:22:17 -08:00
import (
2020-10-29 14:11:52 +01:00
"errors"
2018-01-27 16:22:17 -08:00
"fmt"
"io/ioutil"
2018-02-03 18:56:45 -08:00
2018-01-27 16:22:17 -08:00
"github.com/mgechev/revive/formatter"
2020-10-29 14:11:52 +01:00
"github.com/BurntSushi/toml"
2018-01-27 16:22:17 -08:00
"github.com/mgechev/revive/lint"
"github.com/mgechev/revive/rule"
)
var defaultRules = []lint.Rule{
&rule.VarDeclarationsRule{},
&rule.PackageCommentsRule{},
&rule.DotImportsRule{},
&rule.BlankImportsRule{},
&rule.ExportedRule{},
2018-05-26 21:28:31 -07:00
&rule.VarNamingRule{},
2018-05-26 16:14:36 -07:00
&rule.IndentErrorFlowRule{},
2018-01-27 16:22:17 -08:00
&rule.RangeRule{},
&rule.ErrorfRule{},
2018-05-26 21:28:31 -07:00
&rule.ErrorNamingRule{},
2018-01-27 16:22:17 -08:00
&rule.ErrorStringsRule{},
2018-05-26 21:28:31 -07:00
&rule.ReceiverNamingRule{},
2018-01-27 16:22:17 -08:00
&rule.IncrementDecrementRule{},
&rule.ErrorReturnRule{},
&rule.UnexportedReturnRule{},
2018-05-26 21:28:31 -07:00
&rule.TimeNamingRule{},
&rule.ContextKeysType{},
&rule.ContextAsArgumentRule{},
2018-01-27 16:22:17 -08:00
}
var allRules = append([]lint.Rule{
&rule.ArgumentsLimitRule{},
&rule.CyclomaticRule{},
2018-05-26 21:28:31 -07:00
&rule.FileHeaderRule{},
&rule.EmptyBlockRule{},
2018-06-08 14:22:21 -07:00
&rule.SuperfluousElseRule{},
&rule.ConfusingNamingRule{},
&rule.GetReturnRule{},
&rule.ModifiesParamRule{},
&rule.ConfusingResultsRule{},
&rule.DeepExitRule{},
&rule.UnusedParamRule{},
&rule.UnreachableCodeRule{},
2018-07-17 21:21:27 +02:00
&rule.AddConstantRule{},
&rule.FlagParamRule{},
&rule.UnnecessaryStmtRule{},
2018-07-28 18:07:31 +02:00
&rule.StructTagRule{},
&rule.ModifiesValRecRule{},
&rule.ConstantLogicalExprRule{},
&rule.BoolLiteralRule{},
2018-09-14 04:19:49 +02:00
&rule.RedefinesBuiltinIDRule{},
&rule.ImportsBlacklistRule{},
&rule.FunctionResultsLimitRule{},
&rule.MaxPublicStructsRule{},
&rule.RangeValInClosureRule{},
&rule.RangeValAddress{},
&rule.WaitGroupByValueRule{},
&rule.AtomicRule{},
&rule.EmptyLinesRule{},
&rule.LineLengthLimitRule{},
2018-10-31 15:32:23 +01:00
&rule.CallToGCRule{},
&rule.DuplicatedImportsRule{},
&rule.ImportShadowingRule{},
&rule.BareReturnRule{},
&rule.UnusedReceiverRule{},
&rule.UnhandledErrorRule{},
2019-12-14 08:27:06 +01:00
&rule.CognitiveComplexityRule{},
&rule.StringOfIntRule{},
&rule.StringFormatRule{},
&rule.EarlyReturnRule{},
2020-05-09 17:10:34 +02:00
&rule.UnconditionalRecursionRule{},
&rule.IdenticalBranchesRule{},
2020-05-24 20:49:49 +02:00
&rule.DeferRule{},
&rule.UnexportedNamingRule{},
2021-03-20 23:45:30 +01:00
&rule.FunctionLength{},
&rule.NestedStructs{},
2021-06-29 22:04:51 +02:00
&rule.IfReturnRule{},
2018-01-27 16:22:17 -08:00
}, defaultRules...)
var allFormatters = []lint.Formatter{
2018-05-26 12:08:02 -07:00
&formatter.Stylish{},
2018-05-26 13:47:13 -07:00
&formatter.Friendly{},
2018-01-27 17:01:18 -08:00
&formatter.JSON{},
&formatter.NDJSON{},
2018-01-27 17:01:18 -08:00
&formatter.Default{},
2018-09-17 20:57:56 +02:00
&formatter.Unix{},
&formatter.Checkstyle{},
&formatter.Plain{},
2021-04-05 20:54:33 +02:00
&formatter.Sarif{},
2018-01-27 16:22:17 -08:00
}
func getFormatters() map[string]lint.Formatter {
result := map[string]lint.Formatter{}
for _, f := range allFormatters {
result[f.Name()] = f
}
return result
}
// GetLintingRules yields the linting rules that must be applied by the linter
2020-10-29 14:11:52 +01:00
func GetLintingRules(config *lint.Config) ([]lint.Rule, error) {
if config.EnableAllRules {
return getAllRules(config)
}
return getEnabledRules(config)
}
// getAllRules yields the list of all available rules except those disabled by configuration
func getAllRules(config *lint.Config) ([]lint.Rule, error) {
lintingRules := []lint.Rule{}
for _, r := range allRules {
ruleConf := config.Rules[r.Name()]
if ruleConf.Disabled {
continue // skip disabled rules
}
lintingRules = append(lintingRules, r)
}
return lintingRules, nil
}
// getEnabledRules yields the list of rules that are enabled by configuration
func getEnabledRules(config *lint.Config) ([]lint.Rule, error) {
2018-01-27 16:22:17 -08:00
rulesMap := map[string]lint.Rule{}
for _, r := range allRules {
rulesMap[r.Name()] = r
}
lintingRules := []lint.Rule{}
for name, ruleConfig := range config.Rules {
2018-01-27 16:22:17 -08:00
rule, ok := rulesMap[name]
if !ok {
2020-10-29 14:11:52 +01:00
return nil, fmt.Errorf("cannot find rule: %s", name)
2018-01-27 16:22:17 -08:00
}
if ruleConfig.Disabled {
continue // skip disabled rules
}
2018-01-27 16:22:17 -08:00
lintingRules = append(lintingRules, rule)
}
2020-10-29 14:11:52 +01:00
return lintingRules, nil
2018-01-27 16:22:17 -08:00
}
2020-10-29 14:11:52 +01:00
func parseConfig(path string) (*lint.Config, error) {
2018-01-27 16:22:17 -08:00
config := &lint.Config{}
file, err := ioutil.ReadFile(path)
if err != nil {
2020-10-29 14:11:52 +01:00
return nil, errors.New("cannot read the config file")
2018-01-27 16:22:17 -08:00
}
2020-10-29 14:11:52 +01:00
_, err = toml.Decode(string(file), config)
2018-01-27 16:22:17 -08:00
if err != nil {
2020-10-29 14:11:52 +01:00
return nil, fmt.Errorf("cannot parse the config file: %v", err)
2018-01-27 16:22:17 -08:00
}
2020-10-29 14:11:52 +01:00
return config, nil
2018-01-27 16:22:17 -08:00
}
func normalizeConfig(config *lint.Config) {
2018-01-27 17:01:18 -08:00
if config.Confidence == 0 {
config.Confidence = 0.8
}
2018-01-27 16:22:17 -08:00
severity := config.Severity
if severity != "" {
for k, v := range config.Rules {
if v.Severity == "" {
v.Severity = severity
}
config.Rules[k] = v
}
for k, v := range config.Directives {
if v.Severity == "" {
v.Severity = severity
}
config.Directives[k] = v
}
2018-01-27 16:22:17 -08:00
}
}
2020-10-29 14:11:52 +01:00
// GetConfig yields the configuration
func GetConfig(configPath string) (*lint.Config, error) {
2018-01-27 16:22:17 -08:00
config := defaultConfig()
if configPath != "" {
2020-10-29 14:11:52 +01:00
var err error
config, err = parseConfig(configPath)
if err != nil {
return nil, err
}
2018-01-27 16:22:17 -08:00
}
normalizeConfig(config)
2020-10-29 14:11:52 +01:00
return config, nil
2018-01-27 16:22:17 -08:00
}
2020-10-29 14:11:52 +01:00
// GetFormatter yields the formatter for lint failures
func GetFormatter(formatterName string) (lint.Formatter, error) {
2018-01-27 16:22:17 -08:00
formatters := getFormatters()
2018-01-27 17:01:18 -08:00
formatter := formatters["default"]
2018-01-27 16:22:17 -08:00
if formatterName != "" {
f, ok := formatters[formatterName]
if !ok {
2020-10-29 14:11:52 +01:00
return nil, fmt.Errorf("unknown formatter %v", formatterName)
2018-01-27 16:22:17 -08:00
}
formatter = f
}
2020-10-29 14:11:52 +01:00
return formatter, nil
}
2018-01-27 16:22:17 -08:00
func defaultConfig() *lint.Config {
defaultConfig := lint.Config{
Confidence: 0.0,
Severity: lint.SeverityWarning,
Rules: map[string]lint.RuleConfig{},
}
for _, r := range defaultRules {
defaultConfig.Rules[r.Name()] = lint.RuleConfig{}
}
return &defaultConfig
}