Skip to content

Commit ae0e109

Browse files
committed
chore: use github.com/rogpeppe/go-internal/diff
1 parent 4723dc1 commit ae0e109

File tree

6 files changed

+73
-49
lines changed

6 files changed

+73
-49
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ require (
4545
github.com/gofrs/flock v0.12.1
4646
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a
4747
github.com/golangci/go-printf-func-name v0.1.0
48-
github.com/golangci/gofmt v0.0.0-20241223200906-057b0627d9b9
48+
github.com/golangci/gofmt v0.0.0-20250106114630-d62b90e6713d
4949
github.com/golangci/misspell v0.6.0
5050
github.com/golangci/plugin-module-register v0.1.1
5151
github.com/golangci/revgrep v0.5.3

go.sum

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/gofmt/gofmt.go

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package gofmt
22

33
import (
4+
"bytes"
45
"fmt"
6+
"os"
7+
"path/filepath"
8+
"strings"
59

6-
gofmtAPI "github.com/golangci/gofmt/gofmt"
10+
"github.com/rogpeppe/go-internal/diff"
711
"golang.org/x/tools/go/analysis"
812

13+
"github.com/golangci/gofmt/gofmt"
914
"github.com/golangci/golangci-lint/pkg/config"
1015
"github.com/golangci/golangci-lint/pkg/goanalysis"
1116
"github.com/golangci/golangci-lint/pkg/golinters/internal"
@@ -21,14 +26,23 @@ func New(settings *config.GoFmtSettings) *goanalysis.Linter {
2126
Run: goanalysis.DummyRun,
2227
}
2328

29+
var options gofmt.Options
30+
if settings != nil {
31+
options = gofmt.Options{NeedSimplify: settings.Simplify}
32+
33+
for _, rule := range settings.RewriteRules {
34+
options.RewriteRules = append(options.RewriteRules, gofmt.RewriteRule(rule))
35+
}
36+
}
37+
2438
return goanalysis.NewLinter(
2539
linterName,
2640
"Checks if the code is formatted according to 'gofmt' command.",
2741
[]*analysis.Analyzer{analyzer},
2842
nil,
2943
).WithContextSetter(func(lintCtx *linter.Context) {
3044
analyzer.Run = func(pass *analysis.Pass) (any, error) {
31-
err := runGofmt(lintCtx, pass, settings)
45+
err := run(lintCtx, pass, options)
3246
if err != nil {
3347
return nil, err
3448
}
@@ -38,29 +52,37 @@ func New(settings *config.GoFmtSettings) *goanalysis.Linter {
3852
}).WithLoadMode(goanalysis.LoadModeSyntax)
3953
}
4054

41-
func runGofmt(lintCtx *linter.Context, pass *analysis.Pass, settings *config.GoFmtSettings) error {
42-
var rewriteRules []gofmtAPI.RewriteRule
43-
for _, rule := range settings.RewriteRules {
44-
rewriteRules = append(rewriteRules, gofmtAPI.RewriteRule(rule))
45-
}
46-
55+
func run(lintCtx *linter.Context, pass *analysis.Pass, options gofmt.Options) error {
4756
for _, file := range pass.Files {
4857
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
4958
if !isGoFile {
5059
continue
5160
}
5261

53-
diff, err := gofmtAPI.RunRewrite(position.Filename, settings.Simplify, rewriteRules)
54-
if err != nil { // TODO: skip
55-
return err
56-
}
57-
if diff == nil {
62+
if !strings.HasSuffix(position.Filename, ".go") {
5863
continue
5964
}
6065

61-
err = internal.ExtractDiagnosticFromPatch(pass, file, string(diff), lintCtx)
66+
input, err := os.ReadFile(position.Filename)
67+
if err != nil {
68+
return fmt.Errorf("unable to open file %s: %w", position.Filename, err)
69+
}
70+
71+
output, err := gofmt.Source(position.Filename, input, options)
6272
if err != nil {
63-
return fmt.Errorf("can't extract issues from gofmt diff output %q: %w", string(diff), err)
73+
return fmt.Errorf("error while running goimports: %w", err)
74+
}
75+
76+
if !bytes.Equal(input, output) {
77+
newName := filepath.ToSlash(position.Filename)
78+
oldName := newName + ".orig"
79+
80+
theDiff := diff.Diff(oldName, input, newName, output)
81+
82+
err = internal.ExtractDiagnosticFromPatch(pass, file, string(theDiff), lintCtx)
83+
if err != nil {
84+
return fmt.Errorf("can't extract issues from goimports diff output %q: %w", string(theDiff), err)
85+
}
6486
}
6587
}
6688

pkg/golinters/gofumpt/gofumpt.go

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package gofumpt
33
import (
44
"bytes"
55
"fmt"
6-
"io"
76
"os"
7+
"path/filepath"
88
"strings"
99

10-
"github.com/shazow/go-diff/difflib"
10+
"github.com/rogpeppe/go-internal/diff"
1111
"golang.org/x/tools/go/analysis"
1212
"mvdan.cc/gofumpt/format"
1313

@@ -19,13 +19,7 @@ import (
1919

2020
const linterName = "gofumpt"
2121

22-
type differ interface {
23-
Diff(out io.Writer, a io.ReadSeeker, b io.ReadSeeker) error
24-
}
25-
2622
func New(settings *config.GofumptSettings) *goanalysis.Linter {
27-
diff := difflib.New()
28-
2923
var options format.Options
3024

3125
if settings != nil {
@@ -49,7 +43,7 @@ func New(settings *config.GofumptSettings) *goanalysis.Linter {
4943
nil,
5044
).WithContextSetter(func(lintCtx *linter.Context) {
5145
analyzer.Run = func(pass *analysis.Pass) (any, error) {
52-
err := runGofumpt(lintCtx, pass, diff, options)
46+
err := run(lintCtx, pass, options)
5347
if err != nil {
5448
return nil, err
5549
}
@@ -59,7 +53,7 @@ func New(settings *config.GofumptSettings) *goanalysis.Linter {
5953
}).WithLoadMode(goanalysis.LoadModeSyntax)
6054
}
6155

62-
func runGofumpt(lintCtx *linter.Context, pass *analysis.Pass, diff differ, options format.Options) error {
56+
func run(lintCtx *linter.Context, pass *analysis.Pass, options format.Options) error {
6357
for _, file := range pass.Files {
6458
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
6559
if !isGoFile {
@@ -77,18 +71,14 @@ func runGofumpt(lintCtx *linter.Context, pass *analysis.Pass, diff differ, optio
7771
}
7872

7973
if !bytes.Equal(input, output) {
80-
out := bytes.NewBufferString(fmt.Sprintf("--- %[1]s\n+++ %[1]s\n", position.Filename))
81-
82-
err := diff.Diff(out, bytes.NewReader(input), bytes.NewReader(output))
83-
if err != nil {
84-
return fmt.Errorf("error while running gofumpt: %w", err)
85-
}
74+
newName := filepath.ToSlash(position.Filename)
75+
oldName := newName + ".orig"
8676

87-
diff := out.String()
77+
theDiff := diff.Diff(oldName, input, newName, output)
8878

89-
err = internal.ExtractDiagnosticFromPatch(pass, file, diff, lintCtx)
79+
err = internal.ExtractDiagnosticFromPatch(pass, file, string(theDiff), lintCtx)
9080
if err != nil {
91-
return fmt.Errorf("can't extract issues from gofumpt diff output %q: %w", diff, err)
81+
return fmt.Errorf("can't extract issues from gofumpt diff output %q: %w", string(theDiff), err)
9282
}
9383
}
9484
}

pkg/golinters/gofumpt/testdata/gofumpt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ import "fmt"
55

66
func GofumptNewLine() {
77
fmt.Println( "foo" ) // want "File is not properly formatted"
8-
} // want "File is not properly formatted"
8+
}

pkg/golinters/goimports/goimports.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package goimports
22

33
import (
4+
"bytes"
45
"fmt"
6+
"os"
7+
"path/filepath"
58

6-
goimportsAPI "github.com/golangci/gofmt/goimports"
9+
"github.com/rogpeppe/go-internal/diff"
710
"golang.org/x/tools/go/analysis"
811
"golang.org/x/tools/imports"
912

@@ -31,7 +34,7 @@ func New(settings *config.GoImportsSettings) *goanalysis.Linter {
3134
imports.LocalPrefix = settings.LocalPrefixes
3235

3336
analyzer.Run = func(pass *analysis.Pass) (any, error) {
34-
err := runGoImports(lintCtx, pass)
37+
err := run(lintCtx, pass)
3538
if err != nil {
3639
return nil, err
3740
}
@@ -41,24 +44,33 @@ func New(settings *config.GoImportsSettings) *goanalysis.Linter {
4144
}).WithLoadMode(goanalysis.LoadModeSyntax)
4245
}
4346

44-
func runGoImports(lintCtx *linter.Context, pass *analysis.Pass) error {
47+
func run(lintCtx *linter.Context, pass *analysis.Pass) error {
4548
for _, file := range pass.Files {
4649
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
4750
if !isGoFile {
4851
continue
4952
}
5053

51-
diff, err := goimportsAPI.Run(position.Filename)
52-
if err != nil { // TODO: skip
53-
return err
54-
}
55-
if diff == nil {
56-
continue
54+
input, err := os.ReadFile(position.Filename)
55+
if err != nil {
56+
return fmt.Errorf("unable to open file %s: %w", position.Filename, err)
5757
}
5858

59-
err = internal.ExtractDiagnosticFromPatch(pass, file, string(diff), lintCtx)
59+
output, err := imports.Process(position.Filename, input, nil)
6060
if err != nil {
61-
return fmt.Errorf("can't extract issues from goimports diff output %q: %w", string(diff), err)
61+
return fmt.Errorf("error while running goimports: %w", err)
62+
}
63+
64+
if !bytes.Equal(input, output) {
65+
newName := filepath.ToSlash(position.Filename)
66+
oldName := newName + ".orig"
67+
68+
theDiff := diff.Diff(oldName, input, newName, output)
69+
70+
err = internal.ExtractDiagnosticFromPatch(pass, file, string(theDiff), lintCtx)
71+
if err != nil {
72+
return fmt.Errorf("can't extract issues from goimports diff output %q: %w", string(theDiff), err)
73+
}
6274
}
6375
}
6476

0 commit comments

Comments
 (0)