Skip to content

Commit

Permalink
feat: update gofmt and goimports
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Sep 1, 2022
1 parent 28d7095 commit 821512c
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 5 deletions.
8 changes: 8 additions & 0 deletions .golangci.reference.yml
Expand Up @@ -567,6 +567,14 @@ linters-settings:
# Simplify code: gofmt with `-s` option.
# Default: true
simplify: false
# Apply the rewrite rules to the source before reformatting.
# https://pkg.go.dev/cmd/gofmt
# Default: []
rewrite-rules:
- pattern: 'interface{}'
replacement: 'any'
- pattern: 'a[b:len(a)]'
replacement: 'a[b:]'

gofumpt:
# Select the Go version to target.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -34,7 +34,7 @@ require (
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a
github.com/golangci/go-misc v0.0.0-20220329215616-d24fe342adfe
github.com/golangci/gofmt v0.0.0-20190930125516-244bba706f1a
github.com/golangci/gofmt v0.0.0-20220901083514-bda9c14510ff
github.com/golangci/lint-1 v0.0.0-20191013205115-297bf364a8e0
github.com/golangci/maligned v0.0.0-20180506175553-b1d89398deca
github.com/golangci/misspell v0.3.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum

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

8 changes: 7 additions & 1 deletion pkg/config/linters_settings.go
Expand Up @@ -337,7 +337,13 @@ type GodoxSettings struct {
}

type GoFmtSettings struct {
Simplify bool
Simplify bool
RewriteRules []GoFmtRewriteRule `mapstructure:"rewrite-rules"`
}

type GoFmtRewriteRule struct {
Pattern string
Replacement string
}

type GofumptSettings struct {
Expand Down
7 changes: 6 additions & 1 deletion pkg/golinters/gofmt.go
Expand Up @@ -55,10 +55,15 @@ func NewGofmt(settings *config.GoFmtSettings) *goanalysis.Linter {
func runGofmt(lintCtx *linter.Context, pass *analysis.Pass, settings *config.GoFmtSettings) ([]goanalysis.Issue, error) {
fileNames := getFileNames(pass)

var rewriteRules []gofmtAPI.RewriteRule
for _, rule := range settings.RewriteRules {
rewriteRules = append(rewriteRules, gofmtAPI.RewriteRule(rule))
}

var issues []goanalysis.Issue

for _, f := range fileNames {
diff, err := gofmtAPI.Run(f, settings.Simplify)
diff, err := gofmtAPI.RunRewrite(f, settings.Simplify, rewriteRules)
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`"
}
for _, rule := range settings.Gofmt.RewriteRules {
text += fmt.Sprintf(" `-r '%s -> %s'`", rule.Pattern, rule.Replacement)
}
case goimportsName:
text = "File is not `goimports`-ed"
if settings.Goimports.LocalPrefixes != "" {
Expand Down
5 changes: 5 additions & 0 deletions test/testdata/configs/gofmt_rewrite_rules.yml
@@ -0,0 +1,5 @@
linters-settings:
gofmt:
rewrite-rules:
- pattern: 'a[b:len(a)]'
replacement: 'a[b:]'
17 changes: 17 additions & 0 deletions test/testdata/gofmt_rewrite_rules.go
@@ -0,0 +1,17 @@
//golangcitest:args -Egofmt
//golangcitest:config_path testdata/configs/gofmt_rewrite_rules.yml
package testdata

import "fmt"

func GofmtRewriteRule() {
vals := make([]int, 0)

vals = append(vals, 1)
vals = append(vals, 2)
vals = append(vals, 3)

slice := vals[1:len(vals)] // want "^File is not `gofmt`-ed"

fmt.Println(slice)
}

0 comments on commit 821512c

Please sign in to comment.