Skip to content

Commit 18e6209

Browse files
committed
feat: migrate gofmt
1 parent 8abbb1b commit 18e6209

File tree

1 file changed

+11
-73
lines changed

1 file changed

+11
-73
lines changed

pkg/golinters/gofmt/gofmt.go

Lines changed: 11 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,28 @@
11
package gofmt
22

33
import (
4-
"bytes"
5-
"fmt"
6-
"os"
7-
"path/filepath"
8-
"strings"
9-
10-
"github.com/rogpeppe/go-internal/diff"
114
"golang.org/x/tools/go/analysis"
125

13-
"github.com/golangci/gofmt/gofmt"
146
"github.com/golangci/golangci-lint/pkg/config"
157
"github.com/golangci/golangci-lint/pkg/goanalysis"
8+
"github.com/golangci/golangci-lint/pkg/goformatters"
9+
gofmtbase "github.com/golangci/golangci-lint/pkg/goformatters/gofmt"
1610
"github.com/golangci/golangci-lint/pkg/golinters/internal"
17-
"github.com/golangci/golangci-lint/pkg/lint/linter"
1811
)
1912

2013
const linterName = "gofmt"
2114

2215
func New(settings *config.GoFmtSettings) *goanalysis.Linter {
23-
analyzer := &analysis.Analyzer{
24-
Name: linterName,
25-
Doc: goanalysis.TheOnlyanalyzerDoc,
26-
Run: goanalysis.DummyRun,
27-
}
28-
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-
}
16+
a := goformatters.NewAnalyzer(
17+
internal.LinterLogger.Child(linterName),
18+
"Checks if the code is formatted according to 'gofmt' command.",
19+
gofmtbase.New(settings),
20+
)
3721

3822
return goanalysis.NewLinter(
39-
linterName,
40-
"Checks if the code is formatted according to 'gofmt' command.",
41-
[]*analysis.Analyzer{analyzer},
23+
a.Name,
24+
a.Doc,
25+
[]*analysis.Analyzer{a},
4226
nil,
43-
).WithContextSetter(func(lintCtx *linter.Context) {
44-
analyzer.Run = func(pass *analysis.Pass) (any, error) {
45-
err := run(lintCtx, pass, options)
46-
if err != nil {
47-
return nil, err
48-
}
49-
50-
return nil, nil
51-
}
52-
}).WithLoadMode(goanalysis.LoadModeSyntax)
53-
}
54-
55-
func run(lintCtx *linter.Context, pass *analysis.Pass, options gofmt.Options) error {
56-
for _, file := range pass.Files {
57-
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
58-
if !isGoFile {
59-
continue
60-
}
61-
62-
if !strings.HasSuffix(position.Filename, ".go") {
63-
continue
64-
}
65-
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)
72-
if err != nil {
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-
}
86-
}
87-
}
88-
89-
return nil
27+
).WithLoadMode(goanalysis.LoadModeSyntax)
9028
}

0 commit comments

Comments
 (0)