|
1 | 1 | package gci
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "bytes" |
5 |
| - "fmt" |
6 |
| - "io" |
7 |
| - "os" |
8 |
| - |
9 |
| - gcicfg "github.com/daixiang0/gci/pkg/config" |
10 |
| - "github.com/daixiang0/gci/pkg/gci" |
11 |
| - "github.com/daixiang0/gci/pkg/log" |
12 |
| - "github.com/shazow/go-diff/difflib" |
13 | 4 | "golang.org/x/tools/go/analysis"
|
14 | 5 |
|
15 | 6 | "github.com/golangci/golangci-lint/pkg/config"
|
16 | 7 | "github.com/golangci/golangci-lint/pkg/goanalysis"
|
| 8 | + "github.com/golangci/golangci-lint/pkg/goformatters" |
| 9 | + gcibase "github.com/golangci/golangci-lint/pkg/goformatters/gci" |
17 | 10 | "github.com/golangci/golangci-lint/pkg/golinters/internal"
|
18 |
| - "github.com/golangci/golangci-lint/pkg/lint/linter" |
19 | 11 | )
|
20 | 12 |
|
21 | 13 | const linterName = "gci"
|
22 | 14 |
|
23 |
| -type differ interface { |
24 |
| - Diff(out io.Writer, a io.ReadSeeker, b io.ReadSeeker) error |
25 |
| -} |
26 |
| - |
27 | 15 | func New(settings *config.GciSettings) *goanalysis.Linter {
|
28 |
| - log.InitLogger() |
29 |
| - _ = log.L().Sync() |
30 |
| - |
31 |
| - diff := difflib.New() |
32 |
| - |
33 |
| - a := &analysis.Analyzer{ |
34 |
| - Name: linterName, |
35 |
| - Doc: goanalysis.TheOnlyanalyzerDoc, |
36 |
| - Run: goanalysis.DummyRun, |
| 16 | + formatter, err := gcibase.New(settings) |
| 17 | + if err != nil { |
| 18 | + internal.LinterLogger.Fatalf("%s: create analyzer: %v", linterName, err) |
37 | 19 | }
|
38 | 20 |
|
| 21 | + a := goformatters.NewAnalyzer( |
| 22 | + internal.LinterLogger.Child(linterName), |
| 23 | + "Checks if code and import statements are formatted, with additional rules.", |
| 24 | + formatter, |
| 25 | + ) |
| 26 | + |
39 | 27 | return goanalysis.NewLinter(
|
40 |
| - linterName, |
41 |
| - "Checks if code and import statements are formatted, it makes import statements always deterministic.", |
| 28 | + a.Name, |
| 29 | + a.Doc, |
42 | 30 | []*analysis.Analyzer{a},
|
43 | 31 | nil,
|
44 |
| - ).WithContextSetter(func(lintCtx *linter.Context) { |
45 |
| - a.Run = func(pass *analysis.Pass) (any, error) { |
46 |
| - err := run(lintCtx, pass, settings, diff) |
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, settings *config.GciSettings, diff differ) error { |
57 |
| - cfg := gcicfg.YamlConfig{ |
58 |
| - Cfg: gcicfg.BoolConfig{ |
59 |
| - NoInlineComments: settings.NoInlineComments, |
60 |
| - NoPrefixComments: settings.NoPrefixComments, |
61 |
| - SkipGenerated: settings.SkipGenerated, |
62 |
| - CustomOrder: settings.CustomOrder, |
63 |
| - NoLexOrder: settings.NoLexOrder, |
64 |
| - }, |
65 |
| - SectionStrings: settings.Sections, |
66 |
| - ModPath: pass.Module.Path, |
67 |
| - } |
68 |
| - |
69 |
| - if settings.LocalPrefixes != "" { |
70 |
| - cfg.SectionStrings = []string{ |
71 |
| - "standard", |
72 |
| - "default", |
73 |
| - fmt.Sprintf("prefix(%s)", settings.LocalPrefixes), |
74 |
| - } |
75 |
| - } |
76 |
| - |
77 |
| - parsedCfg, err := cfg.Parse() |
78 |
| - if err != nil { |
79 |
| - return err |
80 |
| - } |
81 |
| - |
82 |
| - for _, file := range pass.Files { |
83 |
| - position, isGoFile := goanalysis.GetGoFilePosition(pass, file) |
84 |
| - if !isGoFile { |
85 |
| - continue |
86 |
| - } |
87 |
| - |
88 |
| - input, err := os.ReadFile(position.Filename) |
89 |
| - if err != nil { |
90 |
| - return fmt.Errorf("unable to open file %s: %w", position.Filename, err) |
91 |
| - } |
92 |
| - |
93 |
| - _, output, err := gci.LoadFormat(input, position.Filename, *parsedCfg) |
94 |
| - if err != nil { |
95 |
| - return fmt.Errorf("error while running gci: %w", err) |
96 |
| - } |
97 |
| - |
98 |
| - if !bytes.Equal(input, output) { |
99 |
| - out := bytes.NewBufferString(fmt.Sprintf("--- %[1]s\n+++ %[1]s\n", position.Filename)) |
100 |
| - |
101 |
| - err := diff.Diff(out, bytes.NewReader(input), bytes.NewReader(output)) |
102 |
| - if err != nil { |
103 |
| - return fmt.Errorf("error while running gci: %w", err) |
104 |
| - } |
105 |
| - |
106 |
| - diff := out.String() |
107 |
| - |
108 |
| - err = internal.ExtractDiagnosticFromPatch(pass, file, diff, lintCtx) |
109 |
| - if err != nil { |
110 |
| - return fmt.Errorf("can't extract issues from gci diff output %q: %w", diff, err) |
111 |
| - } |
112 |
| - } |
113 |
| - } |
114 |
| - |
115 |
| - return nil |
| 32 | + ).WithLoadMode(goanalysis.LoadModeSyntax) |
116 | 33 | }
|
0 commit comments