Skip to content

Commit c76f444

Browse files
committed
feat: formatters configuration structures
1 parent 59397a3 commit c76f444

File tree

7 files changed

+75
-38
lines changed

7 files changed

+75
-38
lines changed

pkg/config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ type Config struct {
2929
Issues Issues `mapstructure:"issues"`
3030
Severity Severity `mapstructure:"severity"`
3131

32+
Formatters Formatters `mapstructure:"formatters"`
33+
3234
InternalCmdTest bool // Option is used only for testing golangci-lint command, don't use it
3335
InternalTest bool // Option is used only for testing golangci-lint code, don't use it
3436
}
@@ -64,6 +66,9 @@ func (c *Config) Validate() error {
6466
func NewDefault() *Config {
6567
return &Config{
6668
LintersSettings: defaultLintersSettings,
69+
Formatters: Formatters{
70+
Settings: defaultFormatterSettings,
71+
},
6772
}
6873
}
6974

pkg/config/formatters.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package config
2+
3+
type Formatters struct {
4+
Enable []string `mapstructure:"enable"`
5+
Settings FormatterSettings `mapstructure:"settings"`
6+
Exclusions FormatterExclusions `mapstructure:"exclusions"`
7+
}
8+
9+
type FormatterExclusions struct {
10+
Generated string `mapstructure:"generated"`
11+
Paths []string `mapstructure:"paths"`
12+
}

pkg/config/formatters_settings.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package config
2+
3+
var defaultFormatterSettings = FormatterSettings{
4+
GoFmt: GoFmtSettings{
5+
Simplify: true,
6+
},
7+
Gci: GciSettings{
8+
Sections: []string{"standard", "default"},
9+
SkipGenerated: true,
10+
},
11+
}
12+
13+
type FormatterSettings struct {
14+
Gci GciSettings `mapstructure:"gci"`
15+
GoFmt GoFmtSettings `mapstructure:"gofmt"`
16+
GoFumpt GoFumptSettings `mapstructure:"gofumpt"`
17+
GoImports GoImportsSettings `mapstructure:"goimports"`
18+
}
19+
20+
type GciSettings struct {
21+
Sections []string `mapstructure:"sections"`
22+
NoInlineComments bool `mapstructure:"no-inline-comments"`
23+
NoPrefixComments bool `mapstructure:"no-prefix-comments"`
24+
SkipGenerated bool `mapstructure:"skip-generated"`
25+
CustomOrder bool `mapstructure:"custom-order"`
26+
NoLexOrder bool `mapstructure:"no-lex-order"`
27+
28+
// Deprecated: use Sections instead.
29+
LocalPrefixes string `mapstructure:"local-prefixes"`
30+
}
31+
32+
type GoFmtSettings struct {
33+
Simplify bool
34+
RewriteRules []GoFmtRewriteRule `mapstructure:"rewrite-rules"`
35+
}
36+
37+
type GoFmtRewriteRule struct {
38+
Pattern string
39+
Replacement string
40+
}
41+
42+
type GoFumptSettings struct {
43+
ModulePath string `mapstructure:"module-path"`
44+
ExtraRules bool `mapstructure:"extra-rules"`
45+
46+
// Deprecated: use the global `run.go` instead.
47+
LangVersion string `mapstructure:"lang-version"`
48+
}
49+
50+
type GoImportsSettings struct {
51+
LocalPrefixes string `mapstructure:"local-prefixes"`
52+
}

pkg/config/linters_settings.go

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ var defaultLintersSettings = LintersSettings{
7777
Gofmt: GoFmtSettings{
7878
Simplify: true,
7979
},
80-
Gofumpt: GofumptSettings{
80+
Gofumpt: GoFumptSettings{
8181
LangVersion: "",
8282
ModulePath: "",
8383
ExtraRules: false,
@@ -236,7 +236,7 @@ type LintersSettings struct {
236236
Godot GodotSettings
237237
Godox GodoxSettings
238238
Gofmt GoFmtSettings
239-
Gofumpt GofumptSettings
239+
Gofumpt GoFumptSettings
240240
Goheader GoHeaderSettings
241241
Goimports GoImportsSettings
242242
GoModDirectives GoModDirectivesSettings
@@ -483,18 +483,6 @@ type FunlenSettings struct {
483483
IgnoreComments bool `mapstructure:"ignore-comments"`
484484
}
485485

486-
type GciSettings struct {
487-
Sections []string `mapstructure:"sections"`
488-
NoInlineComments bool `mapstructure:"no-inline-comments"`
489-
NoPrefixComments bool `mapstructure:"no-prefix-comments"`
490-
SkipGenerated bool `mapstructure:"skip-generated"`
491-
CustomOrder bool `mapstructure:"custom-order"`
492-
NoLexOrder bool `mapstructure:"no-lex-order"`
493-
494-
// Deprecated: use Sections instead.
495-
LocalPrefixes string `mapstructure:"local-prefixes"`
496-
}
497-
498486
type GinkgoLinterSettings struct {
499487
SuppressLenAssertion bool `mapstructure:"suppress-len-assertion"`
500488
SuppressNilAssertion bool `mapstructure:"suppress-nil-assertion"`
@@ -562,34 +550,12 @@ type GodoxSettings struct {
562550
Keywords []string
563551
}
564552

565-
type GoFmtSettings struct {
566-
Simplify bool
567-
RewriteRules []GoFmtRewriteRule `mapstructure:"rewrite-rules"`
568-
}
569-
570-
type GoFmtRewriteRule struct {
571-
Pattern string
572-
Replacement string
573-
}
574-
575-
type GofumptSettings struct {
576-
ModulePath string `mapstructure:"module-path"`
577-
ExtraRules bool `mapstructure:"extra-rules"`
578-
579-
// Deprecated: use the global `run.go` instead.
580-
LangVersion string `mapstructure:"lang-version"`
581-
}
582-
583553
type GoHeaderSettings struct {
584554
Values map[string]map[string]string `mapstructure:"values"`
585555
Template string `mapstructure:"template"`
586556
TemplatePath string `mapstructure:"template-path"`
587557
}
588558

589-
type GoImportsSettings struct {
590-
LocalPrefixes string `mapstructure:"local-prefixes"`
591-
}
592-
593559
type GoModDirectivesSettings struct {
594560
ReplaceAllowList []string `mapstructure:"replace-allow-list"`
595561
ReplaceLocal bool `mapstructure:"replace-local"`

pkg/config/loader.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ func (l *Loader) handleGoVersion() {
321321

322322
l.cfg.LintersSettings.Gofumpt.LangVersion = cmp.Or(l.cfg.LintersSettings.Gofumpt.LangVersion, l.cfg.Run.Go)
323323

324+
l.cfg.Formatters.Settings.GoFumpt.LangVersion = cmp.Or(l.cfg.Formatters.Settings.GoFumpt.LangVersion, l.cfg.Run.Go)
325+
324326
trimmedGoVersion := goutil.TrimGoVersion(l.cfg.Run.Go)
325327

326328
l.cfg.LintersSettings.Revive.Go = trimmedGoVersion

pkg/goformatters/gofumpt/gofumpt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type Formatter struct {
1414
options gofumpt.Options
1515
}
1616

17-
func New(settings *config.GofumptSettings, goVersion string) *Formatter {
17+
func New(settings *config.GoFumptSettings, goVersion string) *Formatter {
1818
var options gofumpt.Options
1919

2020
if settings != nil {

pkg/golinters/gofumpt/gofumpt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
const linterName = "gofumpt"
1414

15-
func New(settings *config.GofumptSettings) *goanalysis.Linter {
15+
func New(settings *config.GoFumptSettings) *goanalysis.Linter {
1616
a := goformatters.NewAnalyzer(
1717
internal.LinterLogger.Child(linterName),
1818
"Checks if code and import statements are formatted, with additional rules.",

0 commit comments

Comments
 (0)