Skip to content

Commit 10c05a3

Browse files
committed
feat: migrate gci
1 parent 5c0ac35 commit 10c05a3

File tree

7 files changed

+22
-108
lines changed

7 files changed

+22
-108
lines changed

.golangci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ issues:
178178
- path: pkg/golinters/godot/godot.go
179179
linters: [staticcheck]
180180
text: "SA1019: settings.CheckAll is deprecated: use Scope instead"
181-
- path: pkg/golinters/gci/gci.go
181+
- path: pkg/goformatters/gci/gci.go
182182
linters: [staticcheck]
183183
text: "SA1019: settings.LocalPrefixes is deprecated: use Sections instead."
184184
- path: pkg/golinters/mnd/mnd.go

go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ require (
9595
github.com/sashamelentyev/interfacebloat v1.1.0
9696
github.com/sashamelentyev/usestdlibvars v1.28.0
9797
github.com/securego/gosec/v2 v2.21.4
98-
github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c
9998
github.com/shirou/gopsutil/v4 v4.24.12
10099
github.com/sirupsen/logrus v1.9.3
101100
github.com/sivchari/containedctx v1.0.3

go.sum

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

pkg/golinters/gci/gci.go

+14-97
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,33 @@
11
package gci
22

33
import (
4-
"bytes"
5-
"fmt"
6-
"io"
7-
"os"
8-
9-
gcicfg "github.com/daixiang0/gci/pkg/config"
10-
"github.com/daixiang0/gci/pkg/gci"
11-
"github.com/daixiang0/gci/pkg/log"
12-
"github.com/shazow/go-diff/difflib"
134
"golang.org/x/tools/go/analysis"
145

156
"github.com/golangci/golangci-lint/pkg/config"
167
"github.com/golangci/golangci-lint/pkg/goanalysis"
8+
"github.com/golangci/golangci-lint/pkg/goformatters"
9+
gcibase "github.com/golangci/golangci-lint/pkg/goformatters/gci"
1710
"github.com/golangci/golangci-lint/pkg/golinters/internal"
18-
"github.com/golangci/golangci-lint/pkg/lint/linter"
1911
)
2012

2113
const linterName = "gci"
2214

23-
type differ interface {
24-
Diff(out io.Writer, a io.ReadSeeker, b io.ReadSeeker) error
25-
}
26-
2715
func New(settings *config.GciSettings) *goanalysis.Linter {
28-
log.InitLogger()
29-
_ = log.L().Sync()
30-
31-
diff := difflib.New()
32-
33-
a := &analysis.Analyzer{
34-
Name: linterName,
35-
Doc: goanalysis.TheOnlyanalyzerDoc,
36-
Run: goanalysis.DummyRun,
16+
formatter, err := gcibase.New(settings)
17+
if err != nil {
18+
internal.LinterLogger.Fatalf("%s: create analyzer: %v", linterName, err)
3719
}
3820

21+
a := goformatters.NewAnalyzer(
22+
internal.LinterLogger.Child(linterName),
23+
"Checks if code and import statements are formatted, with additional rules.",
24+
formatter,
25+
)
26+
3927
return goanalysis.NewLinter(
40-
linterName,
41-
"Checks if code and import statements are formatted, it makes import statements always deterministic.",
28+
a.Name,
29+
a.Doc,
4230
[]*analysis.Analyzer{a},
4331
nil,
44-
).WithContextSetter(func(lintCtx *linter.Context) {
45-
a.Run = func(pass *analysis.Pass) (any, error) {
46-
err := run(lintCtx, pass, settings, diff)
47-
if err != nil {
48-
return nil, err
49-
}
50-
51-
return nil, nil
52-
}
53-
}).WithLoadMode(goanalysis.LoadModeSyntax)
54-
}
55-
56-
func run(lintCtx *linter.Context, pass *analysis.Pass, settings *config.GciSettings, diff differ) error {
57-
cfg := gcicfg.YamlConfig{
58-
Cfg: gcicfg.BoolConfig{
59-
NoInlineComments: settings.NoInlineComments,
60-
NoPrefixComments: settings.NoPrefixComments,
61-
SkipGenerated: settings.SkipGenerated,
62-
CustomOrder: settings.CustomOrder,
63-
NoLexOrder: settings.NoLexOrder,
64-
},
65-
SectionStrings: settings.Sections,
66-
ModPath: pass.Module.Path,
67-
}
68-
69-
if settings.LocalPrefixes != "" {
70-
cfg.SectionStrings = []string{
71-
"standard",
72-
"default",
73-
fmt.Sprintf("prefix(%s)", settings.LocalPrefixes),
74-
}
75-
}
76-
77-
parsedCfg, err := cfg.Parse()
78-
if err != nil {
79-
return err
80-
}
81-
82-
for _, file := range pass.Files {
83-
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
84-
if !isGoFile {
85-
continue
86-
}
87-
88-
input, err := os.ReadFile(position.Filename)
89-
if err != nil {
90-
return fmt.Errorf("unable to open file %s: %w", position.Filename, err)
91-
}
92-
93-
_, output, err := gci.LoadFormat(input, position.Filename, *parsedCfg)
94-
if err != nil {
95-
return fmt.Errorf("error while running gci: %w", err)
96-
}
97-
98-
if !bytes.Equal(input, output) {
99-
out := bytes.NewBufferString(fmt.Sprintf("--- %[1]s\n+++ %[1]s\n", position.Filename))
100-
101-
err := diff.Diff(out, bytes.NewReader(input), bytes.NewReader(output))
102-
if err != nil {
103-
return fmt.Errorf("error while running gci: %w", err)
104-
}
105-
106-
diff := out.String()
107-
108-
err = internal.ExtractDiagnosticFromPatch(pass, file, diff, lintCtx)
109-
if err != nil {
110-
return fmt.Errorf("can't extract issues from gci diff output %q: %w", diff, err)
111-
}
112-
}
113-
}
114-
115-
return nil
32+
).WithLoadMode(goanalysis.LoadModeSyntax)
11633
}

pkg/golinters/gci/testdata/gci.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
//golangcitest:config_path testdata/gci.yml
33
package testdata
44

5-
import ( // want "File is not properly formatted"
6-
"golang.org/x/tools/go/analysis"
7-
"github.com/golangci/golangci-lint/pkg/config" // want "File is not properly formatted"
5+
import (
6+
"golang.org/x/tools/go/analysis" // want "File is not properly formatted"
7+
"github.com/golangci/golangci-lint/pkg/config"
88
"fmt"
99
"errors"
1010
gcicfg "github.com/daixiang0/gci/pkg/config"

pkg/golinters/gci/testdata/gci_cgo.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ package testdata
1212
*/
1313
import "C"
1414

15-
import ( // want "File is not properly formatted"
16-
"golang.org/x/tools/go/analysis"
17-
"github.com/golangci/golangci-lint/pkg/config" // want "File is not properly formatted"
18-
"unsafe"
15+
import (
16+
"golang.org/x/tools/go/analysis" // want "File is not properly formatted"
17+
"github.com/golangci/golangci-lint/pkg/config"
1918
"fmt"
2019
"errors"
2120
gcicfg "github.com/daixiang0/gci/pkg/config"

pkg/result/processors/max_per_file_from_linter.go

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func NewMaxPerFileFromLinter(cfg *config.Config) *MaxPerFileFromLinter {
2020
// otherwise we need to fix all issues in the file at once
2121
maxPerFileFromLinterConfig["gofmt"] = 1
2222
maxPerFileFromLinterConfig["goimports"] = 1
23+
maxPerFileFromLinterConfig["gci"] = 1
2324
}
2425

2526
return &MaxPerFileFromLinter{

0 commit comments

Comments
 (0)