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