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

Basic configuration

This commit is contained in:
mgechev 2018-01-26 20:20:49 -08:00
parent 0b310191f2
commit 9f437c09a5
4 changed files with 69 additions and 16 deletions

12
config.toml Normal file
View File

@ -0,0 +1,12 @@
severity = "error"
confidence = 1.0
[rules]
[rules.else]
arguments = []
severity = "error"
[rules.argument-limit]
arguments = ['2']
severity = "warning"
[rules.names]
arguments = []
severity = "error"

View File

@ -51,8 +51,8 @@ type Arguments = []string
// RuleConfig is type used for the rule configuration.
type RuleConfig struct {
Arguments Arguments
Severity Severity
Arguments Arguments `toml:"arguments"`
Severity Severity `toml:"severity"`
}
// RulesConfig defines the config for all rules.
@ -60,9 +60,9 @@ type RulesConfig = map[string]RuleConfig
// Config defines the config of the linter.
type Config struct {
Confidence float64
Severity Severity
Rules RulesConfig
Confidence float64 `toml:"confidence"`
Severity Severity `toml:"severity"`
Rules RulesConfig `toml:"rules"`
}
// Rule defines an abstract rule interaface

61
main.go
View File

@ -2,13 +2,38 @@ package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/BurntSushi/toml"
"github.com/mgechev/revive/formatter"
"github.com/mgechev/revive/lint"
"github.com/mgechev/revive/rule"
)
var allRules = []lint.Rule{
&rule.ArgumentsLimitRule{},
&rule.VarDeclarationsRule{},
&rule.PackageCommentsRule{},
&rule.DotImportsRule{},
&rule.BlankImportsRule{},
&rule.ExportedRule{},
&rule.NamesRule{},
&rule.ElseRule{},
&rule.IfReturnRule{},
&rule.RangeRule{},
&rule.ErrorfRule{},
&rule.ErrorsRule{},
&rule.ErrorStringsRule{},
&rule.ReceiverNameRule{},
&rule.IncrementDecrementRule{},
&rule.ErrorReturnRule{},
&rule.UnexportedReturnRule{},
&rule.TimeNamesRule{},
&rule.ContextKeyTypeRule{},
&rule.ContextArgumentsRule{},
}
func main() {
src := `
package p
@ -28,17 +53,33 @@ func main() {
revive := lint.New(func(file string) ([]byte, error) {
return []byte(src), nil
})
var result []lint.Rule
result = append(result, &rule.ElseRule{}, &rule.ArgumentsLimitRule{}, &rule.NamesRule{})
var config = lint.RulesConfig{
"argument-limit": lint.RuleConfig{
Arguments: []string{"3"},
Severity: lint.SeverityError,
},
config := &lint.Config{}
file, err := ioutil.ReadFile("config.toml")
if err != nil {
panic("cannot read the config file")
}
_, err = toml.Decode(string(file), config)
if err != nil {
panic("cannot parse the config file: " + err.Error())
}
failures, err := revive.Lint([]string{"foo.go", "bar.go", "baz.go"}, result, config)
rulesMap := map[string]lint.Rule{}
for _, r := range allRules {
rulesMap[r.Name()] = r
}
lintingRules := []lint.Rule{}
for name := range config.Rules {
rule, ok := rulesMap[name]
if !ok {
panic("cannot find rule: " + name)
}
lintingRules = append(lintingRules, rule)
}
failures, err := revive.Lint([]string{"foo.go", "bar.go", "baz.go"}, lintingRules, config.Rules)
if err != nil {
panic(err)
}
@ -49,7 +90,7 @@ func main() {
go (func() {
var formatter formatter.CLIFormatter
output, err = formatter.Format(formatChan, config)
output, err = formatter.Format(formatChan, config.Rules)
if err != nil {
panic(err)
}
@ -61,7 +102,7 @@ func main() {
if exitCode == 0 {
exitCode = 1
}
if c, ok := config[f.RuleName]; ok && c.Severity == lint.SeverityError {
if c, ok := config.Rules[f.RuleName]; ok && c.Severity == lint.SeverityError {
exitCode = 2
}
formatChan <- f

View File

@ -47,7 +47,7 @@ func (r *NamesRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Fail
// Name returns the rule name.
func (r *NamesRule) Name() string {
return "imports"
return "names"
}
func checkList(fl *ast.FieldList, thing string, w *lintNames) {