Skip to content

Commit 6157395

Browse files
committed
Fix printing on conditional/binary nodes
Fixes #613
1 parent 5804ccb commit 6157395

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

ast/print.go

+18-9
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ func (n *BinaryNode) String() string {
6565
var lhs, rhs string
6666
var lwrap, rwrap bool
6767

68-
lb, ok := n.Left.(*BinaryNode)
69-
if ok {
68+
if lb, ok := n.Left.(*BinaryNode); ok {
7069
if operator.Less(lb.Operator, n.Operator) {
7170
lwrap = true
7271
}
@@ -77,9 +76,7 @@ func (n *BinaryNode) String() string {
7776
lwrap = true
7877
}
7978
}
80-
81-
rb, ok := n.Right.(*BinaryNode)
82-
if ok {
79+
if rb, ok := n.Right.(*BinaryNode); ok {
8380
if operator.Less(rb.Operator, n.Operator) {
8481
rwrap = true
8582
}
@@ -88,6 +85,13 @@ func (n *BinaryNode) String() string {
8885
}
8986
}
9087

88+
if _, ok := n.Left.(*ConditionalNode); ok {
89+
lwrap = true
90+
}
91+
if _, ok := n.Right.(*ConditionalNode); ok {
92+
rwrap = true
93+
}
94+
9195
if lwrap {
9296
lhs = fmt.Sprintf("(%s)", n.Left.String())
9397
} else {
@@ -108,20 +112,25 @@ func (n *ChainNode) String() string {
108112
}
109113

110114
func (n *MemberNode) String() string {
115+
node := n.Node.String()
116+
if _, ok := n.Node.(*BinaryNode); ok {
117+
node = fmt.Sprintf("(%s)", node)
118+
}
119+
111120
if n.Optional {
112121
if str, ok := n.Property.(*StringNode); ok && utils.IsValidIdentifier(str.Value) {
113-
return fmt.Sprintf("%s?.%s", n.Node.String(), str.Value)
122+
return fmt.Sprintf("%s?.%s", node, str.Value)
114123
} else {
115-
return fmt.Sprintf("%s?.[%s]", n.Node.String(), n.Property.String())
124+
return fmt.Sprintf("%s?.[%s]", node, n.Property.String())
116125
}
117126
}
118127
if str, ok := n.Property.(*StringNode); ok && utils.IsValidIdentifier(str.Value) {
119128
if _, ok := n.Node.(*PointerNode); ok {
120129
return fmt.Sprintf(".%s", str.Value)
121130
}
122-
return fmt.Sprintf("%s.%s", n.Node.String(), str.Value)
131+
return fmt.Sprintf("%s.%s", node, str.Value)
123132
}
124-
return fmt.Sprintf("%s[%s]", n.Node.String(), n.Property.String())
133+
return fmt.Sprintf("%s[%s]", node, n.Property.String())
125134
}
126135

127136
func (n *SliceNode) String() string {

ast/print_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ func TestPrint(t *testing.T) {
7272
{`a[:]`, `a[:]`},
7373
{`(nil ?? 1) > 0`, `(nil ?? 1) > 0`},
7474
{`{("a" + "b"): 42}`, `{("a" + "b"): 42}`},
75+
{`(One == 1 ? true : false) && Two == 2`, `(One == 1 ? true : false) && Two == 2`},
7576
}
7677

7778
for _, tt := range tests {

expr_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,15 @@ func TestExpr(t *testing.T) {
12981298
require.NoError(t, err, "eval")
12991299
assert.Equal(t, tt.want, got, "eval")
13001300
}
1301+
{
1302+
program, err := expr.Compile(tt.code, expr.Env(mock.Env{}), expr.Optimize(false))
1303+
require.NoError(t, err)
1304+
1305+
code := program.Node().String()
1306+
got, err := expr.Eval(code, env)
1307+
require.NoError(t, err, code)
1308+
assert.Equal(t, tt.want, got, code)
1309+
}
13011310
})
13021311
}
13031312
}

0 commit comments

Comments
 (0)