mirror of
https://github.com/mgechev/revive.git
synced 2025-07-15 01:04:40 +02:00
Add default formatter
This commit is contained in:
10
config.go
10
config.go
@ -42,8 +42,9 @@ var allRules = append([]lint.Rule{
|
||||
}, defaultRules...)
|
||||
|
||||
var allFormatters = []lint.Formatter{
|
||||
&formatter.CLIFormatter{},
|
||||
&formatter.JSONFormatter{},
|
||||
&formatter.CLI{},
|
||||
&formatter.JSON{},
|
||||
&formatter.Default{},
|
||||
}
|
||||
|
||||
func getFormatters() map[string]lint.Formatter {
|
||||
@ -86,6 +87,9 @@ func parseConfig(path string) *lint.Config {
|
||||
}
|
||||
|
||||
func normalizeConfig(config *lint.Config) {
|
||||
if config.Confidence == 0 {
|
||||
config.Confidence = 0.8
|
||||
}
|
||||
severity := config.Severity
|
||||
if severity != "" {
|
||||
for k, v := range config.Rules {
|
||||
@ -108,7 +112,7 @@ func getConfig() *lint.Config {
|
||||
|
||||
func getFormatter() lint.Formatter {
|
||||
formatters := getFormatters()
|
||||
formatter := formatters["cli"]
|
||||
formatter := formatters["default"]
|
||||
if formatterName != "" {
|
||||
f, ok := formatters[formatterName]
|
||||
if !ok {
|
||||
|
21
config.toml
21
config.toml
@ -5,8 +5,19 @@ confidence = 0.8
|
||||
[rule.package-comments]
|
||||
[rule.else]
|
||||
[rule.names]
|
||||
[rule.argument-limit]
|
||||
arguments = [2]
|
||||
[rule.cyclomatic]
|
||||
severity = "error"
|
||||
arguments = [3]
|
||||
[rule.var-declaration]
|
||||
[rule.unexported-return]
|
||||
[rule.time-names]
|
||||
[rule.receiver-names]
|
||||
[rule.range]
|
||||
[rule.increment-decrement]
|
||||
[rule.if-return]
|
||||
[rule.exported]
|
||||
[rule.errors]
|
||||
[rule.errorf]
|
||||
[rule.error-strings]
|
||||
[rule.error-return]
|
||||
[rule.dot-imports]
|
||||
[rule.context-key-types]
|
||||
[rule.context-arguments]
|
||||
[rule.blank-imports]
|
23
defaults.toml
Normal file
23
defaults.toml
Normal file
@ -0,0 +1,23 @@
|
||||
ignore-generated-header = true
|
||||
severity = "warning"
|
||||
confidence = 0.8
|
||||
|
||||
[rule.package-comments]
|
||||
[rule.else]
|
||||
[rule.names]
|
||||
[rule.var-declaration]
|
||||
[rule.unexported-return]
|
||||
[rule.time-names]
|
||||
[rule.receiver-names]
|
||||
[rule.range]
|
||||
[rule.increment-decrement]
|
||||
[rule.if-return]
|
||||
[rule.exported]
|
||||
[rule.errors]
|
||||
[rule.errorf]
|
||||
[rule.error-strings]
|
||||
[rule.error-return]
|
||||
[rule.dot-imports]
|
||||
[rule.context-key-types]
|
||||
[rule.context-arguments]
|
||||
[rule.blank-imports]
|
@ -14,14 +14,14 @@ const (
|
||||
warningEmoji = ""
|
||||
)
|
||||
|
||||
// CLIFormatter is an implementation of the Formatter interface
|
||||
// CLI is an implementation of the Formatter interface
|
||||
// which formats the errors to JSON.
|
||||
type CLIFormatter struct {
|
||||
type CLI struct {
|
||||
Metadata lint.FormatterMetadata
|
||||
}
|
||||
|
||||
// Name returns the name of the formatter
|
||||
func (f *CLIFormatter) Name() string {
|
||||
func (f *CLI) Name() string {
|
||||
return "cli"
|
||||
}
|
||||
|
||||
@ -37,7 +37,7 @@ func formatFailure(failure lint.Failure, severity lint.Severity) []string {
|
||||
}
|
||||
|
||||
// Format formats the failures gotten from the lint.
|
||||
func (f *CLIFormatter) Format(failures <-chan lint.Failure, config lint.RulesConfig) (string, error) {
|
||||
func (f *CLI) Format(failures <-chan lint.Failure, config lint.RulesConfig) (string, error) {
|
||||
var result [][]string
|
||||
var totalErrors = 0
|
||||
var total = 0
|
||||
|
26
formatter/default.go
Normal file
26
formatter/default.go
Normal file
@ -0,0 +1,26 @@
|
||||
package formatter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/mgechev/revive/lint"
|
||||
)
|
||||
|
||||
// Default is an implementation of the Formatter interface
|
||||
// which formats the errors to JSON.
|
||||
type Default struct {
|
||||
Metadata lint.FormatterMetadata
|
||||
}
|
||||
|
||||
// Name returns the name of the formatter
|
||||
func (f *Default) Name() string {
|
||||
return "default"
|
||||
}
|
||||
|
||||
// Format formats the failures gotten from the lint.
|
||||
func (f *Default) Format(failures <-chan lint.Failure, config lint.RulesConfig) (string, error) {
|
||||
for failure := range failures {
|
||||
fmt.Printf("%v%v: %s\n", failure.GetFilename(), failure.Position.Start, failure.Failure)
|
||||
}
|
||||
return "", nil
|
||||
}
|
@ -6,19 +6,19 @@ import (
|
||||
"github.com/mgechev/revive/lint"
|
||||
)
|
||||
|
||||
// JSONFormatter is an implementation of the Formatter interface
|
||||
// JSON is an implementation of the Formatter interface
|
||||
// which formats the errors to JSON.
|
||||
type JSONFormatter struct {
|
||||
type JSON struct {
|
||||
Metadata lint.FormatterMetadata
|
||||
}
|
||||
|
||||
// Name returns the name of the formatter
|
||||
func (f *JSONFormatter) Name() string {
|
||||
func (f *JSON) Name() string {
|
||||
return "json"
|
||||
}
|
||||
|
||||
// Format formats the failures gotten from the lint.
|
||||
func (f *JSONFormatter) Format(failures <-chan lint.Failure, config lint.RulesConfig) (string, error) {
|
||||
func (f *JSON) Format(failures <-chan lint.Failure, config lint.RulesConfig) (string, error) {
|
||||
var slice []lint.Failure
|
||||
for failure := range failures {
|
||||
slice = append(slice, failure)
|
||||
|
11
lint/file.go
11
lint/file.go
@ -92,11 +92,12 @@ func (f *File) isMain() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (f *File) lint(rules []Rule, rulesConfig RulesConfig, failures chan Failure) {
|
||||
func (f *File) lint(rules []Rule, config Config, failures chan Failure) {
|
||||
rulesConfig := config.Rules
|
||||
disabledIntervals := f.disabledIntervals(rules)
|
||||
for _, currentRule := range rules {
|
||||
config := rulesConfig[currentRule.Name()]
|
||||
currentFailures := currentRule.Apply(f, config.Arguments)
|
||||
ruleConfig := rulesConfig[currentRule.Name()]
|
||||
currentFailures := currentRule.Apply(f, ruleConfig.Arguments)
|
||||
for idx, failure := range currentFailures {
|
||||
if failure.RuleName == "" {
|
||||
failure.RuleName = currentRule.Name()
|
||||
@ -108,7 +109,9 @@ func (f *File) lint(rules []Rule, rulesConfig RulesConfig, failures chan Failure
|
||||
}
|
||||
currentFailures = f.filterFailures(currentFailures, disabledIntervals)
|
||||
for _, failure := range currentFailures {
|
||||
failures <- failure
|
||||
if failure.Confidence >= config.Confidence {
|
||||
failures <- failure
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package lint
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"go/token"
|
||||
)
|
||||
|
||||
@ -43,13 +42,11 @@ func isGenerated(src []byte) bool {
|
||||
|
||||
// Lint lints a set of files with the specified rule.
|
||||
func (l *Linter) Lint(filenames []string, ruleSet []Rule, config Config) (<-chan Failure, error) {
|
||||
rulesConfig := config.Rules
|
||||
failures := make(chan Failure)
|
||||
pkg := &Package{
|
||||
fset: token.NewFileSet(),
|
||||
files: map[string]*File{},
|
||||
}
|
||||
var pkgName string
|
||||
for _, filename := range filenames {
|
||||
content, err := l.reader(filename)
|
||||
if err != nil {
|
||||
@ -64,17 +61,11 @@ func (l *Linter) Lint(filenames []string, ruleSet []Rule, config Config) (<-chan
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if pkgName == "" {
|
||||
pkgName = file.AST.Name.Name
|
||||
} else if file.AST.Name.Name != pkgName {
|
||||
return nil, fmt.Errorf("%s is in package %s, not %s", filename, file.AST.Name.Name, pkgName)
|
||||
}
|
||||
|
||||
pkg.files[filename] = file
|
||||
}
|
||||
|
||||
go (func() {
|
||||
pkg.lint(ruleSet, rulesConfig, failures)
|
||||
pkg.lint(ruleSet, config, failures)
|
||||
})()
|
||||
|
||||
return failures, nil
|
||||
|
@ -139,7 +139,7 @@ func receiverType(fn *ast.FuncDecl) string {
|
||||
return "invalid-type"
|
||||
}
|
||||
|
||||
func (p *Package) lint(rules []Rule, config RulesConfig, failures chan Failure) {
|
||||
func (p *Package) lint(rules []Rule, config Config, failures chan Failure) {
|
||||
if len(p.files) == 0 {
|
||||
close(failures)
|
||||
return
|
||||
|
@ -33,7 +33,7 @@ func (r *ErrorStringsRule) Apply(file *lint.File, arguments lint.Arguments) []li
|
||||
|
||||
// Name returns the rule name.
|
||||
func (r *ErrorStringsRule) Name() string {
|
||||
return "errorf"
|
||||
return "error-strings"
|
||||
}
|
||||
|
||||
type lintErrorStrings struct {
|
||||
|
@ -39,7 +39,7 @@ func (r *ExportedRule) Apply(file *lint.File, arguments lint.Arguments) []lint.F
|
||||
|
||||
// Name returns the rule name.
|
||||
func (r *ExportedRule) Name() string {
|
||||
return "imports"
|
||||
return "exported"
|
||||
}
|
||||
|
||||
type lintExported struct {
|
||||
|
@ -32,7 +32,7 @@ func (r *VarDeclarationsRule) Apply(file *lint.File, arguments lint.Arguments) [
|
||||
|
||||
// Name returns the rule name.
|
||||
func (r *VarDeclarationsRule) Name() string {
|
||||
return "blank-imports"
|
||||
return "var-declaration"
|
||||
}
|
||||
|
||||
type lintVarDeclarations struct {
|
||||
|
Reference in New Issue
Block a user