gofumpt: Add lang-version option (#2069)
This commit is contained in:
parent
9ce20f91d5
commit
678ae9fcaf
@ -259,6 +259,9 @@ linters-settings:
|
|||||||
simplify: true
|
simplify: true
|
||||||
|
|
||||||
gofumpt:
|
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
|
# Choose whether or not to use the extra rules that are disabled
|
||||||
# by default
|
# by default
|
||||||
extra-rules: false
|
extra-rules: false
|
||||||
|
@ -59,6 +59,7 @@ var defaultLintersSettings = LintersSettings{
|
|||||||
DefaultSignifiesExhaustive: false,
|
DefaultSignifiesExhaustive: false,
|
||||||
},
|
},
|
||||||
Gofumpt: GofumptSettings{
|
Gofumpt: GofumptSettings{
|
||||||
|
LangVersion: "",
|
||||||
ExtraRules: false,
|
ExtraRules: false,
|
||||||
},
|
},
|
||||||
ErrorLint: ErrorLintSettings{
|
ErrorLint: ErrorLintSettings{
|
||||||
@ -232,6 +233,7 @@ type GoFmtSettings struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type GofumptSettings struct {
|
type GofumptSettings struct {
|
||||||
|
LangVersion string `mapstructure:"lang-version"`
|
||||||
ExtraRules bool `mapstructure:"extra-rules"`
|
ExtraRules bool `mapstructure:"extra-rules"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"golang.org/x/tools/go/analysis"
|
"golang.org/x/tools/go/analysis"
|
||||||
"mvdan.cc/gofumpt/format"
|
"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/golinters/goanalysis"
|
||||||
"github.com/golangci/golangci-lint/pkg/lint/linter"
|
"github.com/golangci/golangci-lint/pkg/lint/linter"
|
||||||
)
|
)
|
||||||
@ -32,6 +33,13 @@ func NewGofumpt() *goanalysis.Linter {
|
|||||||
[]*analysis.Analyzer{analyzer},
|
[]*analysis.Analyzer{analyzer},
|
||||||
nil,
|
nil,
|
||||||
).WithContextSetter(func(lintCtx *linter.Context) {
|
).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) {
|
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
|
||||||
var fileNames []string
|
var fileNames []string
|
||||||
for _, f := range pass.Files {
|
for _, f := range pass.Files {
|
||||||
@ -46,12 +54,12 @@ func NewGofumpt() *goanalysis.Linter {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to open file %s: %w", f, err)
|
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 {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error while running gofumpt: %w", err)
|
return nil, fmt.Errorf("error while running gofumpt: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !bytes.Equal(input, output) {
|
if !bytes.Equal(input, output) {
|
||||||
out := bytes.Buffer{}
|
out := bytes.Buffer{}
|
||||||
_, err = out.WriteString(fmt.Sprintf("--- %[1]s\n+++ %[1]s\n", f))
|
_, err = out.WriteString(fmt.Sprintf("--- %[1]s\n+++ %[1]s\n", f))
|
||||||
@ -90,3 +98,11 @@ func NewGofumpt() *goanalysis.Linter {
|
|||||||
return resIssues
|
return resIssues
|
||||||
}).WithLoadMode(goanalysis.LoadModeSyntax)
|
}).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
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user