Skip to content

Commit b298ffe

Browse files
authored
Adds configuration option to enable all available rules (#521)
Adds configuration option to enable all available rules
1 parent 5b4bd9a commit b298ffe

File tree

5 files changed

+92
-3
lines changed

5 files changed

+92
-3
lines changed

README.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,59 @@ warningCode = 0
240240
severity = "error"
241241
```
242242

243+
By default `revive` will enable only the linting rules that are named in the configuration file.
244+
For example, the previous configuration file makes `revive` to enable only _cyclomatic_ and _package-comments_ linting rules.
245+
246+
To enable all available rules you need to add:
247+
248+
```toml
249+
enableAllRules = true
250+
```
251+
252+
This will enable all available rules no matter of what rules are named in the configuration file.
253+
254+
To disable a rule, you simply mark it as disabled in the configuration.
255+
For example:
256+
257+
```toml
258+
[rule.line-length-limit]
259+
Disabled = true
260+
```
261+
When enabling all rules you still need/can provide specific configurations for rules.
262+
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:
263+
264+
```toml
265+
severity = "warning"
266+
confidence = 0.8
267+
errorCode = 0
268+
warningCode = 0
269+
270+
# Enable all available rules
271+
enableAllRules = true
272+
273+
# Disabled rules
274+
[rule.blank-imports]
275+
Disabled = true
276+
[rule.file-header]
277+
Disabled = true
278+
[rule.max-public-structs]
279+
Disabled = true
280+
[rule.line-length-limit]
281+
Disabled = true
282+
[rule.function-length]
283+
Disabled = true
284+
285+
# Rule tunning
286+
[rule.argument-limit]
287+
Arguments = [5]
288+
[rule.cyclomatic]
289+
Arguments = [10]
290+
[rule.cognitive-complexity]
291+
Arguments = [7]
292+
[rule.function-result-limit]
293+
Arguments = [3]
294+
```
295+
243296
### Default Configuration
244297

245298
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
541594
## License
542595

543596
MIT
544-

config/config.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,48 @@ func getFormatters() map[string]lint.Formatter {
101101
return result
102102
}
103103

104-
// GetLintingRules yields the linting rules activated in the configuration
104+
// GetLintingRules yields the linting rules that must be applied by the linter
105105
func GetLintingRules(config *lint.Config) ([]lint.Rule, error) {
106+
if config.EnableAllRules {
107+
return getAllRules(config)
108+
}
109+
110+
return getEnabledRules(config)
111+
}
112+
113+
// getAllRules yields the list of all available rules except those disabled by configuration
114+
func getAllRules(config *lint.Config) ([]lint.Rule, error) {
115+
lintingRules := []lint.Rule{}
116+
for _, r := range allRules {
117+
ruleConf := config.Rules[r.Name()]
118+
if ruleConf.Disabled {
119+
continue // skip disabled rules
120+
}
121+
122+
lintingRules = append(lintingRules, r)
123+
}
124+
125+
return lintingRules, nil
126+
}
127+
128+
// getEnabledRules yields the list of rules that are enabled by configuration
129+
func getEnabledRules(config *lint.Config) ([]lint.Rule, error) {
106130
rulesMap := map[string]lint.Rule{}
107131
for _, r := range allRules {
108132
rulesMap[r.Name()] = r
109133
}
110134

111135
lintingRules := []lint.Rule{}
112-
for name := range config.Rules {
136+
for name, ruleConfig := range config.Rules {
113137
rule, ok := rulesMap[name]
114138
if !ok {
115139
return nil, fmt.Errorf("cannot find rule: %s", name)
116140
}
141+
142+
if ruleConfig.Disabled {
143+
continue // skip disabled rules
144+
}
145+
117146
lintingRules = append(lintingRules, rule)
118147
}
119148

lint/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type Arguments = []interface{}
77
type RuleConfig struct {
88
Arguments Arguments
99
Severity Severity
10+
Disabled bool
1011
}
1112

1213
// RulesConfig defines the config for all rules.
@@ -25,6 +26,7 @@ type Config struct {
2526
IgnoreGeneratedHeader bool `toml:"ignoreGeneratedHeader"`
2627
Confidence float64
2728
Severity Severity
29+
EnableAllRules bool `toml:"enableAllRules"`
2830
Rules RulesConfig `toml:"rule"`
2931
ErrorCode int `toml:"errorCode"`
3032
WarningCode int `toml:"warningCode"`

rule/cyclomatic.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ type CyclomaticRule struct{}
1717
func (r *CyclomaticRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
1818
var failures []lint.Failure
1919

20+
if len(arguments) == 0 {
21+
panic("not enough arguments for " + r.Name())
22+
}
2023
complexity, ok := arguments[0].(int64) // Alt. non panicking version
2124
if !ok {
2225
panic("invalid argument for cyclomatic complexity")

rule/max-public-structs.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ type MaxPublicStructsRule struct{}
1414
// Apply applies the rule to given file.
1515
func (r *MaxPublicStructsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
1616
var failures []lint.Failure
17+
if len(arguments) == 0 {
18+
panic("not enough arguments for " + r.Name())
19+
}
1720

1821
fileAst := file.AST
1922
walker := &lintMaxPublicStructs{

0 commit comments

Comments
 (0)