From 59a9ea814fb92b730346480de1f628e7cd68d724 Mon Sep 17 00:00:00 2001 From: Taco de Wolff Date: Fri, 26 May 2023 18:03:05 -0400 Subject: [PATCH] JS: add space between ---a when calling JS(), fixes #112 --- js/ast.go | 4 ++-- js/js_test.go | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/js/ast.go b/js/ast.go index 92e80d7..9de7556 100644 --- a/js/ast.go +++ b/js/ast.go @@ -3569,7 +3569,7 @@ func (n UnaryExpr) String() string { func (n UnaryExpr) JS() string { if n.Op == PostIncrToken || n.Op == PostDecrToken { return n.X.JS() + n.Op.String() - } else if IsIdentifierName(n.Op) { + } else if unary, ok := n.X.(*UnaryExpr); ok && (n.Op == PosToken && unary.Op == PreIncrToken || n.Op == NegToken && unary.Op == PreDecrToken) || IsIdentifierName(n.Op) { return n.Op.String() + " " + n.X.JS() } return n.Op.String() + n.X.JS() @@ -3587,7 +3587,7 @@ func (n UnaryExpr) JSWriteTo(w io.Writer) (i int, err error) { wn, err = w.Write(n.Op.Bytes()) i += wn return - } else if IsIdentifierName(n.Op) { + } else if unary, ok := n.X.(UnaryExpr); ok && (n.Op == PosToken && unary.Op == PreIncrToken || n.Op == NegToken && unary.Op == PreDecrToken) || IsIdentifierName(n.Op) { wn, err = w.Write(n.Op.Bytes()) i += wn if err != nil { diff --git a/js/js_test.go b/js/js_test.go index 564087b..278ed81 100644 --- a/js/js_test.go +++ b/js/js_test.go @@ -182,7 +182,8 @@ func TestJS(t *testing.T) { {"x = y?.z;", "x = y?.z; "}, // UnaryExpr - {"x = 1 + 1;", "x = 1 + 1; "}, + {"x = -a;", "x = -a; "}, + {"x = - --a;", "x = - --a; "}, // BinaryExpr {"a << b;", "a << b; "},