|
1 | 1 | package goimports
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "bytes" |
5 |
| - "fmt" |
6 |
| - "os" |
7 |
| - "path/filepath" |
8 |
| - |
9 |
| - "github.com/rogpeppe/go-internal/diff" |
10 | 4 | "golang.org/x/tools/go/analysis"
|
11 |
| - "golang.org/x/tools/imports" |
12 | 5 |
|
13 | 6 | "github.com/golangci/golangci-lint/pkg/config"
|
14 | 7 | "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" |
15 | 10 | "github.com/golangci/golangci-lint/pkg/golinters/internal"
|
16 |
| - "github.com/golangci/golangci-lint/pkg/lint/linter" |
17 | 11 | )
|
18 | 12 |
|
19 | 13 | const linterName = "goimports"
|
20 | 14 |
|
21 | 15 | 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 | + ) |
27 | 21 |
|
28 | 22 | 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}, |
32 | 26 | 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) |
78 | 28 | }
|
0 commit comments