Skip to content

Commit

Permalink
feat: improve formatter messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Dec 21, 2024
1 parent e11de60 commit 91ededf
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 40 deletions.
2 changes: 1 addition & 1 deletion pkg/golinters/gci/internal/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func runAnalysis(pass *analysis.Pass) (any, error) {

pass.Report(analysis.Diagnostic{
Pos: fix.TextEdits[0].Pos,
Message: "Invalid import order",
Message: "File is not properly formatted",
SuggestedFixes: []analysis.SuggestedFix{*fix},
})
}
Expand Down
14 changes: 1 addition & 13 deletions pkg/golinters/gofmt/gofmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,11 @@ func runGofmt(lintCtx *linter.Context, pass *analysis.Pass, settings *config.GoF
continue
}

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

return nil
}

func getIssuedTextGoFmt(settings *config.LintersSettings) string {
text := "File is not `gofmt`-ed"
if settings.Gofmt.Simplify {
text += " with `-s`"
}
for _, rule := range settings.Gofmt.RewriteRules {
text += fmt.Sprintf(" `-r '%s -> %s'`", rule.Pattern, rule.Replacement)
}

return text
}
12 changes: 1 addition & 11 deletions pkg/golinters/gofumpt/gofumpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func runGofumpt(lintCtx *linter.Context, pass *analysis.Pass, diff differ, optio

diff := out.String()

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

return "go" + strings.TrimPrefix(settings.LangVersion, "go")
}

func getIssuedTextGoFumpt(settings *config.LintersSettings) string {
text := "File is not `gofumpt`-ed"

if settings.Gofumpt.ExtraRules {
text += " with `-extra`"
}

return text
}
12 changes: 1 addition & 11 deletions pkg/golinters/goimports/goimports.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,11 @@ func runGoImports(lintCtx *linter.Context, pass *analysis.Pass) error {
continue
}

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

return nil
}

func getIssuedTextGoImports(settings *config.LintersSettings) string {
text := "File is not `goimports`-ed"

if settings.Goimports.LocalPrefixes != "" {
text += " with -local " + settings.Goimports.LocalPrefixes
}

return text
}
7 changes: 3 additions & 4 deletions pkg/golinters/internal/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ func ExtractDiagnosticFromPatch(
file *ast.File,
patch string,
lintCtx *linter.Context,
formatter fmtTextFormatter,
) error {
diffs, err := diffpkg.ParseMultiFileDiff([]byte(patch))
if err != nil {
Expand All @@ -246,23 +245,23 @@ func ExtractDiagnosticFromPatch(
changes := p.parse(hunk)

for _, change := range changes {
pass.Report(toDiagnostic(ft, change, formatter(lintCtx.Settings()), adjLine))
pass.Report(toDiagnostic(ft, change, adjLine))
}
}
}

return nil
}

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

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

return analysis.Diagnostic{
Pos: start,
End: end,
Message: message, // TODO(ldez) change message formatter to have a better message.
Message: "File is not properly formatted",
SuggestedFixes: []analysis.SuggestedFix{{
TextEdits: []analysis.TextEdit{{
Pos: start,
Expand Down

0 comments on commit 91ededf

Please # to comment.