Skip to content

Commit e6ed5a1

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

File tree

6 files changed

+94
-0
lines changed

6 files changed

+94
-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

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+
"github.com/daixiang0/gci/pkg/analyzer"
7+
"github.com/daixiang0/gci/pkg/gci"
8+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
9+
"github.com/golangci/golangci-lint/pkg/lint/linter"
10+
"github.com/pkg/errors"
11+
"golang.org/x/tools/go/analysis"
12+
"golang.org/x/tools/imports"
13+
)
14+
15+
func NewGci() *goanalysis.Linter {
16+
var mu sync.Mutex
17+
var resIssues []goanalysis.Issue
18+
19+
analyzer := analyzer.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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//args: -Egci
2+
//config: linters-settings.gci.local-prefixes=github.com/golangci/golangci-lint
3+
package testdata
4+
5+
import (
6+
"fmt" // ERROR "File is not `goimports`-ed"
7+
"github.com/golangci/golangci-lint/pkg/config"
8+
)
9+
10+
func Bar() {
11+
fmt.Print("x")
12+
_ = config.Config{}
13+
}

0 commit comments

Comments
 (0)