Skip to content

Commit 4ce726b

Browse files
committed
Introduce gci as new linter
Signed-off-by: Xiang Dai <long0dai@foxmail.com>
1 parent ba48f30 commit 4ce726b

File tree

7 files changed

+99
-0
lines changed

7 files changed

+99
-0
lines changed

.golangci.example.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ linters-settings:
109109
funlen:
110110
lines: 60
111111
statements: 40
112+
gci:
113+
# put imports beginning with prefix after 3rd-party packages;
114+
# only support one prefix
115+
local-prefixes: github.com/org/project
112116
gocognit:
113117
# minimal code complexity to report, 30 by default (but we recommend 10-20)
114118
min-complexity: 10

.golangci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ linters-settings:
1414
funlen:
1515
lines: 100
1616
statements: 50
17+
gci:
18+
local-prefixes: github.com/golangci/golangci-lint
1719
goconst:
1820
min-len: 2
1921
min-occurrences: 2

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7
4646
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
4747
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
4848
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
49+
github.com/daixiang0/gci v0.0.0-20200716011047-39508001b569 h1:kfPaCsRtqTiyT6WyAN2MeLCwqNWp34pkCNNrdh/nzOk=
4950
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5051
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
5152
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

pkg/config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ type Run struct {
155155
}
156156

157157
type LintersSettings struct {
158+
Gci struct {
159+
LocalPrefixes string `mapstructure:"local-prefixes"`
160+
}
158161
Govet GovetSettings
159162
Golint struct {
160163
MinConfidence float64 `mapstructure:"min-confidence"`

pkg/golinters/gci.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package golinters
2+
3+
import (
4+
"sync"
5+
6+
gcier "github.com/daixiang0/gci/pkg/analyzer"
7+
"github.com/daixiang0/gci/pkg/gci"
8+
"github.com/pkg/errors"
9+
"golang.org/x/tools/go/analysis"
10+
11+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
12+
"github.com/golangci/golangci-lint/pkg/lint/linter"
13+
)
14+
15+
func NewGci() *goanalysis.Linter {
16+
var mu sync.Mutex
17+
var resIssues []goanalysis.Issue
18+
19+
analyzer := gcier.Analyzer
20+
21+
return goanalysis.NewLinter(
22+
analyzer.Name,
23+
analyzer.Doc,
24+
analyzer.Requires,
25+
nil,
26+
).WithContextSetter(func(lintCtx *linter.Context) {
27+
localFlag := lintCtx.Settings().Gci.LocalPrefixes
28+
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
29+
var fileNames []string
30+
for _, f := range pass.Files {
31+
pos := pass.Fset.PositionFor(f.Pos(), false)
32+
fileNames = append(fileNames, pos.Filename)
33+
}
34+
35+
var issues []goanalysis.Issue
36+
for _, f := range fileNames {
37+
diff, err := gci.Run(f, &gci.FlagSet{LocalFlag: localFlag})
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
if diff == nil {
43+
continue
44+
}
45+
46+
is, err := extractIssuesFromPatch(string(diff), lintCtx.Log, lintCtx, analyzer.Name)
47+
if err != nil {
48+
return nil, errors.Wrapf(err, "can't extract issues from gci diff output %q", string(diff))
49+
}
50+
51+
for i := range is {
52+
issues = append(issues, goanalysis.NewIssue(&is[i], pass))
53+
}
54+
}
55+
56+
if len(issues) == 0 {
57+
return nil, nil
58+
}
59+
60+
mu.Lock()
61+
resIssues = append(resIssues, issues...)
62+
mu.Unlock()
63+
64+
return nil, nil
65+
}
66+
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
67+
return resIssues
68+
}).WithLoadMode(goanalysis.LoadModeSyntax)
69+
}

pkg/lint/lintersdb/manager.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
206206
WithPresets(linter.PresetStyle).
207207
WithLoadForGoAnalysis().
208208
WithURL("https://github.com/denis-tingajkin/go-header"),
209+
linter.NewConfig(golinters.NewGci()).
210+
WithLoadForGoAnalysis().
211+
WithURL("https://github.com/daixiang0/gci"),
209212
linter.NewConfig(golinters.NewMaligned()).
210213
WithLoadForGoAnalysis().
211214
WithPresets(linter.PresetPerformance).

test/testdata/gci.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//args: -Egoimports
2+
//config: linters-settings.goimports.local-prefixes=github.com/golangci/golangci-lint
3+
package goimports
4+
5+
import (
6+
"fmt"
7+
8+
"github.com/golangci/golangci-lint/pkg/config"
9+
10+
"github.com/pkg/errors"
11+
)
12+
13+
func GoimportsLocalTest() {
14+
fmt.Print("x")
15+
_ = config.Config{}
16+
_ = errors.New("")
17+
}

0 commit comments

Comments
 (0)