Skip to content

Commit aba80c7

Browse files
authored
feat: update gofmt and goimports and add option "rewrite-rules" (#3174)
1 parent 0d33a5b commit aba80c7

11 files changed

+101
-7
lines changed

.golangci.reference.yml

+8
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,14 @@ linters-settings:
567567
# Simplify code: gofmt with `-s` option.
568568
# Default: true
569569
simplify: false
570+
# Apply the rewrite rules to the source before reformatting.
571+
# https://pkg.go.dev/cmd/gofmt
572+
# Default: []
573+
rewrite-rules:
574+
- pattern: 'interface{}'
575+
replacement: 'any'
576+
- pattern: 'a[b:len(a)]'
577+
replacement: 'a[b:]'
570578

571579
gofumpt:
572580
# Select the Go version to target.

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ require (
3434
github.com/golangci/check v0.0.0-20180506172741-cfe4005ccda2
3535
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a
3636
github.com/golangci/go-misc v0.0.0-20220329215616-d24fe342adfe
37-
github.com/golangci/gofmt v0.0.0-20190930125516-244bba706f1a
37+
github.com/golangci/gofmt v0.0.0-20220901101216-f2edd75033f2
3838
github.com/golangci/lint-1 v0.0.0-20191013205115-297bf364a8e0
3939
github.com/golangci/maligned v0.0.0-20180506175553-b1d89398deca
4040
github.com/golangci/misspell v0.3.5

go.sum

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/linters_settings.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,13 @@ type GodoxSettings struct {
337337
}
338338

339339
type GoFmtSettings struct {
340-
Simplify bool
340+
Simplify bool
341+
RewriteRules []GoFmtRewriteRule `mapstructure:"rewrite-rules"`
342+
}
343+
344+
type GoFmtRewriteRule struct {
345+
Pattern string
346+
Replacement string
341347
}
342348

343349
type GofumptSettings struct {

pkg/golinters/gofmt.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,15 @@ func NewGofmt(settings *config.GoFmtSettings) *goanalysis.Linter {
5555
func runGofmt(lintCtx *linter.Context, pass *analysis.Pass, settings *config.GoFmtSettings) ([]goanalysis.Issue, error) {
5656
fileNames := getFileNames(pass)
5757

58+
var rewriteRules []gofmtAPI.RewriteRule
59+
for _, rule := range settings.RewriteRules {
60+
rewriteRules = append(rewriteRules, gofmtAPI.RewriteRule(rule))
61+
}
62+
5863
var issues []goanalysis.Issue
5964

6065
for _, f := range fileNames {
61-
diff, err := gofmtAPI.Run(f, settings.Simplify)
66+
diff, err := gofmtAPI.RunRewrite(f, settings.Simplify, rewriteRules)
6267
if err != nil { // TODO: skip
6368
return nil, err
6469
}

pkg/golinters/gofmt_common.go

+3
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ func getErrorTextForLinter(settings *config.LintersSettings, linterName string)
223223
if settings.Gofmt.Simplify {
224224
text += " with `-s`"
225225
}
226+
for _, rule := range settings.Gofmt.RewriteRules {
227+
text += fmt.Sprintf(" `-r '%s -> %s'`", rule.Pattern, rule.Replacement)
228+
}
226229
case goimportsName:
227230
text = "File is not `goimports`-ed"
228231
if settings.Goimports.LocalPrefixes != "" {

pkg/golinters/goimports.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func NewGoimports(settings *config.GoImportsSettings) *goanalysis.Linter {
3434
imports.LocalPrefix = settings.LocalPrefixes
3535

3636
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
37-
issues, err := runGoiImports(lintCtx, pass)
37+
issues, err := runGoImports(lintCtx, pass)
3838
if err != nil {
3939
return nil, err
4040
}
@@ -54,7 +54,7 @@ func NewGoimports(settings *config.GoImportsSettings) *goanalysis.Linter {
5454
}).WithLoadMode(goanalysis.LoadModeSyntax)
5555
}
5656

57-
func runGoiImports(lintCtx *linter.Context, pass *analysis.Pass) ([]goanalysis.Issue, error) {
57+
func runGoImports(lintCtx *linter.Context, pass *analysis.Pass) ([]goanalysis.Issue, error) {
5858
fileNames := getFileNames(pass)
5959

6060
var issues []goanalysis.Issue
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
linters-settings:
2+
gofmt:
3+
rewrite-rules:
4+
- pattern: 'interface{}'
5+
replacement: 'any'
6+
- pattern: 'a[b:len(a)]'
7+
replacement: 'a[b:]'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//golangcitest:args -Egofmt
2+
//golangcitest:config_path testdata/configs/gofmt_rewrite_rules.yml
3+
//golangcitest:expected_exitcode 0
4+
package p
5+
6+
import "fmt"
7+
8+
func GofmtRewriteRule() {
9+
values := make([]int, 0)
10+
11+
values = append(values, 1)
12+
values = append(values, 2)
13+
values = append(values, 3)
14+
15+
slice := values[1:len(values)]
16+
17+
fmt.Println(slice)
18+
}
19+
20+
func GofmtRewriteRule2() {
21+
var to interface{}
22+
23+
fmt.Println(to)
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//golangcitest:args -Egofmt
2+
//golangcitest:config_path testdata/configs/gofmt_rewrite_rules.yml
3+
//golangcitest:expected_exitcode 0
4+
package p
5+
6+
import "fmt"
7+
8+
func GofmtRewriteRule() {
9+
values := make([]int, 0)
10+
11+
values = append(values, 1)
12+
values = append(values, 2)
13+
values = append(values, 3)
14+
15+
slice := values[1:]
16+
17+
fmt.Println(slice)
18+
}
19+
20+
func GofmtRewriteRule2() {
21+
var to any
22+
23+
fmt.Println(to)
24+
}

test/testdata/gofmt_rewrite_rules.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//golangcitest:args -Egofmt
2+
//golangcitest:config_path testdata/configs/gofmt_rewrite_rules.yml
3+
package testdata
4+
5+
import "fmt"
6+
7+
func GofmtRewriteRule() {
8+
vals := make([]int, 0)
9+
10+
vals = append(vals, 1)
11+
vals = append(vals, 2)
12+
vals = append(vals, 3)
13+
14+
slice := vals[1:len(vals)] // want "^File is not `gofmt`-ed"
15+
16+
fmt.Println(slice)
17+
}

0 commit comments

Comments
 (0)