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

gofmt: support rewrite #3172

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pkg/commands/run.go
Expand Up @@ -151,6 +151,9 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is
fs.BoolVar(&lsc.Gofmt.Simplify, "gofmt.simplify", true, "Gofmt: simplify code")
hideFlag("gofmt.simplify")

fs.StringVar(&lsc.Gofmt.Rewrite, "gofmt.rewrite", "", "Gofmt: rewrite code")
hideFlag("gofmt.rewrite")

fs.IntVar(&lsc.Gocyclo.MinComplexity, "gocyclo.min-complexity",
30, "Minimal complexity of function to report it")
hideFlag("gocyclo.min-complexity")
Expand Down
1 change: 1 addition & 0 deletions pkg/config/linters_settings.go
Expand Up @@ -336,6 +336,7 @@ type GodoxSettings struct {

type GoFmtSettings struct {
Simplify bool
Rewrite string
}

type GofumptSettings struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/gofmt.go
Expand Up @@ -58,7 +58,7 @@ func runGofmt(lintCtx *linter.Context, pass *analysis.Pass, settings *config.GoF
var issues []goanalysis.Issue

for _, f := range fileNames {
diff, err := gofmtAPI.Run(f, settings.Simplify)
diff, err := gofmtAPI.RunRewrite(f, settings.Simplify, settings.Rewrite)
if err != nil { // TODO: skip
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/golinters/gofmt_common.go
Expand Up @@ -223,6 +223,9 @@ func getErrorTextForLinter(settings *config.LintersSettings, linterName string)
if settings.Gofmt.Simplify {
text += " with `-s`"
}
if settings.Gofmt.Rewrite != "" {
text += fmt.Sprintf(" `-r '%s'`", settings.Gofmt.Rewrite)
}
case goimportsName:
text = "File is not `goimports`-ed"
if settings.Goimports.LocalPrefixes != "" {
Expand Down