1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-24 08:32:22 +02:00
revive/main.go

143 lines
2.7 KiB
Go
Raw Normal View History

2017-07-04 18:37:19 +02:00
package main
import (
"fmt"
2018-01-27 06:20:49 +02:00
"io/ioutil"
2018-01-25 01:41:40 +02:00
"os"
2017-07-04 18:37:19 +02:00
2018-01-27 06:20:49 +02:00
"github.com/BurntSushi/toml"
2017-08-29 19:47:29 +02:00
"github.com/mgechev/revive/formatter"
2018-01-25 01:44:03 +02:00
"github.com/mgechev/revive/lint"
2017-08-29 19:47:29 +02:00
"github.com/mgechev/revive/rule"
2017-07-04 18:37:19 +02:00
)
2018-01-27 06:20:49 +02:00
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{},
2018-01-27 06:45:17 +02:00
&rule.CyclomaticRule{},
2018-01-27 06:20:49 +02:00
}
2018-01-27 06:25:22 +02:00
func getLintingRules(config *lint.Config) []lint.Rule {
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)
}
return lintingRules
}
func parseConfig(path string) *lint.Config {
config := &lint.Config{}
file, err := ioutil.ReadFile(path)
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())
}
return config
}
2018-01-27 06:34:20 +02:00
func normalizeConfig(config *lint.Config) {
severity := config.Severity
if severity != "" {
for k, v := range config.Rules {
if v.Severity == "" {
v.Severity = severity
}
config.Rules[k] = v
}
}
}
2017-08-28 01:57:16 +02:00
func main() {
2017-07-04 18:37:19 +02:00
src := `
2018-01-25 21:47:39 +02:00
package p
2017-08-28 01:57:16 +02:00
2018-01-25 21:47:39 +02:00
func Test() {
2018-01-27 06:45:17 +02:00
if true || bar && baz {
2018-01-25 21:47:39 +02:00
return 42;
} else {
return 23;
}
2017-11-20 04:23:01 +02:00
}
2018-01-25 01:41:40 +02:00
func foo_bar(a int, b int, c int, d int) {
2017-11-20 04:23:01 +02:00
return a + b + c;
2018-01-25 21:47:39 +02:00
}`
2017-07-04 18:37:19 +02:00
2018-01-25 01:44:03 +02:00
revive := lint.New(func(file string) ([]byte, error) {
2017-08-28 01:57:16 +02:00
return []byte(src), nil
})
2017-11-20 04:23:01 +02:00
2018-01-27 06:25:22 +02:00
config := parseConfig("config.toml")
2018-01-27 06:34:20 +02:00
normalizeConfig(config)
2018-01-27 06:25:22 +02:00
lintingRules := getLintingRules(config)
2017-08-28 01:57:16 +02:00
2018-01-27 06:20:49 +02:00
failures, err := revive.Lint([]string{"foo.go", "bar.go", "baz.go"}, lintingRules, config.Rules)
2017-07-04 18:37:19 +02:00
if err != nil {
panic(err)
}
2018-01-25 01:44:03 +02:00
formatChan := make(chan lint.Failure)
2018-01-25 01:41:40 +02:00
exitChan := make(chan bool)
2017-07-04 18:37:19 +02:00
2018-01-27 06:25:22 +02:00
var output string
2018-01-25 01:41:40 +02:00
go (func() {
var formatter formatter.CLIFormatter
2018-01-27 06:20:49 +02:00
output, err = formatter.Format(formatChan, config.Rules)
2018-01-25 01:41:40 +02:00
if err != nil {
panic(err)
}
exitChan <- true
})()
exitCode := 0
for f := range failures {
2018-01-27 06:34:20 +02:00
if f.Confidence < config.Confidence {
continue
}
2018-01-25 01:46:43 +02:00
if exitCode == 0 {
2018-01-25 01:41:40 +02:00
exitCode = 1
}
2018-01-27 06:20:49 +02:00
if c, ok := config.Rules[f.RuleName]; ok && c.Severity == lint.SeverityError {
2018-01-25 01:46:43 +02:00
exitCode = 2
}
2018-01-25 01:41:40 +02:00
formatChan <- f
}
2018-01-27 06:25:22 +02:00
2018-01-25 01:41:40 +02:00
close(formatChan)
<-exitChan
2017-08-28 01:57:16 +02:00
fmt.Println(output)
2018-01-25 01:41:40 +02:00
os.Exit(exitCode)
2017-07-04 18:37:19 +02:00
}