Skip to content

Commit 904513e

Browse files
committed
go/minformat: fix several formatting issues
Signed-off-by: Iskander Sharipov <quasilyte@gmail.com>
1 parent 7eb11a7 commit 904513e

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

go/minformat/minifier.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ func (m *minifier) printExpr(n ast.Expr) {
105105
m.out.WriteByte(']')
106106

107107
case *ast.BinaryExpr:
108-
m.printExpr(n.X)
109-
m.out.WriteString(n.Op.String())
110-
m.printExpr(n.Y)
108+
m.printBinaryExpr(n)
111109

112110
case *ast.UnaryExpr:
113111
m.out.WriteString(n.Op.String())
@@ -229,12 +227,18 @@ func (m *minifier) printStmt(n ast.Stmt) {
229227
}
230228

231229
case *ast.AssignStmt:
232-
for _, lhs := range n.Lhs {
230+
for i, lhs := range n.Lhs {
233231
m.printExpr(lhs)
232+
if i != len(n.Lhs)-1 {
233+
m.out.WriteByte(',')
234+
}
234235
}
235236
m.out.WriteString(n.Tok.String())
236-
for _, rhs := range n.Rhs {
237+
for i, rhs := range n.Rhs {
237238
m.printExpr(rhs)
239+
if i != len(n.Rhs)-1 {
240+
m.out.WriteByte(',')
241+
}
238242
}
239243

240244
case *ast.IncDecStmt:
@@ -481,6 +485,24 @@ func (m *minifier) printGenDecl(n *ast.GenDecl) {
481485
}
482486
}
483487

488+
func (m *minifier) printBinaryExpr(n *ast.BinaryExpr) {
489+
// Handle `x < -y` and `x - -y`.
490+
if n.Op == token.LSS || n.Op == token.SUB {
491+
y := leftmostExpr(n.Y)
492+
if y, ok := y.(*ast.UnaryExpr); ok && y.Op == token.SUB {
493+
m.printExpr(n.X)
494+
m.out.WriteString(n.Op.String())
495+
m.out.WriteByte(' ')
496+
m.printExpr(n.Y)
497+
return
498+
}
499+
}
500+
501+
m.printExpr(n.X)
502+
m.out.WriteString(n.Op.String())
503+
m.printExpr(n.Y)
504+
}
505+
484506
func (m *minifier) printFuncType(n *ast.FuncType) {
485507
m.out.WriteString("(")
486508
m.printFieldList(n.Params, ',')

go/minformat/minifier_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ func TestMinifyStmt(t *testing.T) {
5656
{`x ++`, `x++`},
5757
{`x [0 ] --`, `x[0]--`},
5858

59+
{`x = f()`, `x=f()`},
60+
{`x, y = f()`, `x,y=f()`},
61+
{`x, y = a, b`, `x,y=a,b`},
62+
5963
{`{ }`, `{}`},
6064
{`{ 1; }`, `{1}`},
6165
{`{ 1; 2 }`, `{1;2}`},
@@ -194,6 +198,13 @@ func TestMinifyExpr(t *testing.T) {
194198
{`interface{ foo(); bar() }`, `interface{foo();bar()}`},
195199
{`interface{ foo(int); bar() (int, error) }`, `interface{foo(int);bar()(int,error)}`},
196200
{`interface { Embedded; foo() }`, `interface{Embedded;foo()}`},
201+
202+
// We need a space between `<` and `-`; otherwise it'll be parsed differently.
203+
{`x < -y`, `x< -y`},
204+
{`d < -35*Second`, `d< -35*Second`},
205+
206+
// We need a space between `-` and another `-`; otherwise it'll be parsed differently.
207+
{`x - -1`, `x- -1`},
197208
}
198209

199210
var m minifier

go/minformat/utils.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package minformat
2+
3+
import (
4+
"go/ast"
5+
)
6+
7+
func leftmostExpr(n ast.Expr) ast.Expr {
8+
switch n := n.(type) {
9+
case *ast.BinaryExpr:
10+
return leftmostExpr(n.X)
11+
default:
12+
return n
13+
}
14+
}

0 commit comments

Comments
 (0)