Skip to content

Commit 457ad6a

Browse files
author
Sergey Vilgelm
committed
Support --fix for gofumpt
1 parent 6e7c317 commit 457ad6a

File tree

8 files changed

+45
-23
lines changed

8 files changed

+45
-23
lines changed

.golangci.example.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ linters-settings:
326326
# Choose whether or not to use the extra rules that are disabled
327327
# by default
328328
extra-rules: false
329+
# simplify code: gofumpt with `-s` option, false by default
330+
simplify: false
329331

330332
# The custom section can be used to define linter plugins to be loaded at runtime. See README doc
331333
# for more info.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ require (
3939
github.com/ryancurrah/gomodguard v1.1.0
4040
github.com/ryanrolds/sqlclosecheck v0.3.0
4141
github.com/securego/gosec/v2 v2.3.0
42+
github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c
4243
github.com/shirou/gopsutil v0.0.0-20190901111213-e4ec7b275ada // v2.19.8
4344
github.com/sirupsen/logrus v1.6.0
4445
github.com/sonatard/noctx v0.0.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb
307307
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
308308
github.com/securego/gosec/v2 v2.3.0 h1:y/9mCF2WPDbSDpL3QDWZD3HHGrSYw0QSHnCqTfs4JPE=
309309
github.com/securego/gosec/v2 v2.3.0/go.mod h1:UzeVyUXbxukhLeHKV3VVqo7HdoQR9MrRfFmZYotn8ME=
310+
github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c h1:W65qqJCIOVP4jpqPQ0YvHYKwcMEMVWIzWC5iNQQfBTU=
311+
github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c/go.mod h1:/PevMnwAxekIXwN8qQyfc5gl2NlkB3CQlkizAbOkeBs=
310312
github.com/shirou/gopsutil v0.0.0-20190901111213-e4ec7b275ada h1:WokF3GuxBeL+n4Lk4Fa8v9mbdjlrl7bHuneF4N1bk2I=
311313
github.com/shirou/gopsutil v0.0.0-20190901111213-e4ec7b275ada/go.mod h1:WWnYX4lzhCH5h/3YBfyVA3VbLYjlMZZAQcW9ojMexNc=
312314
github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4 h1:udFKJ0aHUL60LboW/A+DfgoHVedieIzIXE8uylPue0U=

