Skip to content

Commit 9736548

Browse files
committed
refactor: use isolate MetaFormatter from linters
1 parent 59419bb commit 9736548

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
},
@@ -209,6 +199,8 @@ var defaultLintersSettings = LintersSettings{
209199
}
210200

211201
type LintersSettings struct {
202+
FormatterSettings `mapstructure:",squash"`
203+
212204
Asasalint AsasalintSettings
213205
BiDiChk BiDiChkSettings
214206
CopyLoopVar CopyLoopVarSettings
@@ -226,7 +218,6 @@ type LintersSettings struct {
226218
Fatcontext FatcontextSettings
227219
Forbidigo ForbidigoSettings
228220
Funlen FunlenSettings
229-
Gci GciSettings
230221
GinkgoLinter GinkgoLinterSettings
231222
Gocognit GocognitSettings
232223
GoChecksumType GoChecksumTypeSettings
@@ -235,10 +226,7 @@ type LintersSettings struct {
235226
Gocyclo GoCycloSettings
236227
Godot GodotSettings
237228
Godox GodoxSettings
238-
Gofmt GoFmtSettings
239-
Gofumpt GoFumptSettings
240229
Goheader GoHeaderSettings
241-
Goimports GoImportsSettings
242230
GoModDirectives GoModDirectivesSettings
243231
Gomodguard GoModGuardSettings
244232
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
@@ -394,13 +394,13 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
394394
WithAutoFix().
395395
WithURL("https://github.com/Djarvur/go-err113"),
396396

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

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

415-
linter.NewConfig(goimports.New(&cfg.LintersSettings.Goimports)).
415+
linter.NewConfig(goimports.New(&cfg.LintersSettings.GoImports)).
416416
WithSince("v1.20.0").
417417
WithPresets(linter.PresetFormatting, linter.PresetImport).
418418
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)