|
1 | 1 | package gci
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "fmt"
|
5 |
| - "strings" |
| 6 | + "io" |
| 7 | + "os" |
6 | 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" |
7 | 13 | "golang.org/x/tools/go/analysis"
|
8 | 14 |
|
9 | 15 | "github.com/golangci/golangci-lint/pkg/config"
|
10 | 16 | "github.com/golangci/golangci-lint/pkg/goanalysis"
|
11 |
| - "github.com/golangci/golangci-lint/pkg/golinters/gci/internal" |
| 17 | + "github.com/golangci/golangci-lint/pkg/golinters/internal" |
| 18 | + "github.com/golangci/golangci-lint/pkg/lint/linter" |
12 | 19 | )
|
13 | 20 |
|
14 | 21 | const linterName = "gci"
|
15 | 22 |
|
16 |
| -const prefixSeparator = "¤" |
| 23 | +type differ interface { |
| 24 | + Diff(out io.Writer, a io.ReadSeeker, b io.ReadSeeker) error |
| 25 | +} |
17 | 26 |
|
18 | 27 | 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 |
| 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, |
| 37 | + } |
| 38 | + |
| 39 | + return goanalysis.NewLinter( |
| 40 | + linterName, |
| 41 | + a.Doc, |
| 42 | + []*analysis.Analyzer{a}, |
| 43 | + 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 |
28 | 49 | }
|
29 | 50 |
|
30 |
| - sections = append(sections, section) |
| 51 | + return nil, nil |
31 | 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 | + } |
32 | 68 |
|
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 |
| - }, |
| 69 | + if settings.LocalPrefixes != "" { |
| 70 | + cfg.SectionStrings = []string{ |
| 71 | + "standard", |
| 72 | + "default", |
| 73 | + fmt.Sprintf("prefix(%s)", settings.LocalPrefixes), |
43 | 74 | }
|
| 75 | + } |
| 76 | + |
| 77 | + parsedCfg, err := cfg.Parse() |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
44 | 81 |
|
45 |
| - if settings.LocalPrefixes != "" { |
46 |
| - prefix := []string{ |
47 |
| - "standard", |
48 |
| - "default", |
49 |
| - fmt.Sprintf("prefix(%s)", strings.Join(strings.Split(settings.LocalPrefixes, ","), prefixSeparator)), |
| 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) |
50 | 111 | }
|
51 |
| - cfg[a.Name][internal.SectionsFlag] = prefix |
52 | 112 | }
|
53 | 113 | }
|
54 | 114 |
|
55 |
| - return goanalysis.NewLinter( |
56 |
| - linterName, |
57 |
| - a.Doc, |
58 |
| - []*analysis.Analyzer{a}, |
59 |
| - cfg, |
60 |
| - ).WithLoadMode(goanalysis.LoadModeSyntax) |
| 115 | + return nil |
61 | 116 | }
|
0 commit comments