Skip to content

Commit 0b1e1fa

Browse files
committed
feat: integrate gofump
Signed-off-by: Ilya Lesikov <ilya@lesikov.com>
1 parent 2e1c237 commit 0b1e1fa

6 files changed

Lines changed: 80 additions & 39 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Wormatter
22

3-
A DST-based Go source code formatter that enforces a specific declaration ordering and formatting style.
3+
A DST-based Go source code formatter. Highly opinionated, but very comprehensive. Gofumpt built-in.
44

55
## Installation
66

Taskfile.yaml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@ tasks:
2828
cmds:
2929
- go test -v ./...
3030

31-
format:
32-
desc: Format all Go files using wormatter itself
33-
deps: [build]
34-
cmds:
35-
- ./bin/{{.BINARY_NAME}} ./cmd
36-
- ./bin/{{.BINARY_NAME}} ./pkg
37-
3831
clean:
3932
desc: Remove build artifacts
4033
cmds:

go.mod

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ go 1.25
55
require (
66
github.com/dave/dst v0.27.3
77
github.com/samber/lo v1.52.0
8+
golang.org/x/mod v0.31.0
89
gonum.org/v1/gonum v0.16.0
10+
mvdan.cc/gofumpt v0.9.2
911
)
1012

1113
require (
14+
github.com/google/go-cmp v0.7.0 // indirect
1215
github.com/sergi/go-diff v1.4.0 // indirect
13-
golang.org/x/mod v0.21.0 // indirect
14-
golang.org/x/sync v0.12.0 // indirect
16+
golang.org/x/sync v0.18.0 // indirect
1517
golang.org/x/text v0.23.0 // indirect
16-
golang.org/x/tools v0.26.0 // indirect
18+
golang.org/x/tools v0.39.0 // indirect
1719
)

go.sum

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,39 @@ github.com/dave/jennifer v1.5.0 h1:HmgPN93bVDpkQyYbqhCHj5QlgvUkvEOzMyEvKLgCRrg=
44
github.com/dave/jennifer v1.5.0/go.mod h1:4MnyiFIlZS3l5tSDn8VnzE6ffAhYBMB2SZntBsZGUok=
55
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
66
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7+
github.com/go-quicktest/qt v1.101.0 h1:O1K29Txy5P2OK0dGo59b7b0LR6wKfIhttaAhHUyn7eI=
8+
github.com/go-quicktest/qt v1.101.0/go.mod h1:14Bz/f7NwaXPtdYEgzsx46kqSxVwTbzVZsDC26tQJow=
9+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
10+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
711
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
12+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
13+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
814
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
915
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
16+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
17+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
1018
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
19+
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
20+
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
1121
github.com/samber/lo v1.52.0 h1:Rvi+3BFHES3A8meP33VPAxiBZX/Aws5RxrschYGjomw=
1222
github.com/samber/lo v1.52.0/go.mod h1:4+MXEGsJzbKGaUEQFKBq2xtfuznW9oz/WrgyzMzRoM0=
1323
github.com/sergi/go-diff v1.4.0 h1:n/SP9D5ad1fORl+llWyN+D6qoUETXNZARKjyY2/KVCw=
1424
github.com/sergi/go-diff v1.4.0/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
1525
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1626
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
17-
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
18-
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
19-
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
20-
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
27+
golang.org/x/mod v0.31.0 h1:HaW9xtz0+kOcWKwli0ZXy79Ix+UW/vOfmWI5QVd2tgI=
28+
golang.org/x/mod v0.31.0/go.mod h1:43JraMp9cGx1Rx3AqioxrbrhNsLl2l/iNAvuBkrezpg=
29+
golang.org/x/sync v0.18.0 h1:kr88TuHDroi+UVf+0hZnirlk8o8T+4MrK6mr60WkH/I=
30+
golang.org/x/sync v0.18.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
2131
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
2232
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
23-
golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ=
24-
golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0=
33+
golang.org/x/tools v0.39.0 h1:ik4ho21kwuQln40uelmciQPp9SipgNDdrafrYA4TmQQ=
34+
golang.org/x/tools v0.39.0/go.mod h1:JnefbkDPyD8UU2kI5fuf8ZX4/yUeh9W877ZeBONxUqQ=
2535
gonum.org/v1/gonum v0.16.0 h1:5+ul4Swaf3ESvrOnidPp4GZbzf0mxVQpDCYUQE7OJfk=
2636
gonum.org/v1/gonum v0.16.0/go.mod h1:fef3am4MQ93R2HHpKnLk4/Tbh/s0+wqD5nfa6Pnwy4E=
2737
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2838
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2939
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
3040
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
41+
mvdan.cc/gofumpt v0.9.2 h1:zsEMWL8SVKGHNztrx6uZrXdp7AX8r421Vvp23sz7ik4=
42+
mvdan.cc/gofumpt v0.9.2/go.mod h1:iB7Hn+ai8lPvofHd9ZFGVg2GOr8sBUw1QUWjNbmIL/s=