pkg/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ type ExhaustiveSettings struct {
354354

355355
type GofumptSettings struct {
356356
ExtraRules bool `mapstructure:"extra-rules"`
357+
Simplify bool
357358
}
358359

359360
var defaultLintersSettings = LintersSettings{
@@ -411,6 +412,7 @@ var defaultLintersSettings = LintersSettings{
411412
},
412413
Gofumpt: GofumptSettings{
413414
ExtraRules: false,
415+
Simplify: false,
414416
},
415417
}
416418

pkg/golinters/gofmt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func NewGofmt() *goanalysis.Linter {
4646
continue
4747
}
4848

49-
is, err := extractIssuesFromPatch(string(diff), lintCtx.Log, lintCtx, false)
49+
is, err := extractIssuesFromPatch(string(diff), lintCtx.Log, lintCtx, gofmtName)
5050
if err != nil {
5151
return nil, errors.Wrapf(err, "can't extract issues from gofmt diff output %q", string(diff))
5252
}

pkg/golinters/gofmt_common.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func (p *hunkChangesParser) parse(h *diffpkg.Hunk) []Change {
207207
return p.ret
208208
}
209209

210-
func extractIssuesFromPatch(patch string, log logutils.Log, lintCtx *linter.Context, isGoimports bool) ([]result.Issue, error) {
210+
func extractIssuesFromPatch(patch string, log logutils.Log, lintCtx *linter.Context, linterName string) ([]result.Issue, error) {
211211
diffs, err := diffpkg.ParseMultiFileDiff([]byte(patch))
212212
if err != nil {
213213
return nil, errors.Wrap(err, "can't parse patch")
@@ -226,27 +226,36 @@ func extractIssuesFromPatch(patch string, log logutils.Log, lintCtx *linter.Cont
226226

227227
for _, hunk := range d.Hunks {
228228
var text string
229-
if isGoimports {
230-
text = "File is not `goimports`-ed"
231-
if lintCtx.Settings().Goimports.LocalPrefixes != "" {
232-
text += " with -local " + lintCtx.Settings().Goimports.LocalPrefixes
229+
switch linterName {
230+
case gofumptName:
231+
text = "File is not `gofumpt`-ed"
232+
opts := []string{}
233+
if lintCtx.Settings().Gofumpt.Simplify {
234+
opts = append(opts, "-s")
235+
}
236+
if lintCtx.Settings().Gofumpt.ExtraRules {
237+
opts = append(opts, "-extra")
238+
}
239+
if len(opts) > 0 {
240+
text += " with " + strings.Join(opts, " ")
233241
}
234-
} else {
242+
case gofmtName:
235243
text = "File is not `gofmt`-ed"
236244
if lintCtx.Settings().Gofmt.Simplify {
237245
text += " with `-s`"
238246
}
247+
case goimportsName:
248+
text = "File is not `goimports`-ed"
249+
if lintCtx.Settings().Goimports.LocalPrefixes != "" {
250+
text += " with -local " + lintCtx.Settings().Goimports.LocalPrefixes
251+
}
239252
}
240253
p := hunkChangesParser{
241254
log: log,
242255
}
243256
changes := p.parse(hunk)
244257
for _, change := range changes {
245258
change := change // fix scope
246-
linterName := gofmtName
247-
if isGoimports {
248-
linterName = goimportsName
249-
}
250259
i := result.Issue{
251260
FromLinter: linterName,
252261
Pos: token.Position{

pkg/golinters/gofumpt.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@ package golinters
33
import (
44
"bytes"
55
"fmt"
6-
"go/token"
76
"io/ioutil"
8-
"strings"
97
"sync"
108

9+
"github.com/pkg/errors"
10+
"github.com/shazow/go-diff/difflib"
1111
"golang.org/x/tools/go/analysis"
1212
"mvdan.cc/gofumpt/format"
1313

1414
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
1515
"github.com/golangci/golangci-lint/pkg/lint/linter"
16-
"github.com/golangci/golangci-lint/pkg/result"
1716
)
1817

1918
const gofumptName = "gofumpt"
2019

2120
func NewGofumpt() *goanalysis.Linter {
2221
var mu sync.Mutex
2322
var resIssues []goanalysis.Issue
23+
differ := difflib.New()
2424

2525
analyzer := &analysis.Analyzer{
2626
Name: gofumptName,
@@ -53,14 +53,20 @@ func NewGofumpt() *goanalysis.Linter {
5353
return nil, fmt.Errorf("error while running gofumpt: %w", err)
5454
}
5555
if !bytes.Equal(input, output) {
56-
issues = append(issues, goanalysis.NewIssue(&result.Issue{
57-
FromLinter: gofumptName,
58-
Text: "File is not `gofumpt`-ed",
59-
Pos: token.Position{
60-
Filename: f,
61-
Line: strings.Count(string(input), "\n"),
62-
},
63-
}, pass))
56+
out := bytes.Buffer{}
57+
out.WriteString(fmt.Sprintf("--- %[1]s\n+++ %[1]s\n", f))
58+
59+
differ.Diff(&out, bytes.NewReader(input), bytes.NewReader(output))
60+
diff := out.String()
61+
62+
is, err := extractIssuesFromPatch(diff, lintCtx.Log, lintCtx, gofumptName)
63+
if err != nil {
64+
return nil, errors.Wrapf(err, "can't extract issues from gofumpt diff output %q", string(diff))
65+
}
66+
67+
for i := range is {
68+
issues = append(issues, goanalysis.NewIssue(&is[i], pass))
69+
}
6470
}
6571
}
6672

pkg/golinters/goimports.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func NewGoimports() *goanalysis.Linter {
4747
continue
4848
}
4949

50-
is, err := extractIssuesFromPatch(string(diff), lintCtx.Log, lintCtx, true)
50+
is, err := extractIssuesFromPatch(string(diff), lintCtx.Log, lintCtx, goimportsName)
5151
if err != nil {
5252
return nil, errors.Wrapf(err, "can't extract issues from gofmt diff output %q", string(diff))
5353
}

0 commit comments

Comments
 (0)