Skip to content

Commit 59b16cc

Browse files
committed
feat: improve formatter messages
1 parent e11de60 commit 59b16cc

File tree

5 files changed

+7
-43
lines changed

5 files changed

+7
-43
lines changed

pkg/golinters/gci/internal/analyzer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func runAnalysis(pass *analysis.Pass) (any, error) {
108108

109109
pass.Report(analysis.Diagnostic{
110110
Pos: fix.TextEdits[0].Pos,
111-
Message: "Invalid import order",
111+
Message: "File is not properly formatted",
112112
SuggestedFixes: []analysis.SuggestedFix{*fix},
113113
})
114114
}

pkg/golinters/gofmt/gofmt.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,11 @@ func runGofmt(lintCtx *linter.Context, pass *analysis.Pass, settings *config.GoF
5656
continue
5757
}
5858

59-
err = internal.ExtractDiagnosticFromPatch(pass, file, string(diff), lintCtx, getIssuedTextGoFmt)
59+
err = internal.ExtractDiagnosticFromPatch(pass, file, string(diff), lintCtx)
6060
if err != nil {
6161
return fmt.Errorf("can't extract issues from gofmt diff output %q: %w", string(diff), err)
6262
}
6363
}
6464

6565
return nil
6666
}
67-
68-
func getIssuedTextGoFmt(settings *config.LintersSettings) string {
69-
text := "File is not `gofmt`-ed"
70-
if settings.Gofmt.Simplify {
71-
text += " with `-s`"
72-
}
73-
for _, rule := range settings.Gofmt.RewriteRules {
74-
text += fmt.Sprintf(" `-r '%s -> %s'`", rule.Pattern, rule.Replacement)
75-
}
76-
77-
return text
78-
}

pkg/golinters/gofumpt/gofumpt.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func runGofumpt(lintCtx *linter.Context, pass *analysis.Pass, diff differ, optio
8383

8484
diff := out.String()
8585

86-
err = internal.ExtractDiagnosticFromPatch(pass, file, diff, lintCtx, getIssuedTextGoFumpt)
86+
err = internal.ExtractDiagnosticFromPatch(pass, file, diff, lintCtx)
8787
if err != nil {
8888
return fmt.Errorf("can't extract issues from gofumpt diff output %q: %w", diff, err)
8989
}
@@ -101,13 +101,3 @@ func getLangVersion(settings *config.GofumptSettings) string {
101101

102102
return "go" + strings.TrimPrefix(settings.LangVersion, "go")
103103
}
104-
105-
func getIssuedTextGoFumpt(settings *config.LintersSettings) string {
106-
text := "File is not `gofumpt`-ed"
107-
108-
if settings.Gofumpt.ExtraRules {
109-
text += " with `-extra`"
110-
}
111-
112-
return text
113-
}

pkg/golinters/goimports/goimports.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,11 @@ func runGoImports(lintCtx *linter.Context, pass *analysis.Pass) error {
5454
continue
5555
}
5656

57-
err = internal.ExtractDiagnosticFromPatch(pass, file, string(diff), lintCtx, getIssuedTextGoImports)
57+
err = internal.ExtractDiagnosticFromPatch(pass, file, string(diff), lintCtx)
5858
if err != nil {
5959
return fmt.Errorf("can't extract issues from gofmt diff output %q: %w", string(diff), err)
6060
}
6161
}
6262

6363
return nil
6464
}
65-
66-
func getIssuedTextGoImports(settings *config.LintersSettings) string {
67-
text := "File is not `goimports`-ed"
68-
69-
if settings.Goimports.LocalPrefixes != "" {
70-
text += " with -local " + settings.Goimports.LocalPrefixes
71-
}
72-
73-
return text
74-
}

pkg/golinters/internal/diff.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
diffpkg "github.com/sourcegraph/go-diff/diff"
1212
"golang.org/x/tools/go/analysis"
1313

14-
"github.com/golangci/golangci-lint/pkg/config"
1514
"github.com/golangci/golangci-lint/pkg/goanalysis"
1615
"github.com/golangci/golangci-lint/pkg/lint/linter"
1716
"github.com/golangci/golangci-lint/pkg/logutils"
@@ -30,8 +29,6 @@ const (
3029
diffLineDeleted diffLineType = "deleted"
3130
)
3231

33-
type fmtTextFormatter func(settings *config.LintersSettings) string
34-
3532
type diffLine struct {
3633
originalNumber int // 1-based original line number
3734
typ diffLineType
@@ -219,7 +216,6 @@ func ExtractDiagnosticFromPatch(
219216
file *ast.File,
220217
patch string,
221218
lintCtx *linter.Context,
222-
formatter fmtTextFormatter,
223219
) error {
224220
diffs, err := diffpkg.ParseMultiFileDiff([]byte(patch))
225221
if err != nil {
@@ -246,23 +242,23 @@ func ExtractDiagnosticFromPatch(
246242
changes := p.parse(hunk)
247243

248244
for _, change := range changes {
249-
pass.Report(toDiagnostic(ft, change, formatter(lintCtx.Settings()), adjLine))
245+
pass.Report(toDiagnostic(ft, change, adjLine))
250246
}
251247
}
252248
}
253249

254250
return nil
255251
}
256252

257-
func toDiagnostic(ft *token.File, change Change, message string, adjLine int) analysis.Diagnostic {
253+
func toDiagnostic(ft *token.File, change Change, adjLine int) analysis.Diagnostic {
258254
start := ft.LineStart(change.From + adjLine)
259255

260256
end := goanalysis.EndOfLinePos(ft, change.To+adjLine)
261257

262258
return analysis.Diagnostic{
263259
Pos: start,
264260
End: end,
265-
Message: message, // TODO(ldez) change message formatter to have a better message.
261+
Message: "File is not properly formatted",
266262
SuggestedFixes: []analysis.SuggestedFix{{
267263
TextEdits: []analysis.TextEdit{{
268264
Pos: start,

0 commit comments

Comments
 (0)