Skip to content

feat: improve formatter messages #5243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion pkg/golinters/gci/testdata/gci.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//golangcitest:config_path testdata/gci.yml
package testdata

// want +1 "Invalid import order"
// want +1 "File is not properly formatted"
import (
"golang.org/x/tools/go/analysis"
"github.com/golangci/golangci-lint/pkg/config"
Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/gci/testdata/gci_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package testdata
*/
import "C"

// want +1 "Invalid import order"
// want +1 "File is not properly formatted"
import (
"golang.org/x/tools/go/analysis"
"github.com/golangci/golangci-lint/pkg/config"
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
}
2 changes: 1 addition & 1 deletion pkg/golinters/gofmt/testdata/gofmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import "fmt"

func GofmtNotSimplified() {
var x []string
fmt.Print(x[1:len(x)]) // want "File is not `gofmt`-ed with `-s`"
fmt.Print(x[1:len(x)]) // want "File is not properly formatted"
}
2 changes: 1 addition & 1 deletion pkg/golinters/gofmt/testdata/gofmt_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ func _() {

func GofmtNotSimplified() {
var x []string
fmt.Print(x[1:len(x)]) // want "File is not `gofmt`-ed with `-s`"
fmt.Print(x[1:len(x)]) // want "File is not properly formatted"
}
2 changes: 1 addition & 1 deletion pkg/golinters/gofmt/testdata/gofmt_no_simplify.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ func GofmtNotSimplifiedOk() {
fmt.Print(x[1:len(x)])
}

func GofmtBadFormat(){ // want "^File is not `gofmt`-ed"
func GofmtBadFormat(){ // want "File is not properly formatted"
}
2 changes: 1 addition & 1 deletion pkg/golinters/gofmt/testdata/gofmt_rewrite_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func GofmtRewriteRule() {
vals = append(vals, 2)
vals = append(vals, 3)

slice := vals[1:len(vals)] // want "^File is not `gofmt`-ed"
slice := vals[1:len(vals)] // want "File is not properly formatted"

fmt.Println(slice)
}
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
}
2 changes: 1 addition & 1 deletion pkg/golinters/gofumpt/testdata/gofumpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ package testdata
import "fmt"

func GofumptNewLine() {
fmt.Println( "foo" ) // want "File is not `gofumpt`-ed"
fmt.Println( "foo" ) // want "File is not properly formatted"
}
2 changes: 1 addition & 1 deletion pkg/golinters/gofumpt/testdata/gofumpt_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ func _() {
}

func GofumptNewLine() {
fmt.Println( "foo" ) // want "File is not `gofumpt`-ed"
fmt.Println( "foo" ) // want "File is not properly formatted"
}
2 changes: 1 addition & 1 deletion pkg/golinters/gofumpt/testdata/gofumpt_with_extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ package testdata

import "fmt"

func GofmtNotExtra(bar string, baz string) { // want "File is not `gofumpt`-ed with `-extra`"
func GofmtNotExtra(bar string, baz string) { // want "File is not properly formatted"
fmt.Print("foo")
}
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
}
2 changes: 1 addition & 1 deletion pkg/golinters/goimports/testdata/goimports.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package testdata

import (
"fmt" // want "File is not `goimports`-ed"
"fmt" // want "File is not properly formatted"
"github.com/golangci/golangci-lint/pkg/config"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/goimports/testdata/goimports_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import "C"

import (
"fmt"
"unsafe" // want "File is not `goimports`-ed"
"unsafe" // want "File is not properly formatted"
"github.com/golangci/golangci-lint/pkg/config"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/goimports/testdata/goimports_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package testdata
import (
"fmt"

"github.com/golangci/golangci-lint/pkg/config" // want "File is not `goimports`-ed with -local github.com/golangci/golangci-lint"
"github.com/golangci/golangci-lint/pkg/config" // want "File is not properly formatted"
"golang.org/x/tools/go/analysis"
)

Expand Down
10 changes: 3 additions & 7 deletions pkg/golinters/internal/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
diffpkg "github.com/sourcegraph/go-diff/diff"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/goanalysis"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/logutils"
Expand All @@ -30,8 +29,6 @@ const (
diffLineDeleted diffLineType = "deleted"
)

type fmtTextFormatter func(settings *config.LintersSettings) string

type diffLine struct {
originalNumber int // 1-based original line number
typ diffLineType
Expand Down Expand Up @@ -219,7 +216,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 +242,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
6 changes: 3 additions & 3 deletions test/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func TestCgoWithIssues(t *testing.T) {
desc: "gofmt",
args: []string{"--no-config", "--disable-all", "-Egofmt"},
dir: "cgo_with_issues",
expected: "File is not `gofmt`-ed with `-s` (gofmt)",
expected: "File is not properly formatted (gofmt)",
},
{
desc: "revive",
Expand Down Expand Up @@ -186,7 +186,7 @@ func TestLineDirective(t *testing.T) {
"--disable-all",
},
targetPath: "linedirective",
expected: "File is not `gofmt`-ed with `-s` (gofmt)",
expected: "File is not properly formatted (gofmt)",
},
{
desc: "goimports",
Expand All @@ -195,7 +195,7 @@ func TestLineDirective(t *testing.T) {
"--disable-all",
},
targetPath: "linedirective",
expected: "File is not `goimports`-ed (goimports)",
expected: "File is not properly formatted (goimports)",
},
{
desc: "gomodguard",
Expand Down
Loading