Skip to content

Commit 2518edb

Browse files
committed
refactor: use isolate MetaFormatter from linters
1 parent 5e5a739 commit 2518edb

File tree

5 files changed

+50
-33
lines changed

5 files changed

+50
-33
lines changed

pkg/config/linters_settings.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
)
1111

1212
var defaultLintersSettings = LintersSettings{
13+
FormatterSettings: defaultFormatterSettings,
14+
1315
Asasalint: AsasalintSettings{
1416
UseBuiltinExclusions: true,
1517
},
@@ -43,10 +45,6 @@ var defaultLintersSettings = LintersSettings{
4345
Forbidigo: ForbidigoSettings{
4446
ExcludeGodocExamples: true,
4547
},
46-
Gci: GciSettings{
47-
Sections: []string{"standard", "default"},
48-
SkipGenerated: true,
49-
},
5048
GoChecksumType: GoChecksumTypeSettings{
5149
DefaultSignifiesExhaustive: true,
5250
},
@@ -74,14 +72,6 @@ var defaultLintersSettings = LintersSettings{
7472
Scope: "declarations",
7573
Period: true,
7674
},
77-
Gofmt: GoFmtSettings{
78-
Simplify: true,
79-
},
80-
Gofumpt: GoFumptSettings{
81-
LangVersion: "",
82-
ModulePath: "",
83-
ExtraRules: false,
84-
},
8575
Gosec: GoSecSettings{
8676
Concurrency: runtime.NumCPU(),
8777
},
@@ -214,6 +204,8 @@ var defaultLintersSettings = LintersSettings{
214204
}
215205

216206
type LintersSettings struct {
207+
FormatterSettings `mapstructure:",squash"`
208+
217209
Asasalint AsasalintSettings
218210
BiDiChk BiDiChkSettings
219211
CopyLoopVar CopyLoopVarSettings
@@ -231,7 +223,6 @@ type LintersSettings struct {
231223
Fatcontext FatcontextSettings
232224
Forbidigo ForbidigoSettings
233225
Funlen FunlenSettings
234-
Gci GciSettings
235226
GinkgoLinter GinkgoLinterSettings
236227
Gocognit GocognitSettings
237228
GoChecksumType GoChecksumTypeSettings
@@ -240,10 +231,7 @@ type LintersSettings struct {
240231
Gocyclo GoCycloSettings
241232
Godot GodotSettings
242233
Godox GodoxSettings
243-
Gofmt GoFmtSettings
244-
Gofumpt GoFumptSettings
245234
Goheader GoHeaderSettings
246-
Goimports GoImportsSettings
247235
GoModDirectives GoModDirectivesSettings
248236
Gomodguard GoModGuardSettings
249237
Gosec GoSecSettings

pkg/config/loader.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,7 @@ func (l *Loader) handleGoVersion() {
319319

320320
l.cfg.LintersSettings.ParallelTest.Go = l.cfg.Run.Go
321321

322-
l.cfg.LintersSettings.Gofumpt.LangVersion = cmp.Or(l.cfg.LintersSettings.Gofumpt.LangVersion, l.cfg.Run.Go)
323-
322+
l.cfg.LintersSettings.GoFumpt.LangVersion = cmp.Or(l.cfg.LintersSettings.GoFumpt.LangVersion, l.cfg.Run.Go)
324323
l.cfg.Formatters.Settings.GoFumpt.LangVersion = cmp.Or(l.cfg.Formatters.Settings.GoFumpt.LangVersion, l.cfg.Run.Go)
325324

326325
trimmedGoVersion := goutil.TrimGoVersion(l.cfg.Run.Go)
@@ -428,16 +427,22 @@ func (l *Loader) handleLinterOptionDeprecations() {
428427
if l.cfg.LintersSettings.Gci.LocalPrefixes != "" {
429428
l.log.Warnf("The configuration option `linters.gci.local-prefixes` is deprecated, please use `prefix()` inside `linters.gci.sections`.")
430429
}
430+
if l.cfg.Formatters.Settings.Gci.LocalPrefixes != "" {
431+
l.log.Warnf("The configuration option `formatters.settings.gci.local-prefixes` is deprecated, please use `prefix()` inside `formatters.settings.gci.sections`.")
432+
}
431433

432434
// Deprecated since v1.33.0.
433435
if l.cfg.LintersSettings.Godot.CheckAll != nil {
434436
l.log.Warnf("The configuration option `linters.godot.check-all` is deprecated, please use `linters.godot.scope: all`.")
435437
}
436438

437439
// Deprecated since v1.47.0
438-
if l.cfg.LintersSettings.Gofumpt.LangVersion != "" {
440+
if l.cfg.LintersSettings.GoFumpt.LangVersion != "" {
439441
l.log.Warnf("The configuration option `linters.gofumpt.lang-version` is deprecated, please use global `run.go`.")
440442
}
443+
if l.cfg.Formatters.Settings.GoFumpt.LangVersion != "" {
444+
l.log.Warnf("The configuration option `formatters.settings.gofumpt.lang-version` is deprecated, please use global `run.go`.")
445+
}
441446

442447
// Deprecated since v1.47.0
443448
if l.cfg.LintersSettings.Staticcheck.GoVersion != "" {

pkg/goformatters/meta_formatter.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import (
44
"bytes"
55
"fmt"
66
"go/format"
7+
"slices"
78

89
"github.com/golangci/golangci-lint/pkg/config"
910
"github.com/golangci/golangci-lint/pkg/goformatters/gci"
1011
"github.com/golangci/golangci-lint/pkg/goformatters/gofmt"
1112
"github.com/golangci/golangci-lint/pkg/goformatters/gofumpt"
1213
"github.com/golangci/golangci-lint/pkg/goformatters/goimports"
13-
"github.com/golangci/golangci-lint/pkg/lint/linter"
1414
"github.com/golangci/golangci-lint/pkg/logutils"
1515
)
1616

@@ -19,24 +19,30 @@ type MetaFormatter struct {
1919
formatters []Formatter
2020
}
2121

22-
func NewMetaFormatter(log logutils.Log, cfg *config.Config, enabledLinters map[string]*linter.Config) (*MetaFormatter, error) {
22+
func NewMetaFormatter(log logutils.Log, cfg *config.Formatters, runCfg *config.Run) (*MetaFormatter, error) {
23+
for _, formatter := range cfg.Enable {
24+
if !IsFormatter(formatter) {
25+
return nil, fmt.Errorf("invalid formatter %q", formatter)
26+
}
27+
}
28+
2329
m := &MetaFormatter{log: log}
2430

25-
if _, ok := enabledLinters[gofmt.Name]; ok {
26-
m.formatters = append(m.formatters, gofmt.New(&cfg.LintersSettings.Gofmt))
31+
if slices.Contains(cfg.Enable, gofmt.Name) {
32+
m.formatters = append(m.formatters, gofmt.New(&cfg.Settings.GoFmt))
2733
}
2834

29-
if _, ok := enabledLinters[gofumpt.Name]; ok {
30-
m.formatters = append(m.formatters, gofumpt.New(&cfg.LintersSettings.Gofumpt, cfg.Run.Go))
35+
if slices.Contains(cfg.Enable, gofumpt.Name) {
36+
m.formatters = append(m.formatters, gofumpt.New(&cfg.Settings.GoFumpt, runCfg.Go))
3137
}
3238

33-
if _, ok := enabledLinters[goimports.Name]; ok {
34-
m.formatters = append(m.formatters, goimports.New(&cfg.LintersSettings.Goimports))
39+
if slices.Contains(cfg.Enable, goimports.Name) {
40+
m.formatters = append(m.formatters, goimports.New(&cfg.Settings.GoImports))
3541
}
3642

3743
// gci is a last because the only goal of gci is to handle imports.
38-
if _, ok := enabledLinters[gci.Name]; ok {
39-
formatter, err := gci.New(&cfg.LintersSettings.Gci)
44+
if slices.Contains(cfg.Enable, gci.Name) {
45+
formatter, err := gci.New(&cfg.Settings.Gci)
4046
if err != nil {
4147
return nil, fmt.Errorf("gci: creating formatter: %w", err)
4248
}
@@ -72,3 +78,7 @@ func (m *MetaFormatter) Format(filename string, src []byte) []byte {
7278

7379
return data
7480
}
81+
82+
func IsFormatter(name string) bool {
83+
return slices.Contains([]string{gofmt.Name, gofumpt.Name, goimports.Name, gci.Name}, name)
84+
}

pkg/lint/lintersdb/builder_linter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,13 +393,13 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
393393
WithAutoFix().
394394
WithURL("https://github.com/Djarvur/go-err113"),
395395

396-
linter.NewConfig(gofmt.New(&cfg.LintersSettings.Gofmt)).
396+
linter.NewConfig(gofmt.New(&cfg.LintersSettings.GoFmt)).
397397
WithSince("v1.0.0").
398398
WithPresets(linter.PresetFormatting).
399399
WithAutoFix().
400400
WithURL("https://pkg.go.dev/cmd/gofmt"),
401401

402-
linter.NewConfig(gofumpt.New(&cfg.LintersSettings.Gofumpt)).
402+
linter.NewConfig(gofumpt.New(&cfg.LintersSettings.GoFumpt)).
403403
WithSince("v1.28.0").
404404
WithPresets(linter.PresetFormatting).
405405
WithAutoFix().
@@ -411,7 +411,7 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
411411
WithAutoFix().
412412
WithURL("https://github.com/denis-tingaikin/go-header"),
413413

414-
linter.NewConfig(goimports.New(&cfg.LintersSettings.Goimports)).
414+
linter.NewConfig(goimports.New(&cfg.LintersSettings.GoImports)).
415415
WithSince("v1.20.0").
416416
WithPresets(linter.PresetFormatting, linter.PresetImport).
417417
WithAutoFix().

pkg/lint/runner.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"runtime/debug"
88
"strings"
99

10+
"golang.org/x/exp/maps"
11+
1012
"github.com/golangci/golangci-lint/internal/errorutil"
1113
"github.com/golangci/golangci-lint/pkg/config"
1214
"github.com/golangci/golangci-lint/pkg/fsutils"
@@ -71,7 +73,19 @@ func NewRunner(log logutils.Log, cfg *config.Config, args []string, goenv *gouti
7173
return nil, fmt.Errorf("failed to get enabled linters: %w", err)
7274
}
7375

74-
metaFormatter, err := goformatters.NewMetaFormatter(log, cfg, enabledLinters)
76+
var enabledFormatters []string
77+
for _, name := range maps.Keys(enabledLinters) {
78+
if goformatters.IsFormatter(name) {
79+
enabledFormatters = append(enabledFormatters, name)
80+
}
81+
}
82+
83+
formattersCfg := &config.Formatters{
84+
Enable: enabledFormatters,
85+
Settings: cfg.LintersSettings.FormatterSettings,
86+
}
87+
88+
metaFormatter, err := goformatters.NewMetaFormatter(log, formattersCfg, &cfg.Run)
7589
if err != nil {
7690
return nil, fmt.Errorf("failed to create meta-formatter: %w", err)
7791
}

0 commit comments

Comments
 (0)