|
1 | 1 | package gci
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "fmt"
|
| 6 | + "io" |
| 7 | + "os" |
5 | 8 | "strings"
|
6 | 9 |
|
| 10 | + gcicfg "github.com/daixiang0/gci/pkg/config" |
| 11 | + "github.com/daixiang0/gci/pkg/gci" |
| 12 | + "github.com/daixiang0/gci/pkg/log" |
| 13 | + "github.com/shazow/go-diff/difflib" |
7 | 14 | "golang.org/x/tools/go/analysis"
|
8 | 15 |
|
9 | 16 | "github.com/golangci/golangci-lint/pkg/config"
|
10 | 17 | "github.com/golangci/golangci-lint/pkg/goanalysis"
|
11 |
| - "github.com/golangci/golangci-lint/pkg/golinters/gci/internal" |
| 18 | + "github.com/golangci/golangci-lint/pkg/golinters/internal" |
| 19 | + "github.com/golangci/golangci-lint/pkg/lint/linter" |
12 | 20 | )
|
13 | 21 |
|
14 | 22 | const linterName = "gci"
|
15 | 23 |
|
16 |
| -const prefixSeparator = "¤" |
| 24 | +type differ interface { |
| 25 | + Diff(out io.Writer, a io.ReadSeeker, b io.ReadSeeker) error |
| 26 | +} |
17 | 27 |
|
18 | 28 | func New(settings *config.GciSettings) *goanalysis.Linter {
|
19 |
| - a := internal.NewAnalyzer() |
20 |
| - |
21 |
| - var cfg map[string]map[string]any |
22 |
| - if settings != nil { |
23 |
| - var sections []string |
24 |
| - for _, section := range settings.Sections { |
25 |
| - if strings.HasPrefix(section, "prefix(") { |
26 |
| - sections = append(sections, strings.ReplaceAll(section, ",", prefixSeparator)) |
27 |
| - continue |
| 29 | + log.InitLogger() |
| 30 | + _ = log.L().Sync() |
| 31 | + |
| 32 | + diff := difflib.New() |
| 33 | + |
| 34 | + a := &analysis.Analyzer{ |
| 35 | + Name: linterName, |
| 36 | + Doc: goanalysis.TheOnlyanalyzerDoc, |
| 37 | + Run: goanalysis.DummyRun, |
| 38 | + } |
| 39 | + |
| 40 | + return goanalysis.NewLinter( |
| 41 | + linterName, |
| 42 | + a.Doc, |
| 43 | + []*analysis.Analyzer{a}, |
| 44 | + nil, |
| 45 | + ).WithContextSetter(func(lintCtx *linter.Context) { |
| 46 | + a.Run = func(pass *analysis.Pass) (any, error) { |
| 47 | + err := run(lintCtx, pass, settings, diff) |
| 48 | + if err != nil { |
| 49 | + return nil, err |
28 | 50 | }
|
29 | 51 |
|
30 |
| - sections = append(sections, section) |
| 52 | + return nil, nil |
31 | 53 | }
|
| 54 | + }).WithLoadMode(goanalysis.LoadModeSyntax) |
| 55 | +} |
| 56 | + |
| 57 | +func run(lintCtx *linter.Context, pass *analysis.Pass, settings *config.GciSettings, diff differ) error { |
| 58 | + cfg := gcicfg.YamlConfig{ |
| 59 | + Cfg: gcicfg.BoolConfig{ |
| 60 | + NoInlineComments: settings.NoInlineComments, |
| 61 | + NoPrefixComments: settings.NoPrefixComments, |
| 62 | + SkipGenerated: settings.SkipGenerated, |
| 63 | + CustomOrder: settings.CustomOrder, |
| 64 | + NoLexOrder: settings.NoLexOrder, |
| 65 | + }, |
| 66 | + SectionStrings: settings.Sections, |
| 67 | + ModPath: pass.Module.Path, |
| 68 | + } |
32 | 69 |
|
33 |
| - cfg = map[string]map[string]any{ |
34 |
| - a.Name: { |
35 |
| - internal.NoInlineCommentsFlag: settings.NoInlineComments, |
36 |
| - internal.NoPrefixCommentsFlag: settings.NoPrefixComments, |
37 |
| - internal.SkipGeneratedFlag: settings.SkipGenerated, |
38 |
| - internal.SectionsFlag: sections, // bug because prefix contains comas. |
39 |
| - internal.CustomOrderFlag: settings.CustomOrder, |
40 |
| - internal.NoLexOrderFlag: settings.NoLexOrder, |
41 |
| - internal.PrefixDelimiterFlag: prefixSeparator, |
42 |
| - }, |
| 70 | + if settings.LocalPrefixes != "" { |
| 71 | + cfg.SectionStrings = []string{ |
| 72 | + "standard", |
| 73 | + "default", |
| 74 | + fmt.Sprintf("prefix(%s)", settings.LocalPrefixes), |
43 | 75 | }
|
| 76 | + } |
| 77 | + |
| 78 | + parsedCfg, err := cfg.Parse() |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
44 | 82 |
|
45 |
| - if settings.LocalPrefixes != "" { |
46 |
| - prefix := []string{ |
47 |
| - "standard", |
48 |
| - "default", |
49 |
| - fmt.Sprintf("prefix(%s)", strings.Join(strings.Split(settings.LocalPrefixes, ","), prefixSeparator)), |
| 83 | + for _, file := range pass.Files { |
| 84 | + position := goanalysis.GetFilePosition(pass, file) |
| 85 | + |
| 86 | + if !strings.HasSuffix(position.Filename, ".go") { |
| 87 | + continue |
| 88 | + } |
| 89 | + |
| 90 | + input, err := os.ReadFile(position.Filename) |
| 91 | + if err != nil { |
| 92 | + return fmt.Errorf("unable to open file %s: %w", position.Filename, err) |
| 93 | + } |
| 94 | + |
| 95 | + _, output, err := gci.LoadFormat(input, position.Filename, *parsedCfg) |
| 96 | + |
| 97 | + if !bytes.Equal(input, output) { |
| 98 | + out := bytes.NewBufferString(fmt.Sprintf("--- %[1]s\n+++ %[1]s\n", position.Filename)) |
| 99 | + |
| 100 | + err := diff.Diff(out, bytes.NewReader(input), bytes.NewReader(output)) |
| 101 | + if err != nil { |
| 102 | + return fmt.Errorf("error while running gci: %w", err) |
| 103 | + } |
| 104 | + |
| 105 | + diff := out.String() |
| 106 | + |
| 107 | + err = internal.ExtractDiagnosticFromPatch(pass, file, diff, lintCtx) |
| 108 | + if err != nil { |
| 109 | + return fmt.Errorf("can't extract issues from gci diff output %q: %w", diff, err) |
50 | 110 | }
|
51 |
| - cfg[a.Name][internal.SectionsFlag] = prefix |
52 | 111 | }
|
53 | 112 | }
|
54 | 113 |
|
55 |
| - return goanalysis.NewLinter( |
56 |
| - linterName, |
57 |
| - a.Doc, |
58 |
| - []*analysis.Analyzer{a}, |
59 |
| - cfg, |
60 |
| - ).WithLoadMode(goanalysis.LoadModeSyntax) |
| 114 | + return nil |
61 | 115 | }
|
0 commit comments