|
1 | 1 | package gofumpt
|
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 |
| - "mvdan.cc/gofumpt/format" |
13 | 5 |
|
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 | + gofumptbase "github.com/golangci/golangci-lint/pkg/goformatters/gofumpt" |
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 = "gofumpt"
|
21 | 14 |
|
22 | 15 | 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 | + ) |
38 | 21 |
|
39 | 22 | 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}, |
43 | 26 | 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) |
96 | 28 | }
|
0 commit comments