Skip to content
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

gofumpt: Add lang-version option #2069

Merged
merged 2 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}