Skip to content

Commit

Permalink
build(deps): bump github.com/daixiang0/gci from 0.12.3 to 0.13.3 (#4522)
Browse files Browse the repository at this point in the history
Co-authored-by: Fernandez Ludovic <ldez@users.noreply.github.com>
  • Loading branch information
dependabot[bot] and ldez authored Mar 28, 2024
1 parent c5f13ba commit ed20557
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ linters-settings:

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

# Skip generated files.
# Default: true
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ require (
github.com/charithe/durationcheck v0.0.10
github.com/ckaznocha/intrange v0.1.1
github.com/curioswitch/go-reassign v0.2.0
github.com/daixiang0/gci v0.12.3
github.com/daixiang0/gci v0.13.3
github.com/denis-tingaikin/go-header v0.5.0
github.com/fatih/color v1.16.0
github.com/firefart/nonamedreturns v1.0.4
Expand All @@ -44,6 +44,7 @@ require (
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a
github.com/golangci/gofmt v0.0.0-20231018234816-f50ced29576e
github.com/golangci/misspell v0.4.1
github.com/golangci/modinfo v0.3.4
github.com/golangci/plugin-module-register v0.1.1
github.com/golangci/revgrep v0.5.2
github.com/golangci/unconvert v0.0.0-20240309020433-c5143eacb3ed
Expand Down
6 changes: 4 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion jsonschema/golangci.next.jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,21 @@
"description": "Section configuration to compare against.",
"type": "array",
"items": {
"type": "string"
"anyOf": [
{
"enum": [
"standard",
"default",
"blank",
"dot",
"alias",
"localmodule"
]
},
{
"type": "string"
}
]
},
"default": ["standard", "default"]
},
Expand Down
124 changes: 107 additions & 17 deletions pkg/golinters/gci.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package golinters

import (
"fmt"
"sort"
"strings"
"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/daixiang0/gci/pkg/section"
"github.com/golangci/modinfo"
"github.com/hexops/gotextdiff"
"github.com/hexops/gotextdiff/myers"
"github.com/hexops/gotextdiff/span"
Expand All @@ -29,6 +33,9 @@ func NewGci(settings *config.GciSettings) *goanalysis.Linter {
Name: gciName,
Doc: goanalysis.TheOnlyanalyzerDoc,
Run: goanalysis.DummyRun,
Requires: []*analysis.Analyzer{
modinfo.Analyzer,
},
}

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

var err error
cfg, err = rawCfg.Parse()
cfg, err = YamlConfig{origin: rawCfg}.Parse()
if err != nil {
internal.LinterLogger.Fatalf("gci: configuration parsing: %v", err)
}
Expand All @@ -62,6 +69,12 @@ func NewGci(settings *config.GciSettings) *goanalysis.Linter {
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
analyzer.Run = func(pass *analysis.Pass) (any, error) {
var err error
cfg.Sections, err = hackSectionList(pass, cfg)
if err != nil {
return nil, err
}

issues, err := runGci(pass, lintCtx, cfg, &lock)
if err != nil {
return nil, err
Expand Down Expand Up @@ -111,6 +124,57 @@ func runGci(pass *analysis.Pass, lintCtx *linter.Context, cfg *gcicfg.Config, lo
return issues, 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 _, sect := range settings.Gci.Sections {
text += " -s " + sect
}
}

if settings.Gci.CustomOrder {
text += " --custom-order"
}

return text
}

func hackSectionList(pass *analysis.Pass, cfg *gcicfg.Config) (section.SectionList, error) {
var sections section.SectionList

for _, sect := range cfg.Sections {
// local module hack
if v, ok := sect.(*section.LocalModule); ok {
info, err := modinfo.FindModuleFromPass(pass)
if err != nil {
return nil, err
}

if info.Path == "" {
continue
}

v.Path = info.Path
}

sections = append(sections, sect)
}

return sections, 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
Expand All @@ -130,29 +194,55 @@ func diffFormattedFilesToArray(paths []string, cfg gcicfg.Config, diffs *[]strin
})
}

func getIssuedTextGci(settings *config.LintersSettings) string {
text := "File is not `gci`-ed"
// Code bellow this comment is borrowed and modified from gci.
// https://github.com/daixiang0/gci/blob/4725b0c101801e7449530eee2ddb0c72592e3405/pkg/config/config.go

var defaultOrder = map[string]int{
section.StandardType: 0,
section.DefaultType: 1,
section.CustomType: 2,
section.BlankType: 3,
section.DotType: 4,
section.AliasType: 5,
section.LocalModuleType: 6,
}

hasOptions := settings.Gci.SkipGenerated || len(settings.Gci.Sections) > 0
if !hasOptions {
return text
}
type YamlConfig struct {
origin gcicfg.YamlConfig
}

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

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

if len(settings.Gci.Sections) > 0 {
for _, section := range settings.Gci.Sections {
text += " -s " + section
}
if sections == nil {
sections = section.DefaultSections()
}

if settings.Gci.CustomOrder {
text += " --custom-order"
// if default order sorted sections
if !g.origin.Cfg.CustomOrder {
sort.Slice(sections, func(i, j int) bool {
sectionI, sectionJ := sections[i].Type(), sections[j].Type()

if strings.Compare(sectionI, sectionJ) == 0 {
return strings.Compare(sections[i].String(), sections[j].String()) < 0
}
return defaultOrder[sectionI] < defaultOrder[sectionJ]
})
}

return text
sectionSeparators, err := section.Parse(g.origin.SectionSeparatorStrings)
if err != nil {
return nil, err
}
if sectionSeparators == nil {
sectionSeparators = section.DefaultSectionSeparators()
}

return &gcicfg.Config{BoolConfig: g.origin.Cfg, Sections: sections, SectionSeparators: sectionSeparators}, nil
}

0 comments on commit ed20557

Please sign in to comment.