forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgci.go
158 lines (128 loc) · 4.19 KB
/
gci.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package golinters
import (
"fmt"
"sync"
gcicfg "github.com/daixiang0/gci/pkg/config"
"github.com/daixiang0/gci/pkg/gci"
"github.com/daixiang0/gci/pkg/io"
"github.com/daixiang0/gci/pkg/log"
"github.com/hexops/gotextdiff"
"github.com/hexops/gotextdiff/myers"
"github.com/hexops/gotextdiff/span"
"golang.org/x/tools/go/analysis"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/goanalysis"
"github.com/golangci/golangci-lint/pkg/golinters/internal"
"github.com/golangci/golangci-lint/pkg/lint/linter"
)
const gciName = "gci"
func NewGci(settings *config.GciSettings) *goanalysis.Linter {
var mu sync.Mutex
var resIssues []goanalysis.Issue
analyzer := &analysis.Analyzer{
Name: gciName,
Doc: goanalysis.TheOnlyanalyzerDoc,
Run: goanalysis.DummyRun,
}
var cfg *gcicfg.Config
if settings != nil {
rawCfg := gcicfg.YamlConfig{
Cfg: gcicfg.BoolConfig{
SkipGenerated: settings.SkipGenerated,
CustomOrder: settings.CustomOrder,
},
SectionStrings: settings.Sections,
}
if settings.LocalPrefixes != "" {
prefix := []string{"standard", "default", fmt.Sprintf("prefix(%s)", settings.LocalPrefixes)}
rawCfg.SectionStrings = prefix
}
var err error
cfg, err = rawCfg.Parse()
if err != nil {
internal.LinterLogger.Fatalf("gci: configuration parsing: %v", err)
}
}
var lock sync.Mutex
return goanalysis.NewLinter(
gciName,
"Gci controls Go package import order and makes it always deterministic.",
[]*analysis.Analyzer{analyzer},
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
analyzer.Run = func(pass *analysis.Pass) (any, error) {
issues, err := runGci(pass, lintCtx, cfg, &lock)
if err != nil {
return nil, err
}
if len(issues) == 0 {
return nil, nil
}
mu.Lock()
resIssues = append(resIssues, issues...)
mu.Unlock()
return nil, nil
}
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
return resIssues
}).WithLoadMode(goanalysis.LoadModeSyntax)
}
func runGci(pass *analysis.Pass, lintCtx *linter.Context, cfg *gcicfg.Config, lock *sync.Mutex) ([]goanalysis.Issue, error) {
fileNames := internal.GetFileNames(pass)
var diffs []string
err := diffFormattedFilesToArray(fileNames, *cfg, &diffs, lock)
if err != nil {
return nil, err
}
var issues []goanalysis.Issue
for _, diff := range diffs {
if diff == "" {
continue
}
is, err := internal.ExtractIssuesFromPatch(diff, lintCtx, gciName, getIssuedTextGci)
if err != nil {
return nil, fmt.Errorf("can't extract issues from gci diff output %s: %w", diff, err)
}
for i := range is {
issues = append(issues, goanalysis.NewIssue(&is[i], pass))
}
}
return issues, nil
}
// diffFormattedFilesToArray is a copy of gci.DiffFormattedFilesToArray without io.StdInGenerator.
// gci.DiffFormattedFilesToArray uses gci.processStdInAndGoFilesInPaths that uses io.StdInGenerator but stdin is not active on CI.
// https://github.com/daixiang0/gci/blob/6f5cb16718ba07f0342a58de9b830ec5a6d58790/pkg/gci/gci.go#L63-L75
// https://github.com/daixiang0/gci/blob/6f5cb16718ba07f0342a58de9b830ec5a6d58790/pkg/gci/gci.go#L80
func diffFormattedFilesToArray(paths []string, cfg gcicfg.Config, diffs *[]string, lock *sync.Mutex) error {
log.InitLogger()
defer func() { _ = log.L().Sync() }()
return gci.ProcessFiles(io.GoFilesInPathsGenerator(paths, true), cfg, func(filePath string, unmodifiedFile, formattedFile []byte) error {
fileURI := span.URIFromPath(filePath)
edits := myers.ComputeEdits(fileURI, string(unmodifiedFile), string(formattedFile))
unifiedEdits := gotextdiff.ToUnified(filePath, filePath, string(unmodifiedFile), edits)
lock.Lock()
*diffs = append(*diffs, fmt.Sprint(unifiedEdits))
lock.Unlock()
return nil
})
}
func getIssuedTextGci(settings *config.LintersSettings) string {
text := "File is not `gci`-ed"
hasOptions := settings.Gci.SkipGenerated || len(settings.Gci.Sections) > 0
if !hasOptions {
return text
}
text += " with"
if settings.Gci.SkipGenerated {
text += " --skip-generated"
}
if len(settings.Gci.Sections) > 0 {
for _, section := range settings.Gci.Sections {
text += " -s " + section
}
}
if settings.Gci.CustomOrder {
text += " --custom-order"
}
return text
}