forked from golangci/golangci-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgci.go
More file actions
77 lines (63 loc) · 1.85 KB
/
gci.go
File metadata and controls
77 lines (63 loc) · 1.85 KB
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
package golinters
import (
"sync"
"github.com/daixiang0/gci/pkg/gci"
"github.com/pkg/errors"
"golang.org/x/tools/go/analysis"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
"github.com/golangci/golangci-lint/pkg/lint/linter"
)
const gciName = "gci"
func NewGci() *goanalysis.Linter {
var mu sync.Mutex
var resIssues []goanalysis.Issue
analyzer := &analysis.Analyzer{
Name: gciName,
Doc: goanalysis.TheOnlyanalyzerDoc,
}
return goanalysis.NewLinter(
gciName,
"Gci control golang package import order and make it always deterministic.",
[]*analysis.Analyzer{analyzer},
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
localFlag := lintCtx.Settings().Gci.LocalPrefixes
goimportsFlag := lintCtx.Settings().Goimports.LocalPrefixes
if localFlag == "" && goimportsFlag != "" {
localFlag = goimportsFlag
}
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
var fileNames []string
for _, f := range pass.Files {
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}
var issues []goanalysis.Issue
for _, f := range fileNames {
diff, err := gci.Run(f, &gci.FlagSet{LocalFlag: localFlag})
if err != nil {
return nil, err
}
if diff == nil {
continue
}
is, err := extractIssuesFromPatch(string(diff), lintCtx.Log, lintCtx, gciName)
if err != nil {
return nil, errors.Wrapf(err, "can't extract issues from gci diff output %q", string(diff))
}
for i := range is {
issues = append(issues, goanalysis.NewIssue(&is[i], pass))
}
}
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)
}