-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: apply formatters after the suggested fixes (#5246)
- Loading branch information
Showing
11 changed files
with
337 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package goformatters | ||
|
||
type Formatter interface { | ||
Name() string | ||
Format(filename string, src []byte) ([]byte, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package gci | ||
|
||
import ( | ||
gcicfg "github.com/daixiang0/gci/pkg/config" | ||
"github.com/daixiang0/gci/pkg/gci" | ||
"github.com/ldez/grignotin/gomod" | ||
|
||
"github.com/golangci/golangci-lint/pkg/config" | ||
) | ||
|
||
const Name = "gci" | ||
|
||
type Formatter struct { | ||
config *gcicfg.Config | ||
} | ||
|
||
func New(cfg config.GciSettings) (*Formatter, error) { | ||
modPath, err := gomod.GetModulePath() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
parsedCfg, err := gcicfg.YamlConfig{ | ||
Cfg: gcicfg.BoolConfig{ | ||
NoInlineComments: cfg.NoInlineComments, | ||
NoPrefixComments: cfg.NoPrefixComments, | ||
SkipGenerated: cfg.SkipGenerated, | ||
CustomOrder: cfg.CustomOrder, | ||
NoLexOrder: cfg.NoLexOrder, | ||
}, | ||
SectionStrings: cfg.Sections, | ||
ModPath: modPath, | ||
}.Parse() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Formatter{config: parsedCfg}, nil | ||
} | ||
|
||
func (*Formatter) Name() string { | ||
return Name | ||
} | ||
|
||
func (f *Formatter) Format(filename string, src []byte) ([]byte, error) { | ||
_, formatted, err := gci.LoadFormat(src, filename, *f.config) | ||
return formatted, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package gofmt | ||
|
||
import ( | ||
"github.com/golangci/gofmt/gofmt" | ||
|
||
"github.com/golangci/golangci-lint/pkg/config" | ||
) | ||
|
||
const Name = "gofmt" | ||
|
||
type Formatter struct { | ||
options gofmt.Options | ||
} | ||
|
||
func New(cfg config.GoFmtSettings) *Formatter { | ||
var rewriteRules []gofmt.RewriteRule | ||
for _, rule := range cfg.RewriteRules { | ||
rewriteRules = append(rewriteRules, gofmt.RewriteRule(rule)) | ||
} | ||
|
||
return &Formatter{ | ||
options: gofmt.Options{ | ||
NeedSimplify: cfg.Simplify, | ||
RewriteRules: rewriteRules, | ||
}, | ||
} | ||
} | ||
|
||
func (*Formatter) Name() string { | ||
return Name | ||
} | ||
|
||
func (f *Formatter) Format(filename string, src []byte) ([]byte, error) { | ||
return gofmt.Source(filename, src, f.options) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package gofumpt | ||
|
||
import ( | ||
"strings" | ||
|
||
gofumpt "mvdan.cc/gofumpt/format" | ||
|
||
"github.com/golangci/golangci-lint/pkg/config" | ||
) | ||
|
||
const Name = "gofumpt" | ||
|
||
type Formatter struct { | ||
options gofumpt.Options | ||
} | ||
|
||
func New(cfg config.GofumptSettings, goVersion string) *Formatter { | ||
return &Formatter{ | ||
options: gofumpt.Options{ | ||
LangVersion: getLangVersion(goVersion), | ||
ModulePath: cfg.ModulePath, | ||
ExtraRules: cfg.ExtraRules, | ||
}, | ||
} | ||
} | ||
|
||
func (*Formatter) Name() string { | ||
return Name | ||
} | ||
|
||
func (f *Formatter) Format(_ string, src []byte) ([]byte, error) { | ||
return gofumpt.Source(src, f.options) | ||
} | ||
|
||
// modified copy of pkg/golinters/gofumpt/gofumpt.go | ||
func getLangVersion(v string) string { | ||
if v == "" { | ||
// TODO: defaults to "1.15", in the future (v2) must be removed. | ||
return "go1.15" | ||
} | ||
|
||
return "go" + strings.TrimPrefix(v, "go") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package goimports | ||
|
||
import ( | ||
"golang.org/x/tools/imports" | ||
) | ||
|
||
const Name = "goimports" | ||
|
||
type Formatter struct{} | ||
|
||
func New() *Formatter { | ||
return &Formatter{} | ||
} | ||
|
||
func (*Formatter) Name() string { | ||
return Name | ||
} | ||
|
||
func (*Formatter) Format(filename string, src []byte) ([]byte, error) { | ||
// The `imports.LocalPrefix` (`settings.LocalPrefixes`) is a global var. | ||
return imports.Process(filename, src, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.