Skip to content

Commit 38d298c

Browse files
authored
Adding gofumpt (#1177)
1 parent 36a5a40 commit 38d298c

File tree

6 files changed

+103
-2
lines changed

6 files changed

+103
-2
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Follow the news and releases on our [twitter](https://twitter.com/golangci) and our [blog](https://medium.com/golangci).
22
There is the most valuable changes log:
33

4+
### June 2020
5+
1. Add new linters: `gofumpt`
6+
47
### May 2020
58

69
1. Add new linters: `nolintlint`, `goerr113`

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ require (
5757
golang.org/x/tools v0.0.0-20200519015757-0d0afa43d58a
5858
gopkg.in/yaml.v2 v2.3.0
5959
honnef.co/go/tools v0.0.1-2020.1.4
60+
mvdan.cc/gofumpt v0.0.0-20200513141252-abc0db2c416a
6061
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed
6162
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b // indirect
6263
mvdan.cc/unparam v0.0.0-20190720180237-d51796306d8f

go.sum

+7-2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
145145
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
146146
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
147147
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
148+
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
149+
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
150+
github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no=
148151
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
149152
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
150153
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
@@ -307,6 +310,7 @@ github.com/quasilyte/go-ruleguard v0.1.2-0.20200318202121-b00d7a75d3d8 h1:Dvnesv
307310
github.com/quasilyte/go-ruleguard v0.1.2-0.20200318202121-b00d7a75d3d8/go.mod h1:CGFX09Ci3pq9QZdj86B+VGIdNj4VyCo2iPOGS9esB/k=
308311
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
309312
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
313+
github.com/rogpeppe/go-internal v1.5.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
310314
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
311315
github.com/ryancurrah/gomodguard v1.1.0 h1:DWbye9KyMgytn8uYpuHkwf0RHqAYO6Ay/D0TbCpPtVU=
312316
github.com/ryancurrah/gomodguard v1.1.0/go.mod h1:4O8tr7hBODaGE6VIhfJDHcwzh5GUccKSJBU0UMXJFVM=
@@ -359,8 +363,6 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
359363
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
360364
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
361365
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
362-
github.com/stretchr/testify v1.6.0 h1:jlIyCplCJFULU/01vCkhKuTyc3OorI3bJFuw6obfgho=
363-
github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
364366
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
365367
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
366368
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
@@ -519,6 +521,7 @@ golang.org/x/tools v0.0.0-20200331202046-9d5940d49312/go.mod h1:EkVYQZoAsY45+roY
519521
golang.org/x/tools v0.0.0-20200414032229-332987a829c3/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
520522
golang.org/x/tools v0.0.0-20200422022333-3d57cf2e726e h1:3Dzrrxi54Io7Aoyb0PYLsI47K2TxkRQg+cqUn+m04do=
521523
golang.org/x/tools v0.0.0-20200422022333-3d57cf2e726e/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
524+
golang.org/x/tools v0.0.0-20200428185508-e9a00ec82136/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
522525
golang.org/x/tools v0.0.0-20200519015757-0d0afa43d58a h1:gILuVKC+ZPD6g/tj6zBOdnOH1ZHI0zZ86+KLMogc6/s=
523526
golang.org/x/tools v0.0.0-20200519015757-0d0afa43d58a/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
524527
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
@@ -582,6 +585,8 @@ honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWh
582585
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
583586
honnef.co/go/tools v0.0.1-2020.1.4 h1:UoveltGrhghAA7ePc+e+QYDHXrBps2PqFZiHkGR/xK8=
584587
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
588+
mvdan.cc/gofumpt v0.0.0-20200513141252-abc0db2c416a h1:TTEzidAa7rn93JGy1ACigx6o9VcsRLKG7qICdErmvUs=
589+
mvdan.cc/gofumpt v0.0.0-20200513141252-abc0db2c416a/go.mod h1:4q/PlrZKQLU5MowSvCKM3U4xJUPtJ8vKWx7vsWFJ3MI=
585590
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed h1:WX1yoOaKQfddO/mLzdV4wptyWgoH/6hwLs7QHTixo0I=
586591
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed/go.mod h1:Xkxe497xwlCKkIaQYRfC7CSLworTXY9RMqwhhCm+8Nc=
587592
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b h1:DxJ5nJdkhDlLok9K6qO+5290kphDJbHOQO1DFFFTeBo=

pkg/golinters/gofumpt.go

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package golinters
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"go/token"
7+
"io/ioutil"
8+
"strings"
9+
"sync"
10+
11+
"golang.org/x/tools/go/analysis"
12+
"mvdan.cc/gofumpt/format"
13+
14+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
15+
"github.com/golangci/golangci-lint/pkg/lint/linter"
16+
"github.com/golangci/golangci-lint/pkg/result"
17+
)
18+
19+
const gofumptName = "gofumpt"
20+
21+
func NewGofumpt() *goanalysis.Linter {
22+
var mu sync.Mutex
23+
var resIssues []goanalysis.Issue
24+
25+
analyzer := &analysis.Analyzer{
26+
Name: gofumptName,
27+
Doc: goanalysis.TheOnlyanalyzerDoc,
28+
}
29+
return goanalysis.NewLinter(
30+
gofumptName,
31+
"Gofumpt checks whether code was gofumpt-ed.",
32+
[]*analysis.Analyzer{analyzer},
33+
nil,
34+
).WithContextSetter(func(lintCtx *linter.Context) {
35+
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
36+
var fileNames []string
37+
for _, f := range pass.Files {
38+
pos := pass.Fset.PositionFor(f.Pos(), false)
39+
fileNames = append(fileNames, pos.Filename)
40+
}
41+
42+
var issues []goanalysis.Issue
43+
44+
for _, f := range fileNames {
45+
input, err := ioutil.ReadFile(f)
46+
if err != nil {
47+
return nil, fmt.Errorf("unable to open file %s: %w", f, err)
48+
}
49+
output, err := format.Source(input, "")
50+
if err != nil {
51+
return nil, fmt.Errorf("error while running gofumpt: %w", err)
52+
}
53+
if !bytes.Equal(input, output) {
54+
issues = append(issues, goanalysis.NewIssue(&result.Issue{
55+
FromLinter: gofumptName,
56+
Text: "File is not `gofumpt`-ed",
57+
Pos: token.Position{
58+
Filename: f,
59+
Line: strings.Count(string(input), "\n"),
60+
},
61+
}, pass))
62+
}
63+
}
64+
65+
if len(issues) == 0 {
66+
return nil, nil
67+
}
68+
69+
mu.Lock()
70+
resIssues = append(resIssues, issues...)
71+
mu.Unlock()
72+
73+
return nil, nil
74+
}
75+
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
76+
return resIssues
77+
}).WithLoadMode(goanalysis.LoadModeSyntax)
78+
}

pkg/lint/lintersdb/manager.go

+3
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
195195
WithPresets(linter.PresetFormatting).
196196
WithAutoFix().
197197
WithURL("https://golang.org/cmd/gofmt/"),
198+
linter.NewConfig(golinters.NewGofumpt()).
199+
WithPresets(linter.PresetFormatting).
200+
WithURL("https://github.com/mvdan/gofumpt"),
198201
linter.NewConfig(golinters.NewGoimports()).
199202
WithPresets(linter.PresetFormatting).
200203
WithAutoFix().

test/testdata/gofumpt.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//args: -Egofumpt
2+
package testdata
3+
4+
import "fmt"
5+
6+
func GofumptNewLine() {
7+
8+
fmt.Println("foo")
9+
}
10+
11+
// ERROR "File is not `gofumpt`-ed"

0 commit comments

Comments
 (0)