1
0
mirror of https://github.com/mgechev/revive.git synced 2025-01-24 03:47:45 +02:00

Adds configuration option to enable all available rules (#521)

Adds configuration option to enable all available rules
This commit is contained in:
SalvadorC 2021-05-21 09:53:10 +02:00 committed by GitHub
parent 5b4bd9a3c2
commit b298ffe4ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 92 additions and 3 deletions

View File

@ -240,6 +240,59 @@ warningCode = 0
severity = "error"
```
By default `revive` will enable only the linting rules that are named in the configuration file.
For example, the previous configuration file makes `revive` to enable only _cyclomatic_ and _package-comments_ linting rules.
To enable all available rules you need to add:
```toml
enableAllRules = true
```
This will enable all available rules no matter of what rules are named in the configuration file.
To disable a rule, you simply mark it as disabled in the configuration.
For example:
```toml
[rule.line-length-limit]
Disabled = true
```
When enabling all rules you still need/can provide specific configurations for rules.
The following files is an example configuration were all rules are enabled, with exception to those that are explicitly disabled, and some rules are configured with particular arguments:
```toml
severity = "warning"
confidence = 0.8
errorCode = 0
warningCode = 0
# Enable all available rules
enableAllRules = true
# Disabled rules
[rule.blank-imports]
Disabled = true
[rule.file-header]
Disabled = true
[rule.max-public-structs]
Disabled = true
[rule.line-length-limit]
Disabled = true
[rule.function-length]
Disabled = true
# Rule tunning
[rule.argument-limit]
Arguments = [5]
[rule.cyclomatic]
Arguments = [10]
[rule.cognitive-complexity]
Arguments = [7]
[rule.function-result-limit]
Arguments = [3]
```
### Default Configuration
The default configuration of `revive` can be found at `defaults.toml`. This will enable all rules available in `golint` and use their default configuration (i.e. the way they are hardcoded in `golint`).
@ -541,4 +594,3 @@ REVIVE_FORCE_COLOR=1 revive -formatter friendly ./... | tee revive.log
## License
MIT

View File

@ -101,19 +101,48 @@ func getFormatters() map[string]lint.Formatter {
return result
}
// GetLintingRules yields the linting rules activated in the configuration
// GetLintingRules yields the linting rules that must be applied by the linter
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) {
rulesMap := map[string]lint.Rule{}
for _, r := range allRules {
rulesMap[r.Name()] = r
}
lintingRules := []lint.Rule{}
for name := range config.Rules {
for name, ruleConfig := range config.Rules {
rule, ok := rulesMap[name]
if !ok {
return nil, fmt.Errorf("cannot find rule: %s", name)
}
if ruleConfig.Disabled {
continue // skip disabled rules
}
lintingRules = append(lintingRules, rule)
}

View File

@ -7,6 +7,7 @@ type Arguments = []interface{}
type RuleConfig struct {
Arguments Arguments
Severity Severity
Disabled bool
}
// RulesConfig defines the config for all rules.
@ -25,6 +26,7 @@ type Config struct {
IgnoreGeneratedHeader bool `toml:"ignoreGeneratedHeader"`
Confidence float64
Severity Severity
EnableAllRules bool `toml:"enableAllRules"`
Rules RulesConfig `toml:"rule"`
ErrorCode int `toml:"errorCode"`
WarningCode int `toml:"warningCode"`

View File

@ -17,6 +17,9 @@ type CyclomaticRule struct{}
func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
var failures []lint.Failure
if len(arguments) == 0 {
panic("not enough arguments for " + r.Name())
}
complexity, ok := arguments[0].(int64) // Alt. non panicking version
if !ok {
panic("invalid argument for cyclomatic complexity")

View File

@ -14,6 +14,9 @@ type MaxPublicStructsRule struct{}
// Apply applies the rule to given file.
func (r *MaxPublicStructsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
var failures []lint.Failure
if len(arguments) == 0 {
panic("not enough arguments for " + r.Name())
}
fileAst := file.AST
walker := &lintMaxPublicStructs{