1
0
mirror of https://github.com/mgechev/revive.git synced 2025-01-30 04:31:00 +02:00
revive/config/config.go
ccoVeille 3421eaecf0
refactor: fix linting issues (#1188)
* refactor: fix thelper issues

test/utils_test.go:19:6   thelper  test helper function should start from t.Helper()
test/utils_test.go:42:6   thelper  test helper function should start from t.Helper()
test/utils_test.go:63:6   thelper  test helper function should start from t.Helper()
test/utils_test.go:146:6  thelper  test helper function should start from t.Helper()

* refactor: fix govet issues

rule/error_strings.go:50:21  govet  printf: non-constant format string in call to fmt.Errorf

* refactor: fix gosimple issue

rule/bare_return.go:52:9  gosimple  S1016: should convert w (type lintBareReturnRule) to bareReturnFinder instead of using struct literal

* refactor: fix errorlint issues

lint/filefilter.go:70:86    errorlint  non-wrapping format verb for fmt.Errorf. Use `%w` to format errors
lint/filefilter.go:113:104  errorlint  non-wrapping format verb for fmt.Errorf. Use `%w` to format errors
lint/filefilter.go:125:89   errorlint  non-wrapping format verb for fmt.Errorf. Use `%w` to format errors
lint/linter.go:166:72       errorlint  non-wrapping format verb for fmt.Errorf. Use `%w` to format errors
lint/linter.go:171:73       errorlint  non-wrapping format verb for fmt.Errorf. Use `%w` to format errors
config/config.go:174:57     errorlint  non-wrapping format verb for fmt.Errorf. Use `%w` to format errors
config/config.go:179:64     errorlint  non-wrapping format verb for fmt.Errorf. Use `%w` to format errors

* refactor: fix revive issue about comment spacing

cli/main.go:31:2 revive comment-spacings: no space between comment delimiter and comment text

* refactor: fix revive issue about unused-receiver

revivelib/core_test.go:77:7                     revive       unused-receiver: method receiver 'r' is not referenced in method's body, consider removing or renaming it as _
revivelib/core_test.go:81:7                     revive       unused-receiver: method receiver 'r' is not referenced in method's body, consider removing or renaming it as _
rule/context_as_argument.go:76:7                revive       unused-receiver: method receiver 'r' is not referenced in method's body, consider removing or renaming it as _
rule/var_naming.go:73:7                         revive       unused-receiver: method receiver 'r' is not referenced in method's body, consider removing or renaming it as _
rule/modifies_value_receiver.go:59:7            revive       unused-receiver: method receiver 'r' is not referenced in method's body, consider removing or renaming it as _
rule/filename_format.go:43:7                    revive       unused-receiver: method receiver 'r' is not referenced in method's body, consider removing or renaming it as _

* refactor: fix revive issues about unused-parameter

revivelib/core_test.go:81:24                    revive       unused-parameter: parameter 'file' seems to be unused, consider removing or renaming it as _
revivelib/core_test.go:81:41                    revive       unused-parameter: parameter 'arguments' seems to be unused, consider removing or renaming it as _

* refactor: fix gocritic issues about commentedOutCode

test/utils_test.go:119:5                       gocritic  commentedOutCode: may want to remove commented-out code
rule/unreachable_code.go:65:3                  gocritic  commentedOutCode: may want to remove commented-out code
2024-12-12 08:42:41 +01:00

267 lines
6.3 KiB
Go

// Package config implements revive's configuration data structures and related methods
package config
import (
"errors"
"fmt"
"os"
"github.com/BurntSushi/toml"
"github.com/mgechev/revive/formatter"
"github.com/mgechev/revive/lint"
"github.com/mgechev/revive/rule"
)
var defaultRules = []lint.Rule{
&rule.VarDeclarationsRule{},
&rule.PackageCommentsRule{},
&rule.DotImportsRule{},
&rule.BlankImportsRule{},
&rule.ExportedRule{},
&rule.VarNamingRule{},
&rule.IndentErrorFlowRule{},
&rule.RangeRule{},
&rule.ErrorfRule{},
&rule.ErrorNamingRule{},
&rule.ErrorStringsRule{},
&rule.ReceiverNamingRule{},
&rule.IncrementDecrementRule{},
&rule.ErrorReturnRule{},
&rule.UnexportedReturnRule{},
&rule.TimeNamingRule{},
&rule.ContextKeysType{},
&rule.ContextAsArgumentRule{},
&rule.EmptyBlockRule{},
&rule.SuperfluousElseRule{},
&rule.UnusedParamRule{},
&rule.UnreachableCodeRule{},
&rule.RedefinesBuiltinIDRule{},
}
var allRules = append([]lint.Rule{
&rule.ArgumentsLimitRule{},
&rule.CyclomaticRule{},
&rule.FileHeaderRule{},
&rule.ConfusingNamingRule{},
&rule.GetReturnRule{},
&rule.ModifiesParamRule{},
&rule.ConfusingResultsRule{},
&rule.DeepExitRule{},
&rule.AddConstantRule{},
&rule.FlagParamRule{},
&rule.UnnecessaryStmtRule{},
&rule.StructTagRule{},
&rule.ModifiesValRecRule{},
&rule.ConstantLogicalExprRule{},
&rule.BoolLiteralRule{},
&rule.ImportsBlocklistRule{},
&rule.FunctionResultsLimitRule{},
&rule.MaxPublicStructsRule{},
&rule.RangeValInClosureRule{},
&rule.RangeValAddress{},
&rule.WaitGroupByValueRule{},
&rule.AtomicRule{},
&rule.EmptyLinesRule{},
&rule.LineLengthLimitRule{},
&rule.CallToGCRule{},
&rule.DuplicatedImportsRule{},
&rule.ImportShadowingRule{},
&rule.BareReturnRule{},
&rule.UnusedReceiverRule{},
&rule.UnhandledErrorRule{},
&rule.CognitiveComplexityRule{},
&rule.StringOfIntRule{},
&rule.StringFormatRule{},
&rule.EarlyReturnRule{},
&rule.UnconditionalRecursionRule{},
&rule.IdenticalBranchesRule{},
&rule.DeferRule{},
&rule.UnexportedNamingRule{},
&rule.FunctionLength{},
&rule.NestedStructs{},
&rule.UselessBreak{},
&rule.UncheckedTypeAssertionRule{},
&rule.TimeEqualRule{},
&rule.BannedCharsRule{},
&rule.OptimizeOperandsOrderRule{},
&rule.UseAnyRule{},
&rule.DataRaceRule{},
&rule.CommentSpacingsRule{},
&rule.IfReturnRule{},
&rule.RedundantImportAlias{},
&rule.ImportAliasNamingRule{},
&rule.EnforceMapStyleRule{},
&rule.EnforceRepeatedArgTypeStyleRule{},
&rule.EnforceSliceStyleRule{},
&rule.MaxControlNestingRule{},
&rule.CommentsDensityRule{},
&rule.FileLengthLimitRule{},
&rule.FilenameFormatRule{},
&rule.RedundantBuildTagRule{},
&rule.UseErrorsNewRule{},
}, defaultRules...)
// allFormatters is a list of all available formatters to output the linting results.
// Keep the list sorted and in sync with available formatters in README.md.
var allFormatters = []lint.Formatter{
&formatter.Checkstyle{},
&formatter.Default{},
&formatter.Friendly{},
&formatter.JSON{},
&formatter.NDJSON{},
&formatter.Plain{},
&formatter.Sarif{},
&formatter.Stylish{},
&formatter.Unix{},
}
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
func GetLintingRules(config *lint.Config, extraRules []lint.Rule) ([]lint.Rule, error) {
rulesMap := map[string]lint.Rule{}
for _, r := range allRules {
rulesMap[r.Name()] = r
}
for _, r := range extraRules {
if _, ok := rulesMap[r.Name()]; ok {
continue
}
rulesMap[r.Name()] = r
}
var lintingRules []lint.Rule
for name, ruleConfig := range config.Rules {
actualName := actualRuleName(name)
r, ok := rulesMap[actualName]
if !ok {
return nil, fmt.Errorf("cannot find rule: %s", name)
}
if ruleConfig.Disabled {
continue // skip disabled rules
}
lintingRules = append(lintingRules, r)
}
return lintingRules, nil
}
func actualRuleName(name string) string {
switch name {
case "imports-blacklist":
return "imports-blocklist"
default:
return name
}
}
func parseConfig(path string, config *lint.Config) error {
file, err := os.ReadFile(path)
if err != nil {
return errors.New("cannot read the config file")
}
err = toml.Unmarshal(file, config)
if err != nil {
return fmt.Errorf("cannot parse the config file: %w", err)
}
for k, r := range config.Rules {
err := r.Initialize()
if err != nil {
return fmt.Errorf("error in config of rule [%s] : [%w]", k, err)
}
config.Rules[k] = r
}
return nil
}
func normalizeConfig(config *lint.Config) {
if len(config.Rules) == 0 {
config.Rules = map[string]lint.RuleConfig{}
}
if config.EnableAllRules {
// Add to the configuration all rules not yet present in it
for _, r := range allRules {
ruleName := r.Name()
_, alreadyInConf := config.Rules[ruleName]
if alreadyInConf {
continue
}
// Add the rule with an empty conf for
config.Rules[ruleName] = lint.RuleConfig{}
}
}
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
}
}
}
const defaultConfidence = 0.8
// GetConfig yields the configuration
func GetConfig(configPath string) (*lint.Config, error) {
config := &lint.Config{}
switch {
case configPath != "":
config.Confidence = defaultConfidence
err := parseConfig(configPath, config)
if err != nil {
return nil, err
}
default: // no configuration provided
config = defaultConfig()
}
normalizeConfig(config)
return config, nil
}
// GetFormatter yields the formatter for lint failures
func GetFormatter(formatterName string) (lint.Formatter, error) {
formatters := getFormatters()
result := formatters["default"]
if formatterName != "" {
f, ok := formatters[formatterName]
if !ok {
return nil, fmt.Errorf("unknown formatter %v", formatterName)
}
result = f
}
return result, nil
}
func defaultConfig() *lint.Config {
defaultConfig := lint.Config{
Confidence: defaultConfidence,
Severity: lint.SeverityWarning,
Rules: map[string]lint.RuleConfig{},
}
for _, r := range defaultRules {
defaultConfig.Rules[r.Name()] = lint.RuleConfig{}
}
return &defaultConfig
}