gofumpt: Add lang-version option (#2069)

This commit is contained in:
Kailun Qin 2021-06-23 11:56:20 +08:00 committed by GitHub
parent 9ce20f91d5
commit 678ae9fcaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 5 deletions

View File

@ -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

View File

@ -59,7 +59,8 @@ var defaultLintersSettings = LintersSettings{
DefaultSignifiesExhaustive: false,
},
Gofumpt: GofumptSettings{
ExtraRules: false,
LangVersion: "",
ExtraRules: false,
},
ErrorLint: ErrorLintSettings{
Errorf: true,
@ -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 {

View File

@ -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"
)
@ -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 {
@ -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))
@ -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
}