1
0
mirror of https://github.com/mgechev/revive.git synced 2025-11-25 22:12:38 +02:00

refactor: tiny refactoring on friendly, sarif and stylish formatters (#1355)

* refactor: replace function call by var ref and move def near utilization in friendly and stylish formatters

* refactor: makes reduces visibility of Sarif.AddResult function by renaming it as addResult

* fix: stylish formatter doesn't handle singular/plural in the synthesis message
This commit is contained in:
chavacava
2025-05-18 10:52:49 +02:00
committed by GitHub
parent f3f77bb387
commit e16f5aa5a6
4 changed files with 26 additions and 22 deletions

View File

@@ -117,7 +117,7 @@ test.go
(2, 5) https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#rule test failure (2, 5) https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#rule test failure
✖ 1 problem (0 errors) (1 warnings) ✖ 1 problem (0 errors) (1 warning)
`, `,
}, },
{ {

View File

@@ -13,14 +13,6 @@ import (
"github.com/mgechev/revive/lint" "github.com/mgechev/revive/lint"
) )
func getErrorEmoji() string {
return color.RedString("✘")
}
func getWarningEmoji() string {
return color.YellowString("⚠")
}
// Friendly is an implementation of the Formatter interface // Friendly is an implementation of the Formatter interface
// which formats the errors to JSON. // which formats the errors to JSON.
type Friendly struct { type Friendly struct {
@@ -64,10 +56,13 @@ func (f *Friendly) printFriendlyFailure(sb *strings.Builder, failure lint.Failur
sb.WriteString("\n\n") sb.WriteString("\n\n")
} }
var errorEmoji = color.RedString("✘")
var warningEmoji = color.YellowString("⚠")
func (*Friendly) printHeaderRow(sb *strings.Builder, failure lint.Failure, severity lint.Severity) { func (*Friendly) printHeaderRow(sb *strings.Builder, failure lint.Failure, severity lint.Severity) {
emoji := getWarningEmoji() emoji := warningEmoji
if severity == lint.SeverityError { if severity == lint.SeverityError {
emoji = getErrorEmoji() emoji = errorEmoji
} }
sb.WriteString(table([][]string{{emoji, ruleDescriptionURL(failure.RuleName), color.GreenString(failure.Failure)}})) sb.WriteString(table([][]string{{emoji, ruleDescriptionURL(failure.RuleName), color.GreenString(failure.Failure)}}))
} }
@@ -82,9 +77,9 @@ type statEntry struct {
} }
func (*Friendly) printSummary(w io.Writer, errors, warnings int) { func (*Friendly) printSummary(w io.Writer, errors, warnings int) {
emoji := getWarningEmoji() emoji := warningEmoji
if errors > 0 { if errors > 0 {
emoji = getErrorEmoji() emoji = errorEmoji
} }
problemsLabel := "problems" problemsLabel := "problems"
if errors+warnings == 1 { if errors+warnings == 1 {

View File

@@ -27,7 +27,7 @@ func (*Sarif) Format(failures <-chan lint.Failure, cfg lint.Config) (string, err
sarifLog := newReviveRunLog(cfg) sarifLog := newReviveRunLog(cfg)
for failure := range failures { for failure := range failures {
sarifLog.AddResult(failure) sarifLog.addResult(failure)
} }
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
@@ -72,7 +72,7 @@ func (l *reviveRunLog) addRules(cfg map[string]lint.RuleConfig) {
} }
} }
func (l *reviveRunLog) AddResult(failure lint.Failure) { func (l *reviveRunLog) addResult(failure lint.Failure) {
positiveOrZero := func(x int) int { positiveOrZero := func(x int) int {
if x > 0 { if x > 0 {
return x return x

View File

@@ -20,10 +20,10 @@ func (*Stylish) Name() string {
func formatFailure(failure lint.Failure, severity lint.Severity) []string { func formatFailure(failure lint.Failure, severity lint.Severity) []string {
fString := color.CyanString(failure.Failure) fString := color.CyanString(failure.Failure)
fURL := ruleDescriptionURL(failure.RuleName)
fName := color.RedString(fURL)
lineColumn := failure.Position lineColumn := failure.Position
pos := fmt.Sprintf("(%d, %d)", lineColumn.Start.Line, lineColumn.Start.Column) pos := fmt.Sprintf("(%d, %d)", lineColumn.Start.Line, lineColumn.Start.Column)
fURL := ruleDescriptionURL(failure.RuleName)
fName := color.RedString(fURL)
if severity == lint.SeverityWarning { if severity == lint.SeverityWarning {
fName = color.YellowString(fURL) fName = color.YellowString(fURL)
} }
@@ -44,10 +44,6 @@ func (*Stylish) Format(failures <-chan lint.Failure, config lint.Config) (string
} }
result = append(result, formatFailure(f, lint.Severity(currentType))) result = append(result, formatFailure(f, lint.Severity(currentType)))
} }
ps := "problems"
if total == 1 {
ps = "problem"
}
fileReport := map[string][][]string{} fileReport := map[string][][]string{}
@@ -66,7 +62,20 @@ func (*Stylish) Format(failures <-chan lint.Failure, config lint.Config) (string
output += table(val) + "\n" output += table(val) + "\n"
} }
suffix := fmt.Sprintf(" %d %s (%d errors) (%d warnings)", total, ps, totalErrors, total-totalErrors) problemsLabel := "problems"
if total == 1 {
problemsLabel = "problem"
}
totalWarnings := total - totalErrors
warningsLabel := "warnings"
if totalWarnings == 1 {
warningsLabel = "warning"
}
errorsLabel := "errors"
if totalErrors == 1 {
errorsLabel = "error"
}
suffix := fmt.Sprintf(" %d %s (%d %s) (%d %s)", total, problemsLabel, totalErrors, errorsLabel, totalWarnings, warningsLabel)
switch { switch {
case total > 0 && totalErrors > 0: case total > 0 && totalErrors > 0: