|
1 | 1 | package golinters |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bytes" |
5 | 4 | "fmt" |
6 | | - "sync" |
| 5 | + "strings" |
7 | 6 |
|
8 | | - "github.com/daixiang0/gci/pkg/gci" |
9 | | - "github.com/pkg/errors" |
10 | | - "github.com/shazow/go-diff/difflib" |
| 7 | + gci "github.com/daixiang0/gci/pkg/analyzer" |
11 | 8 | "golang.org/x/tools/go/analysis" |
12 | 9 |
|
| 10 | + "github.com/golangci/golangci-lint/pkg/config" |
13 | 11 | "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" |
14 | 12 | "github.com/golangci/golangci-lint/pkg/lint/linter" |
15 | 13 | ) |
16 | 14 |
|
17 | 15 | const gciName = "gci" |
18 | 16 |
|
19 | | -func NewGci() *goanalysis.Linter { |
20 | | - var mu sync.Mutex |
21 | | - var resIssues []goanalysis.Issue |
22 | | - differ := difflib.New() |
| 17 | +func NewGci(settings *config.GciSettings) *goanalysis.Linter { |
| 18 | + var linterCfg map[string]map[string]interface{} |
23 | 19 |
|
24 | | - analyzer := &analysis.Analyzer{ |
25 | | - Name: gciName, |
26 | | - Doc: goanalysis.TheOnlyanalyzerDoc, |
27 | | - } |
28 | | - return goanalysis.NewLinter( |
29 | | - gciName, |
30 | | - "Gci control golang package import order and make it always deterministic.", |
31 | | - []*analysis.Analyzer{analyzer}, |
32 | | - nil, |
33 | | - ).WithContextSetter(func(lintCtx *linter.Context) { |
34 | | - localFlag := lintCtx.Settings().Gci.LocalPrefixes |
35 | | - goimportsFlag := lintCtx.Settings().Goimports.LocalPrefixes |
36 | | - if localFlag == "" && goimportsFlag != "" { |
37 | | - localFlag = goimportsFlag |
| 20 | + if settings != nil { |
| 21 | + cfg := map[string]interface{}{ |
| 22 | + gci.NoInlineCommentsFlag: settings.NoInlineComments, |
| 23 | + gci.NoPrefixCommentsFlag: settings.NoPrefixComments, |
| 24 | + gci.SectionsFlag: strings.Join(settings.Sections, gci.SectionDelimiter), |
| 25 | + gci.SectionSeparatorsFlag: strings.Join(settings.SectionSeparator, gci.SectionDelimiter), |
38 | 26 | } |
39 | 27 |
|
40 | | - analyzer.Run = func(pass *analysis.Pass) (interface{}, error) { |
41 | | - var fileNames []string |
42 | | - for _, f := range pass.Files { |
43 | | - pos := pass.Fset.PositionFor(f.Pos(), false) |
44 | | - fileNames = append(fileNames, pos.Filename) |
45 | | - } |
46 | | - |
47 | | - var issues []goanalysis.Issue |
48 | | - |
49 | | - flagSet := gci.FlagSet{ |
50 | | - LocalFlag: gci.ParseLocalFlag(localFlag), |
51 | | - } |
52 | | - |
53 | | - for _, f := range fileNames { |
54 | | - source, result, err := gci.Run(f, &flagSet) |
55 | | - if err != nil { |
56 | | - return nil, err |
57 | | - } |
58 | | - if result == nil { |
59 | | - continue |
60 | | - } |
61 | | - |
62 | | - diff := bytes.Buffer{} |
63 | | - _, err = diff.WriteString(fmt.Sprintf("--- %[1]s\n+++ %[1]s\n", f)) |
64 | | - if err != nil { |
65 | | - return nil, fmt.Errorf("can't write diff header: %v", err) |
66 | | - } |
67 | | - |
68 | | - err = differ.Diff(&diff, bytes.NewReader(source), bytes.NewReader(result)) |
69 | | - if err != nil { |
70 | | - return nil, fmt.Errorf("can't get gci diff output: %v", err) |
71 | | - } |
72 | | - |
73 | | - is, err := extractIssuesFromPatch(diff.String(), lintCtx.Log, lintCtx, gciName) |
74 | | - if err != nil { |
75 | | - return nil, errors.Wrapf(err, "can't extract issues from gci diff output %q", diff.String()) |
76 | | - } |
77 | | - |
78 | | - for i := range is { |
79 | | - issues = append(issues, goanalysis.NewIssue(&is[i], pass)) |
80 | | - } |
81 | | - } |
82 | | - |
83 | | - if len(issues) == 0 { |
84 | | - return nil, nil |
85 | | - } |
| 28 | + if settings.LocalPrefixes != "" { |
| 29 | + prefix := []string{"standard", "default", fmt.Sprintf("prefix(%s)", settings.LocalPrefixes)} |
| 30 | + cfg[gci.SectionsFlag] = strings.Join(prefix, gci.SectionDelimiter) |
| 31 | + } |
86 | 32 |
|
87 | | - mu.Lock() |
88 | | - resIssues = append(resIssues, issues...) |
89 | | - mu.Unlock() |
| 33 | + linterCfg = map[string]map[string]interface{}{ |
| 34 | + gci.Analyzer.Name: cfg, |
| 35 | + } |
| 36 | + } |
90 | 37 |
|
91 | | - return nil, nil |
| 38 | + return goanalysis.NewLinter( |
| 39 | + gciName, |
| 40 | + "Gci controls golang package import order and makes it always deterministic.", |
| 41 | + []*analysis.Analyzer{gci.Analyzer}, |
| 42 | + linterCfg, |
| 43 | + ).WithContextSetter(func(lintCtx *linter.Context) { |
| 44 | + if settings.LocalPrefixes != "" { |
| 45 | + lintCtx.Log.Warnf("gci: `local-prefixes` is deprecated, use `sections` and `prefix(%s)` instead.", settings.LocalPrefixes) |
92 | 46 | } |
93 | | - }).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { |
94 | | - return resIssues |
95 | 47 | }).WithLoadMode(goanalysis.LoadModeSyntax) |
96 | 48 | } |
0 commit comments