From deb72d62385a998c4caf839bb8a2adeb5290d024 Mon Sep 17 00:00:00 2001 From: mgechev Date: Sat, 27 Jan 2018 17:01:18 -0800 Subject: [PATCH] Add default formatter --- config.go | 10 +++++++--- config.toml | 21 ++++++++++++++++----- defaults.toml | 23 +++++++++++++++++++++++ formatter/cli_formatter.go | 8 ++++---- formatter/default.go | 26 ++++++++++++++++++++++++++ formatter/json_formatter.go | 8 ++++---- lint/file.go | 11 +++++++---- lint/linter.go | 11 +---------- lint/package.go | 2 +- rule/error-strings.go | 2 +- rule/exported.go | 2 +- rule/var-declarations.go | 2 +- 12 files changed, 92 insertions(+), 34 deletions(-) create mode 100644 defaults.toml create mode 100644 formatter/default.go diff --git a/config.go b/config.go index 3f5bc74..83317b6 100644 --- a/config.go +++ b/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 { diff --git a/config.toml b/config.toml index 7a1ce47..553b533 100644 --- a/config.toml +++ b/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] \ No newline at end of file +[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] \ No newline at end of file diff --git a/defaults.toml b/defaults.toml new file mode 100644 index 0000000..553b533 --- /dev/null +++ b/defaults.toml @@ -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] \ No newline at end of file diff --git a/formatter/cli_formatter.go b/formatter/cli_formatter.go index eb4ed8a..c221e0f 100644 --- a/formatter/cli_formatter.go +++ b/formatter/cli_formatter.go @@ -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 diff --git a/formatter/default.go b/formatter/default.go new file mode 100644 index 0000000..e13872c --- /dev/null +++ b/formatter/default.go @@ -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 +} diff --git a/formatter/json_formatter.go b/formatter/json_formatter.go index af78f18..d24c4b8 100644 --- a/formatter/json_formatter.go +++ b/formatter/json_formatter.go @@ -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) diff --git a/lint/file.go b/lint/file.go index caa02ad..ee50dee 100644 --- a/lint/file.go +++ b/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 + } } } } diff --git a/lint/linter.go b/lint/linter.go index 5f79d72..2e6c855 100644 --- a/lint/linter.go +++ b/lint/linter.go @@ -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 diff --git a/lint/package.go b/lint/package.go index da0d431..73c9be2 100644 --- a/lint/package.go +++ b/lint/package.go @@ -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 diff --git a/rule/error-strings.go b/rule/error-strings.go index ae664b4..95cf39a 100644 --- a/rule/error-strings.go +++ b/rule/error-strings.go @@ -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 { diff --git a/rule/exported.go b/rule/exported.go index 880755e..bb95599 100644 --- a/rule/exported.go +++ b/rule/exported.go @@ -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 { diff --git a/rule/var-declarations.go b/rule/var-declarations.go index 29a336b..0d7a038 100644 --- a/rule/var-declarations.go +++ b/rule/var-declarations.go @@ -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 {