From 91e39e7e2fa394f2b3d29cd388cd21933eac5e6d Mon Sep 17 00:00:00 2001 From: mappu Date: Thu, 9 Apr 2020 19:40:18 +1200 Subject: [PATCH] node: elide some extra parentheses for commutative binary ops (plus/concat) --- node.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/node.go b/node.go index 862ccc3..65f63c3 100644 --- a/node.go +++ b/node.go @@ -1490,6 +1490,17 @@ func (this *conversionState) convertBinaryCommon(left, right node.Node, goBinary rhs = "(" + rhs + ")" } + // We can elide even more parens in case of some commutative operators + // We can only do this in the left-associative case + // FIXME Plus and Concat aren't necessarily commutative together even though they both have + here(!!) + if _, ok := left.(*binary.Plus); ok && goBinaryOperator == `+` { + lhs = removeParens(lhs) + } + if _, ok := left.(*binary.Concat); ok && goBinaryOperator == `+` { + lhs = removeParens(lhs) + } + + // Done return "(" + lhs + " " + goBinaryOperator + " " + rhs + ")", nil }