Repository: mgechev/revive
Pull request #1130 (2024-11-15 18:44:01)

refactor: remove redundant fmt.Fprintln calls
The PR refactors "friendly" formatter printing functions.


Patch:
diff --git a/formatter/default.go b/formatter/default.go
index 2d5a044..7af4aad 100644
--- a/formatter/default.go
+++ b/formatter/default.go
@@ -26,3 +26,7 @@ func (*Default) Format(failures <-chan lint.Failure, _ lint.Config) (string, err
 	}
 	return buf.String(), nil
 }
+
+func ruleDescriptionURL(ruleName string) string {
+	return "https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#" + ruleName
+}
diff --git a/formatter/friendly.go b/formatter/friendly.go
index 5ff329a..bbbbcb6 100644
--- a/formatter/friendly.go
+++ b/formatter/friendly.go
@@ -58,8 +58,7 @@ func (f *Friendly) Format(failures <-chan lint.Failure, config lint.Config) (str
 func (f *Friendly) printFriendlyFailure(w io.Writer, failure lint.Failure, severity lint.Severity) {
 	f.printHeaderRow(w, failure, severity)
 	f.printFilePosition(w, failure)
-	fmt.Fprintln(w)
-	fmt.Fprintln(w)
+	fmt.Fprintf(w, "\n\n")
 }
 
 func (f *Friendly) printHeaderRow(w io.Writer, failure lint.Failure, severity lint.Severity) {
@@ -67,7 +66,7 @@ func (f *Friendly) printHeaderRow(w io.Writer, failure lint.Failure, severity li
 	if severity == lint.SeverityError {
 		emoji = getErrorEmoji()
 	}
-	fmt.Fprint(w, f.table([][]string{{emoji, "https://revive.run/r#" + failure.RuleName, color.GreenString(failure.Failure)}}))
+	fmt.Fprint(w, f.table([][]string{{emoji, ruleDescriptionURL(failure.RuleName), color.GreenString(failure.Failure)}}))
 }
 
 func (*Friendly) printFilePosition(w io.Writer, failure lint.Failure) {
@@ -98,13 +97,11 @@ func (*Friendly) printSummary(w io.Writer, errors, warnings int) {
 	}
 	str := fmt.Sprintf("%d %s (%d %s, %d %s)", errors+warnings, problemsLabel, errors, errorsLabel, warnings, warningsLabel)
 	if errors > 0 {
-		fmt.Fprintf(w, "%s %s\n", emoji, color.RedString(str))
-		fmt.Fprintln(w)
+		fmt.Fprintf(w, "%s %s\n\n", emoji, color.RedString(str))
 		return
 	}
 	if warnings > 0 {
-		fmt.Fprintf(w, "%s %s\n", emoji, color.YellowString(str))
-		fmt.Fprintln(w)
+		fmt.Fprintf(w, "%s %s\n\n", emoji, color.YellowString(str))
 		return
 	}
 }
diff --git a/formatter/plain.go b/formatter/plain.go
index 09ebf6c..c972432 100644
--- a/formatter/plain.go
+++ b/formatter/plain.go
@@ -22,7 +22,7 @@ func (*Plain) Name() string {
 func (*Plain) Format(failures <-chan lint.Failure, _ lint.Config) (string, error) {
 	var buf bytes.Buffer
 	for failure := range failures {
-		fmt.Fprintf(&buf, "%v: %s %s\n", failure.Position.Start, failure.Failure, "https://revive.run/r#"+failure.RuleName)
+		fmt.Fprintf(&buf, "%v: %s %s\n", failure.Position.Start, failure.Failure, ruleDescriptionURL(failure.RuleName))
 	}
 	return buf.String(), nil
 }
diff --git a/formatter/sarif.go b/formatter/sarif.go
index c42da73..72da160 100644
--- a/formatter/sarif.go
+++ b/formatter/sarif.go
@@ -20,7 +20,7 @@ func (*Sarif) Name() string {
 	return "sarif"
 }
 
-const reviveSite = "https://revive.run"
+const reviveSite = "https://github.com/mgechev/revive"
 
 // Format formats the failures gotten from the lint.
 func (*Sarif) Format(failures <-chan lint.Failure, cfg lint.Config) (string, error) {
diff --git a/formatter/stylish.go b/formatter/stylish.go
index 828228c..bd821a3 100644
--- a/formatter/stylish.go
+++ b/formatter/stylish.go
@@ -22,11 +22,12 @@ func (*Stylish) Name() string {
 
 func formatFailure(failure lint.Failure, severity lint.Severity) []string {
 	fString := color.CyanString(failure.Failure)
-	fName := color.RedString("https://revive.run/r#" + failure.RuleName)
+	fURL := ruleDescriptionURL(failure.RuleName)
+	fName := color.RedString(fURL)
 	lineColumn := failure.Position
 	pos := fmt.Sprintf("(%d, %d)", lineColumn.Start.Line, lineColumn.Start.Column)
 	if severity == lint.SeverityWarning {
-		fName = color.YellowString("https://revive.run/r#" + failure.RuleName)
+		fName = color.YellowString(fURL)
 	}
 	return []string{failure.GetFilename(), pos, fName, fString}
 }


Test patch:
diff --git a/formatter/formatter_test.go b/formatter/formatter_test.go
index e0d4e9d..89f46b1 100644
--- a/formatter/formatter_test.go
+++ b/formatter/formatter_test.go
@@ -50,7 +50,7 @@ func TestFormatter(t *testing.T) {
 		{
 			formatter: &formatter.Friendly{},
 			want: `
-⚠  https://revive.run/r#rule  test failure  
+⚠  https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#rule  test failure  
   test.go:2:5
 
 ⚠ 1 problem (0 errors, 1 warning)
@@ -69,7 +69,7 @@ Warnings:
 		},
 		{
 			formatter: &formatter.Plain{},
-			want:      `test.go:2:5: test failure https://revive.run/r#rule`,
+			want:      `test.go:2:5: test failure https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#rule`,
 		},
 		{
 			formatter: &formatter.Sarif{},
@@ -100,7 +100,7 @@ Warnings:
       ],
       "tool": {
         "driver": {
-          "informationUri": "https://revive.run",
+          "informationUri": "https://github.com/mgechev/revive",
           "name": "revive"
         }
       }
@@ -114,7 +114,7 @@ Warnings:
 			formatter: &formatter.Stylish{},
 			want: `
 test.go
-  (2, 5)  https://revive.run/r#rule  test failure  
+  (2, 5)  https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#rule  test failure  
 
 
  ✖ 1 problem (0 errors) (1 warnings)
diff --git a/revivelib/core_test.go b/revivelib/core_test.go
index 10670c7..60ed94b 100644
--- a/revivelib/core_test.go
+++ b/revivelib/core_test.go
@@ -54,11 +54,11 @@ func TestReviveFormat(t *testing.T) {
 	}
 
 	errorMsgs := []string{
-		"(91, 3)  https://revive.run/r#unreachable-code  unreachable code after this statement",
-		"(98, 3)  https://revive.run/r#unreachable-code  unreachable code after this statement",
-		"(15, 2)  https://revive.run/r#if-return         redundant if ...; err != nil check, just return error instead.",
-		"(88, 3)  https://revive.run/r#if-return         redundant if ...; err != nil check, just return error instead.",
-		"(95, 3)  https://revive.run/r#if-return         redundant if ...; err != nil check, just return error instead.",
+		"(91, 3)  https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#unreachable-code  unreachable code after this statement",
+		"(98, 3)  https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#unreachable-code  unreachable code after this statement",
+		"(15, 2)  https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#if-return         redundant if ...; err != nil check, just return error instead.",
+		"(88, 3)  https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#if-return         redundant if ...; err != nil check, just return error instead.",
+		"(95, 3)  https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#if-return         redundant if ...; err != nil check, just return error instead.",
 	}
 	for _, errorMsg := range errorMsgs {
 		if !strings.Contains(failures, errorMsg) {