1
0
mirror of https://github.com/mgechev/revive.git synced 2025-01-08 03:13:27 +02:00

New formatter: checkstyle (#37)

* Adds rule superfluous-else (an extension of indent-error-flow)

* Fix superfluous-else rule struct namming.

* Adds superfuous-else rule to the rules table

* Adds confusing-naming rule

* adds multifile test

* [WIP] fix multiple file test

* draft solution for detecting confusing-names through multiple files

* [WIP] confusing-name multiple files

* clean-up

* draft working version

* cleaner version + more informative messages

* adds check on struct field names

* fix config.go

* clean master

* new ADS rule: newerr

* ADS-print working version

* ads-print final version

* ads-lost-err working version

* confusing-namming: fix tests

* removes ads-* rules

* Adds checkstyle formatter

* Update README.md

fix typo
This commit is contained in:
chavacava 2018-07-15 21:45:15 +02:00 committed by Minko Gechev
parent 7c17e85217
commit 439cef2893
3 changed files with 79 additions and 1 deletions

View File

@ -96,7 +96,8 @@ go get -u github.com/mgechev/revive
- `ndjson` - outputs the failures as stream in newline delimited JSON (NDJSON) format.
- `friendly` - outputs the failures when found. Shows summary of all the failures.
- `stylish` - formats the failures in a table. Keep in mind that it doesn't stream the output so it might be perceived as slower compared to others.
- `checkstyle` - outputs the failures in XML format compatible with that of Java's [Checkstyle](https://checkstyle.org/).
### Sample Invocations
```shell

View File

@ -63,6 +63,7 @@ var allFormatters = []lint.Formatter{
&formatter.JSON{},
&formatter.NDJSON{},
&formatter.Default{},
&formatter.Checkstyle{},
}
func getFormatters() map[string]lint.Formatter {

76
formatter/checkstyle.go Normal file
View File

@ -0,0 +1,76 @@
package formatter
import (
"bytes"
"encoding/xml"
"github.com/mgechev/revive/lint"
plainTemplate "text/template"
)
// Checkstyle is an implementation of the Formatter interface
// which formats the errors to Checkstyle-like format.
type Checkstyle struct {
Metadata lint.FormatterMetadata
}
// Name returns the name of the formatter
func (f *Checkstyle) Name() string {
return "checkstyle"
}
type issue struct {
Line int
Col int
What string
Confidence float64
Severity lint.Severity
RuleName string
}
// Format formats the failures gotten from the lint.
func (f *Checkstyle) Format(failures <-chan lint.Failure, config lint.RulesConfig) (string, error) {
var issues = map[string][]issue{}
for failure := range failures {
buf := new(bytes.Buffer)
xml.Escape(buf, []byte(failure.Failure))
what := buf.String()
iss := issue{
Line: failure.Position.Start.Line,
Col: failure.Position.Start.Column,
What: what,
Confidence: failure.Confidence,
Severity: severity(config, failure),
RuleName: failure.RuleName,
}
fn := failure.GetFilename()
if issues[fn] == nil {
issues[fn] = make([]issue, 0)
}
issues[fn] = append(issues[fn], iss)
}
t, err := plainTemplate.New("revive").Parse(checkstyleTemplate)
if err != nil {
return "", err
}
buf := new(bytes.Buffer)
err = t.Execute(buf, issues)
if err != nil {
return "", err
}
return buf.String(), nil
}
const checkstyleTemplate = `<?xml version='1.0' encoding='UTF-8'?>
<checkstyle version="5.0">
{{- range $k, $v := . }}
<file name="{{ $k }}">
{{- range $i, $issue := $v }}
<error line="{{ $issue.Line }}" column="{{ $issue.Col }}" message="{{ $issue.What }} (confidence {{ $issue.Confidence}})" severity="{{ $issue.Severity }}" source="revive/{{ $issue.RuleName }}"/>
{{- end }}
</file>
{{- end }}
</checkstyle>`