Skip to content

Commit

Permalink
gofumpt: Add lang-version option (golangci#2069)
Browse files Browse the repository at this point in the history
  • Loading branch information
kailun-qin authored and SeigeC committed Apr 4, 2023
1 parent 8a4390e commit f5aef4a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ linters-settings:
simplify: true

gofumpt:
# Select the Go version to target. The default is `1.15`.
lang-version: "1.15"

# Choose whether or not to use the extra rules that are disabled
# by default
extra-rules: false
Expand Down
6 changes: 4 additions & 2 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ var defaultLintersSettings = LintersSettings{
DefaultSignifiesExhaustive: false,
},
Gofumpt: GofumptSettings{
ExtraRules: false,
LangVersion: "",
ExtraRules: false,
},
ErrorLint: ErrorLintSettings{
Errorf: true,
Expand Down Expand Up @@ -232,7 +233,8 @@ type GoFmtSettings struct {
}

type GofumptSettings struct {
ExtraRules bool `mapstructure:"extra-rules"`
LangVersion string `mapstructure:"lang-version"`
ExtraRules bool `mapstructure:"extra-rules"`
}

type GoHeaderSettings struct {
Expand Down
22 changes: 19 additions & 3 deletions pkg/golinters/gofumpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"golang.org/x/tools/go/analysis"
"mvdan.cc/gofumpt/format"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
"github.com/golangci/golangci-lint/pkg/lint/linter"
)
Expand All @@ -32,6 +33,13 @@ func NewGofumpt() *goanalysis.Linter {
[]*analysis.Analyzer{analyzer},
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
settings := lintCtx.Settings().Gofumpt

options := format.Options{
LangVersion: getLangVersion(settings),
ExtraRules: settings.ExtraRules,
}

analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
var fileNames []string
for _, f := range pass.Files {
Expand All @@ -46,12 +54,12 @@ func NewGofumpt() *goanalysis.Linter {
if err != nil {
return nil, fmt.Errorf("unable to open file %s: %w", f, err)
}
output, err := format.Source(input, format.Options{
ExtraRules: lintCtx.Settings().Gofumpt.ExtraRules,
})

output, err := format.Source(input, options)
if err != nil {
return nil, fmt.Errorf("error while running gofumpt: %w", err)
}

if !bytes.Equal(input, output) {
out := bytes.Buffer{}
_, err = out.WriteString(fmt.Sprintf("--- %[1]s\n+++ %[1]s\n", f))
Expand Down Expand Up @@ -90,3 +98,11 @@ func NewGofumpt() *goanalysis.Linter {
return resIssues
}).WithLoadMode(goanalysis.LoadModeSyntax)
}

func getLangVersion(settings config.GofumptSettings) string {
if settings.LangVersion == "" {
// TODO: defaults to "1.15", in the future (v2) must be set by using build.Default.ReleaseTags like staticcheck.
return "1.15"
}
return settings.LangVersion
}

0 comments on commit f5aef4a

Please sign in to comment.