pkg/formatter/formatter.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ import (
1414
"github.com/dave/dst"
1515
"github.com/dave/dst/decorator"
1616
"github.com/samber/lo"
17+
"golang.org/x/mod/modfile"
1718
"gonum.org/v1/gonum/graph/simple"
1819
"gonum.org/v1/gonum/graph/topo"
20+
"mvdan.cc/gofumpt/format"
1921
)
2022

2123
type declCollector struct {
@@ -164,7 +166,15 @@ func FormatFile(filePath string) error {
164166
return err
165167
}
166168

167-
return os.WriteFile(filePath, buf.Bytes(), 0644)
169+
formatted, err := format.Source(buf.Bytes(), format.Options{
170+
LangVersion: detectGoVersion(filePath),
171+
ExtraRules: true,
172+
})
173+
if err != nil {
174+
return err
175+
}
176+
177+
return os.WriteFile(filePath, formatted, 0644)
168178
}
169179

170180
func reorderDeclarations(f *dst.File) []dst.Decl {
@@ -825,6 +835,24 @@ func buildCallGraph(funcs []*dst.FuncDecl, localFuncs map[string]bool) map[strin
825835
return graph
826836
}
827837

838+
func detectGoVersion(filePath string) string {
839+
dir := filepath.Dir(filePath)
840+
for {
841+
goModPath := filepath.Join(dir, "go.mod")
842+
if data, err := os.ReadFile(goModPath); err == nil {
843+
if mf, err := modfile.Parse(goModPath, data, nil); err == nil && mf.Go != nil {
844+
return "go" + mf.Go.Version
845+
}
846+
}
847+
parent := filepath.Dir(dir)
848+
if parent == dir {
849+
break
850+
}
851+
dir = parent
852+
}
853+
return ""
854+
}
855+
828856
func containsIota(expr dst.Expr) bool {
829857
switch e := expr.(type) {
830858
case *dst.Ident:

pkg/formatter/testdata/expected.go

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,23 @@ func NewServer() *Server {
8080
}
8181

8282
func NewServerWithOptions(host string, port int) *Server {
83-
return &Server{Host: host, port: port}
83+
return &Server{
84+
Host: host, port: port,
85+
}
8486
}
8587

86-
func (s *Server) PublicMethod() {}
87-
8888
func (s *Server) AnotherPublic() {
8989
fmt.Println("another")
9090
}
9191

92+
func (s *Server) PublicMethod() {}
93+
94+
func (s *Server) handleRequest() {}
95+
9296
func (s *Server) privateMethod() {
9397
return
9498
}
9599

96-
func (s *Server) handleRequest() {}
97-
98100
type Client struct {
99101
URL string
100102

@@ -132,7 +134,9 @@ func NewConfig() Config {
132134
}
133135

134136
func NewConfigWithDefaults() *Config {
135-
return &Config{Timeout: 30, Verbose: true, debug: false, name: "default"}
137+
return &Config{
138+
Timeout: 30, Verbose: true, debug: false, name: "default",
139+
}
136140
}
137141

138142
type Empty struct{}
@@ -173,16 +177,23 @@ func ProcessDataPublic(data string) string {
173177
return strings.ToLower(data)
174178
}
175179

176-
func helperLower() {
177-
fmt.Println("helper")
180+
func createMixed() *Mixed {
181+
return &Mixed{
182+
Address: "addr", Name: "test", age: 25, count: 1,
183+
}
178184
}
179185

180-
func processData(data string) string {
181-
return strings.ToUpper(data)
186+
func functionWithEarlyReturn(x int) int {
187+
if x < 0 {
188+
return 0
189+
}
190+
y := x * 2
191+
192+
return y
182193
}
183194

184-
func createMixed() *Mixed {
185-
return &Mixed{Address: "addr", Name: "test", age: 25, count: 1}
195+
func functionWithOnlyReturn() int {
196+
return 42
186197
}
187198

188199
func functionWithReturn() int {
@@ -192,17 +203,12 @@ func functionWithReturn() int {
192203
return x + y
193204
}
194205

195-
func functionWithEarlyReturn(x int) int {
196-
if x < 0 {
197-
return 0
198-
}
199-
y := x * 2
200-
201-
return y
206+
func helperLower() {
207+
fmt.Println("helper")
202208
}
203209

204-
func functionWithOnlyReturn() int {
205-
return 42
210+
func processData(data string) string {
211+
return strings.ToUpper(data)
206212
}
207213

208214
func standaloneHelper() {}

0 commit comments

Comments
 (0)