Skip to content

Commit 5c0ac35

Browse files
committed
feat: migrate gofumpt
1 parent 214710a commit 5c0ac35

File tree

1 file changed

+11
-79
lines changed

1 file changed

+11
-79
lines changed

pkg/golinters/gofumpt/gofumpt.go

+11-79
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,28 @@
11
package gofumpt
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"
12-
"mvdan.cc/gofumpt/format"
135

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+
gofumptbase "github.com/golangci/golangci-lint/pkg/goformatters/gofumpt"
1610
"github.com/golangci/golangci-lint/pkg/golinters/internal"
17-
"github.com/golangci/golangci-lint/pkg/lint/linter"
1811
)
1912

2013
const linterName = "gofumpt"
2114

2215
func New(settings *config.GofumptSettings) *goanalysis.Linter {
23-
var options format.Options
24-
25-
if settings != nil {
26-
options = format.Options{
27-
LangVersion: getLangVersion(settings),
28-
ModulePath: settings.ModulePath,
29-
ExtraRules: settings.ExtraRules,
30-
}
31-
}
32-
33-
analyzer := &analysis.Analyzer{
34-
Name: linterName,
35-
Doc: goanalysis.TheOnlyanalyzerDoc,
36-
Run: goanalysis.DummyRun,
37-
}
16+
a := goformatters.NewAnalyzer(
17+
internal.LinterLogger.Child(linterName),
18+
"Checks if code and import statements are formatted, with additional rules.",
19+
gofumptbase.New(settings, settings.LangVersion),
20+
)
3821

3922
return goanalysis.NewLinter(
40-
linterName,
41-
"Checks if code and import statements are formatted, with additional rules.",
42-
[]*analysis.Analyzer{analyzer},
23+
a.Name,
24+
a.Doc,
25+
[]*analysis.Analyzer{a},
4326
nil,
44-
).WithContextSetter(func(lintCtx *linter.Context) {
45-
analyzer.Run = func(pass *analysis.Pass) (any, error) {
46-
err := run(lintCtx, pass, options)
47-
if err != nil {
48-
return nil, err
49-
}
50-
51-
return nil, nil
52-
}
53-
}).WithLoadMode(goanalysis.LoadModeSyntax)
54-
}
55-
56-
func run(lintCtx *linter.Context, pass *analysis.Pass, options format.Options) error {
57-
for _, file := range pass.Files {
58-
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
59-
if !isGoFile {
60-
continue
61-
}
62-
63-
input, err := os.ReadFile(position.Filename)
64-
if err != nil {
65-
return fmt.Errorf("unable to open file %s: %w", position.Filename, err)
66-
}
67-
68-
output, err := format.Source(input, options)
69-
if err != nil {
70-
return fmt.Errorf("error while running gofumpt: %w", err)
71-
}
72-
73-
if !bytes.Equal(input, output) {
74-
newName := filepath.ToSlash(position.Filename)
75-
oldName := newName + ".orig"
76-
77-
theDiff := diff.Diff(oldName, input, newName, output)
78-
79-
err = internal.ExtractDiagnosticFromPatch(pass, file, string(theDiff), lintCtx)
80-
if err != nil {
81-
return fmt.Errorf("can't extract issues from gofumpt diff output %q: %w", string(theDiff), err)
82-
}
83-
}
84-
}
85-
86-
return nil
87-
}
88-
89-
func getLangVersion(settings *config.GofumptSettings) string {
90-
if settings == nil || settings.LangVersion == "" {
91-
// TODO: defaults to "1.15", in the future (v2) must be removed.
92-
return "go1.15"
93-
}
94-
95-
return "go" + strings.TrimPrefix(settings.LangVersion, "go")
27+
).WithLoadMode(goanalysis.LoadModeSyntax)
9628
}

0 commit comments

Comments
 (0)