Skip to content

Commit ed20557

Browse files
build(deps): bump github.com/daixiang0/gci from 0.12.3 to 0.13.3 (#4522)
Co-authored-by: Fernandez Ludovic <[email protected]>
1 parent c5f13ba commit ed20557

File tree

5 files changed

+130
-22
lines changed

5 files changed

+130
-22
lines changed

.golangci.next.reference.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ linters-settings:
436436

437437
# Section configuration to compare against.
438438
# Section names are case-insensitive and may contain parameters in ().
439-
# The default order of sections is `standard > default > custom > blank > dot > alias`,
439+
# The default order of sections is `standard > default > custom > blank > dot > alias > localmodule`,
440440
# If `custom-order` is `true`, it follows the order of `sections` option.
441441
# Default: ["standard", "default"]
442442
sections:
@@ -446,6 +446,7 @@ linters-settings:
446446
- blank # Blank section: contains all blank imports. This section is not present unless explicitly enabled.
447447
- dot # Dot section: contains all dot imports. This section is not present unless explicitly enabled.
448448
- alias # Alias section: contains all alias imports. This section is not present unless explicitly enabled.
449+
- localmodule # Local module section: contains all local packages. This section is not present unless explicitly enabled.
449450

450451
# Skip generated files.
451452
# Default: true

go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ require (
3131
github.com/charithe/durationcheck v0.0.10
3232
github.com/ckaznocha/intrange v0.1.1
3333
github.com/curioswitch/go-reassign v0.2.0
34-
github.com/daixiang0/gci v0.12.3
34+
github.com/daixiang0/gci v0.13.3
3535
github.com/denis-tingaikin/go-header v0.5.0
3636
github.com/fatih/color v1.16.0
3737
github.com/firefart/nonamedreturns v1.0.4
@@ -44,6 +44,7 @@ require (
4444
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a
4545
github.com/golangci/gofmt v0.0.0-20231018234816-f50ced29576e
4646
github.com/golangci/misspell v0.4.1
47+
github.com/golangci/modinfo v0.3.4
4748
github.com/golangci/plugin-module-register v0.1.1
4849
github.com/golangci/revgrep v0.5.2
4950
github.com/golangci/unconvert v0.0.0-20240309020433-c5143eacb3ed

go.sum

+4-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jsonschema/golangci.next.jsonschema.json

+15-1
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,21 @@
10201020
"description": "Section configuration to compare against.",
10211021
"type": "array",
10221022
"items": {
1023-
"type": "string"
1023+
"anyOf": [
1024+
{
1025+
"enum": [
1026+
"standard",
1027+
"default",
1028+
"blank",
1029+
"dot",
1030+
"alias",
1031+
"localmodule"
1032+
]
1033+
},
1034+
{
1035+
"type": "string"
1036+
}
1037+
]
10241038
},
10251039
"default": ["standard", "default"]
10261040
},

pkg/golinters/gci.go

+107-17
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ package golinters
22

33
import (
44
"fmt"
5+
"sort"
6+
"strings"
57
"sync"
68

79
gcicfg "github.com/daixiang0/gci/pkg/config"
810
"github.com/daixiang0/gci/pkg/gci"
911
"github.com/daixiang0/gci/pkg/io"
1012
"github.com/daixiang0/gci/pkg/log"
13+
"github.com/daixiang0/gci/pkg/section"
14+
"github.com/golangci/modinfo"
1115
"github.com/hexops/gotextdiff"
1216
"github.com/hexops/gotextdiff/myers"
1317
"github.com/hexops/gotextdiff/span"
@@ -29,6 +33,9 @@ func NewGci(settings *config.GciSettings) *goanalysis.Linter {
2933
Name: gciName,
3034
Doc: goanalysis.TheOnlyanalyzerDoc,
3135
Run: goanalysis.DummyRun,
36+
Requires: []*analysis.Analyzer{
37+
modinfo.Analyzer,
38+
},
3239
}
3340

3441
var cfg *gcicfg.Config
@@ -47,7 +54,7 @@ func NewGci(settings *config.GciSettings) *goanalysis.Linter {
4754
}
4855

4956
var err error
50-
cfg, err = rawCfg.Parse()
57+
cfg, err = YamlConfig{origin: rawCfg}.Parse()
5158
if err != nil {
5259
internal.LinterLogger.Fatalf("gci: configuration parsing: %v", err)
5360
}
@@ -62,6 +69,12 @@ func NewGci(settings *config.GciSettings) *goanalysis.Linter {
6269
nil,
6370
).WithContextSetter(func(lintCtx *linter.Context) {
6471
analyzer.Run = func(pass *analysis.Pass) (any, error) {
72+
var err error
73+
cfg.Sections, err = hackSectionList(pass, cfg)
74+
if err != nil {
75+
return nil, err
76+
}
77+
6578
issues, err := runGci(pass, lintCtx, cfg, &lock)
6679
if err != nil {
6780
return nil, err
@@ -111,6 +124,57 @@ func runGci(pass *analysis.Pass, lintCtx *linter.Context, cfg *gcicfg.Config, lo
111124
return issues, nil
112125
}
113126

127+
func getIssuedTextGci(settings *config.LintersSettings) string {
128+
text := "File is not `gci`-ed"
129+
130+
hasOptions := settings.Gci.SkipGenerated || len(settings.Gci.Sections) > 0
131+
if !hasOptions {
132+
return text
133+
}
134+
135+
text += " with"
136+
137+
if settings.Gci.SkipGenerated {
138+
text += " --skip-generated"
139+
}
140+
141+
if len(settings.Gci.Sections) > 0 {
142+
for _, sect := range settings.Gci.Sections {
143+
text += " -s " + sect
144+
}
145+
}
146+
147+
if settings.Gci.CustomOrder {
148+
text += " --custom-order"
149+
}
150+
151+
return text
152+
}
153+
154+
func hackSectionList(pass *analysis.Pass, cfg *gcicfg.Config) (section.SectionList, error) {
155+
var sections section.SectionList
156+
157+
for _, sect := range cfg.Sections {
158+
// local module hack
159+
if v, ok := sect.(*section.LocalModule); ok {
160+
info, err := modinfo.FindModuleFromPass(pass)
161+
if err != nil {
162+
return nil, err
163+
}
164+
165+
if info.Path == "" {
166+
continue
167+
}
168+
169+
v.Path = info.Path
170+
}
171+
172+
sections = append(sections, sect)
173+
}
174+
175+
return sections, nil
176+
}
177+
114178
// diffFormattedFilesToArray is a copy of gci.DiffFormattedFilesToArray without io.StdInGenerator.
115179
// gci.DiffFormattedFilesToArray uses gci.processStdInAndGoFilesInPaths that uses io.StdInGenerator but stdin is not active on CI.
116180
// https://github.com/daixiang0/gci/blob/6f5cb16718ba07f0342a58de9b830ec5a6d58790/pkg/gci/gci.go#L63-L75
@@ -130,29 +194,55 @@ func diffFormattedFilesToArray(paths []string, cfg gcicfg.Config, diffs *[]strin
130194
})
131195
}
132196

133-
func getIssuedTextGci(settings *config.LintersSettings) string {
134-
text := "File is not `gci`-ed"
197+
// Code bellow this comment is borrowed and modified from gci.
198+
// https://github.com/daixiang0/gci/blob/4725b0c101801e7449530eee2ddb0c72592e3405/pkg/config/config.go
199+
200+
var defaultOrder = map[string]int{
201+
section.StandardType: 0,
202+
section.DefaultType: 1,
203+
section.CustomType: 2,
204+
section.BlankType: 3,
205+
section.DotType: 4,
206+
section.AliasType: 5,
207+
section.LocalModuleType: 6,
208+
}
135209

136-
hasOptions := settings.Gci.SkipGenerated || len(settings.Gci.Sections) > 0
137-
if !hasOptions {
138-
return text
139-
}
210+
type YamlConfig struct {
211+
origin gcicfg.YamlConfig
212+
}
140213

141-
text += " with"
214+
//nolint:gocritic // code borrowed from gci and modified to fix LocalModule section behavior.
215+
func (g YamlConfig) Parse() (*gcicfg.Config, error) {
216+
var err error
142217

143-
if settings.Gci.SkipGenerated {
144-
text += " --skip-generated"
218+
sections, err := section.Parse(g.origin.SectionStrings)
219+
if err != nil {
220+
return nil, err
145221
}
146222

147-
if len(settings.Gci.Sections) > 0 {
148-
for _, section := range settings.Gci.Sections {
149-
text += " -s " + section
150-
}
223+
if sections == nil {
224+
sections = section.DefaultSections()
151225
}
152226

153-
if settings.Gci.CustomOrder {
154-
text += " --custom-order"
227+
// if default order sorted sections
228+
if !g.origin.Cfg.CustomOrder {
229+
sort.Slice(sections, func(i, j int) bool {
230+
sectionI, sectionJ := sections[i].Type(), sections[j].Type()
231+
232+
if strings.Compare(sectionI, sectionJ) == 0 {
233+
return strings.Compare(sections[i].String(), sections[j].String()) < 0
234+
}
235+
return defaultOrder[sectionI] < defaultOrder[sectionJ]
236+
})
155237
}
156238

157-
return text
239+
sectionSeparators, err := section.Parse(g.origin.SectionSeparatorStrings)
240+
if err != nil {
241+
return nil, err
242+
}
243+
if sectionSeparators == nil {
244+
sectionSeparators = section.DefaultSectionSeparators()
245+
}
246+
247+
return &gcicfg.Config{BoolConfig: g.origin.Cfg, Sections: sections, SectionSeparators: sectionSeparators}, nil
158248
}

0 commit comments

Comments
 (0)