Skip to content

Commit 214710a

Browse files
committed
feat: migrate goimports
1 parent 18e6209 commit 214710a

File tree

1 file changed

+11
-61
lines changed

1 file changed

+11
-61
lines changed

pkg/golinters/goimports/goimports.go

+11-61
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,28 @@
11
package goimports
22

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

136
"github.com/golangci/golangci-lint/pkg/config"
147
"github.com/golangci/golangci-lint/pkg/goanalysis"
8+
"github.com/golangci/golangci-lint/pkg/goformatters"
9+
goimportsbase "github.com/golangci/golangci-lint/pkg/goformatters/goimports"
1510
"github.com/golangci/golangci-lint/pkg/golinters/internal"
16-
"github.com/golangci/golangci-lint/pkg/lint/linter"
1711
)
1812

1913
const linterName = "goimports"
2014

2115
func New(settings *config.GoImportsSettings) *goanalysis.Linter {
22-
analyzer := &analysis.Analyzer{
23-
Name: linterName,
24-
Doc: goanalysis.TheOnlyanalyzerDoc,
25-
Run: goanalysis.DummyRun,
26-
}
16+
a := goformatters.NewAnalyzer(
17+
internal.LinterLogger.Child(linterName),
18+
"Checks if the code and import statements are formatted according to the 'goimports' command.",
19+
goimportsbase.New(settings),
20+
)
2721

2822
return goanalysis.NewLinter(
29-
linterName,
30-
"Checks if the code and import statements are formatted according to the 'goimports' command.",
31-
[]*analysis.Analyzer{analyzer},
23+
a.Name,
24+
a.Doc,
25+
[]*analysis.Analyzer{a},
3226
nil,
33-
).WithContextSetter(func(lintCtx *linter.Context) {
34-
imports.LocalPrefix = settings.LocalPrefixes
35-
36-
analyzer.Run = func(pass *analysis.Pass) (any, error) {
37-
err := run(lintCtx, pass)
38-
if err != nil {
39-
return nil, err
40-
}
41-
42-
return nil, nil
43-
}
44-
}).WithLoadMode(goanalysis.LoadModeSyntax)
45-
}
46-
47-
func run(lintCtx *linter.Context, pass *analysis.Pass) error {
48-
for _, file := range pass.Files {
49-
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
50-
if !isGoFile {
51-
continue
52-
}
53-
54-
input, err := os.ReadFile(position.Filename)
55-
if err != nil {
56-
return fmt.Errorf("unable to open file %s: %w", position.Filename, err)
57-
}
58-
59-
output, err := imports.Process(position.Filename, input, nil)
60-
if err != nil {
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-
}
74-
}
75-
}
76-
77-
return nil
27+
).WithLoadMode(goanalysis.LoadModeSyntax)
7828
}

0 commit comments

Comments
 (0)