Skip to content

Commit 9713880

Browse files
authored
Fix unary node print for conditional expr (#676)
* feat: extract code for compiling equal operator * fix: fix unary node inside condition node print
1 parent b24c7f3 commit 9713880

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

ast/print.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,16 @@ func (n *ConstantNode) String() string {
4545
}
4646

4747
func (n *UnaryNode) String() string {
48-
op := ""
48+
op := n.Operator
4949
if n.Operator == "not" {
5050
op = fmt.Sprintf("%s ", n.Operator)
51-
} else {
52-
op = fmt.Sprintf("%s", n.Operator)
5351
}
54-
if _, ok := n.Node.(*BinaryNode); ok {
52+
wrap := false
53+
switch n.Node.(type) {
54+
case *BinaryNode, *ConditionalNode:
55+
wrap = true
56+
}
57+
if wrap {
5558
return fmt.Sprintf("%s(%s)", op, n.Node.String())
5659
}
5760
return fmt.Sprintf("%s%s", op, n.Node.String())

ast/print_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func TestPrint(t *testing.T) {
7777
{`(nil ?? 1) > 0`, `(nil ?? 1) > 0`},
7878
{`{("a" + "b"): 42}`, `{("a" + "b"): 42}`},
7979
{`(One == 1 ? true : false) && Two == 2`, `(One == 1 ? true : false) && Two == 2`},
80+
{`not (a == 1 ? b > 1 : b < 2)`, `not (a == 1 ? b > 1 : b < 2)`},
8081
}
8182

8283
for _, tt := range tests {

0 commit comments

Comments
 (0)