Skip to content

feat: new fmt command with dedicated formatter configuration #5357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Feb 17, 2025
Merged
Prev Previous commit
Next Next commit
feat: formatters configuration structures
  • Loading branch information
ldez committed Feb 17, 2025
commit 49ee0ae8c047d8e9e373cc89d13ff7cde5321d82
5 changes: 5 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type Config struct {
Issues Issues `mapstructure:"issues"`
Severity Severity `mapstructure:"severity"`

Formatters Formatters `mapstructure:"formatters"`

InternalCmdTest bool // Option is used only for testing golangci-lint command, don't use it
InternalTest bool // Option is used only for testing golangci-lint code, don't use it
}
Expand Down Expand Up @@ -64,6 +66,9 @@ func (c *Config) Validate() error {
func NewDefault() *Config {
return &Config{
LintersSettings: defaultLintersSettings,
Formatters: Formatters{
Settings: defaultFormatterSettings,
},
}
}

Expand Down
12 changes: 12 additions & 0 deletions pkg/config/formatters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package config

type Formatters struct {
Enable []string `mapstructure:"enable"`
Settings FormatterSettings `mapstructure:"settings"`
Exclusions FormatterExclusions `mapstructure:"exclusions"`
}

type FormatterExclusions struct {
Generated string `mapstructure:"generated"`
Paths []string `mapstructure:"paths"`
}
52 changes: 52 additions & 0 deletions pkg/config/formatters_settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package config

var defaultFormatterSettings = FormatterSettings{
GoFmt: GoFmtSettings{
Simplify: true,
},
Gci: GciSettings{
Sections: []string{"standard", "default"},
SkipGenerated: true,
},
}

type FormatterSettings struct {
Gci GciSettings `mapstructure:"gci"`
GoFmt GoFmtSettings `mapstructure:"gofmt"`
GoFumpt GoFumptSettings `mapstructure:"gofumpt"`
GoImports GoImportsSettings `mapstructure:"goimports"`
}

type GciSettings struct {
Sections []string `mapstructure:"sections"`
NoInlineComments bool `mapstructure:"no-inline-comments"`
NoPrefixComments bool `mapstructure:"no-prefix-comments"`
SkipGenerated bool `mapstructure:"skip-generated"`
CustomOrder bool `mapstructure:"custom-order"`
NoLexOrder bool `mapstructure:"no-lex-order"`

// Deprecated: use Sections instead.
LocalPrefixes string `mapstructure:"local-prefixes"`
}

type GoFmtSettings struct {
Simplify bool
RewriteRules []GoFmtRewriteRule `mapstructure:"rewrite-rules"`
}

type GoFmtRewriteRule struct {
Pattern string
Replacement string
}

type GoFumptSettings struct {
ModulePath string `mapstructure:"module-path"`
ExtraRules bool `mapstructure:"extra-rules"`

// Deprecated: use the global `run.go` instead.
LangVersion string `mapstructure:"lang-version"`
}

type GoImportsSettings struct {
LocalPrefixes string `mapstructure:"local-prefixes"`
}
38 changes: 2 additions & 36 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ var defaultLintersSettings = LintersSettings{
Gofmt: GoFmtSettings{
Simplify: true,
},
Gofumpt: GofumptSettings{
Gofumpt: GoFumptSettings{
LangVersion: "",
ModulePath: "",
ExtraRules: false,
Expand Down Expand Up @@ -241,7 +241,7 @@ type LintersSettings struct {
Godot GodotSettings
Godox GodoxSettings
Gofmt GoFmtSettings
Gofumpt GofumptSettings
Gofumpt GoFumptSettings
Goheader GoHeaderSettings
Goimports GoImportsSettings
GoModDirectives GoModDirectivesSettings
Expand Down Expand Up @@ -488,18 +488,6 @@ type FunlenSettings struct {
IgnoreComments bool `mapstructure:"ignore-comments"`
}

type GciSettings struct {
Sections []string `mapstructure:"sections"`
NoInlineComments bool `mapstructure:"no-inline-comments"`
NoPrefixComments bool `mapstructure:"no-prefix-comments"`
SkipGenerated bool `mapstructure:"skip-generated"`
CustomOrder bool `mapstructure:"custom-order"`
NoLexOrder bool `mapstructure:"no-lex-order"`

// Deprecated: use Sections instead.
LocalPrefixes string `mapstructure:"local-prefixes"`
}

type GinkgoLinterSettings struct {
SuppressLenAssertion bool `mapstructure:"suppress-len-assertion"`
SuppressNilAssertion bool `mapstructure:"suppress-nil-assertion"`
Expand Down Expand Up @@ -567,34 +555,12 @@ type GodoxSettings struct {
Keywords []string
}

type GoFmtSettings struct {
Simplify bool
RewriteRules []GoFmtRewriteRule `mapstructure:"rewrite-rules"`
}

type GoFmtRewriteRule struct {
Pattern string
Replacement string
}

type GofumptSettings struct {
ModulePath string `mapstructure:"module-path"`
ExtraRules bool `mapstructure:"extra-rules"`

// Deprecated: use the global `run.go` instead.
LangVersion string `mapstructure:"lang-version"`
}

type GoHeaderSettings struct {
Values map[string]map[string]string `mapstructure:"values"`
Template string `mapstructure:"template"`
TemplatePath string `mapstructure:"template-path"`
}

type GoImportsSettings struct {
LocalPrefixes string `mapstructure:"local-prefixes"`
}

type GoModDirectivesSettings struct {
ReplaceAllowList []string `mapstructure:"replace-allow-list"`
ReplaceLocal bool `mapstructure:"replace-local"`
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ func (l *Loader) handleGoVersion() {

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

l.cfg.Formatters.Settings.GoFumpt.LangVersion = cmp.Or(l.cfg.Formatters.Settings.GoFumpt.LangVersion, l.cfg.Run.Go)

trimmedGoVersion := goutil.TrimGoVersion(l.cfg.Run.Go)

l.cfg.LintersSettings.Revive.Go = trimmedGoVersion
Expand Down
2 changes: 1 addition & 1 deletion pkg/goformatters/gofumpt/gofumpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Formatter struct {
options gofumpt.Options
}

func New(settings *config.GofumptSettings, goVersion string) *Formatter {
func New(settings *config.GoFumptSettings, goVersion string) *Formatter {
var options gofumpt.Options

if settings != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/gofumpt/gofumpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

const linterName = "gofumpt"

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