Skip to content

Commit ffaec9e

Browse files
committedFeb 12, 2024
Fix ast printing of ?.[
1 parent 84ac0b8 commit ffaec9e

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed
 

‎ast/print.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (n *MemberNode) String() string {
112112
if str, ok := n.Property.(*StringNode); ok && utils.IsValidIdentifier(str.Value) {
113113
return fmt.Sprintf("%s?.%s", n.Node.String(), str.Value)
114114
} else {
115-
return fmt.Sprintf("get(%s, %s)", n.Node.String(), n.Property.String())
115+
return fmt.Sprintf("%s?.[%s]", n.Node.String(), n.Property.String())
116116
}
117117
}
118118
if str, ok := n.Property.(*StringNode); ok && utils.IsValidIdentifier(str.Value) {

‎ast/print_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ func TestPrint(t *testing.T) {
2828
{`a["the b"]`, `a["the b"]`},
2929
{`a.b[0]`, `a.b[0]`},
3030
{`a?.b`, `a?.b`},
31+
{`x[0][1]`, `x[0][1]`},
32+
{`x?.[0]?.[1]`, `x?.[0]?.[1]`},
3133
{`-a`, `-a`},
3234
{`!a`, `!a`},
3335
{`not a`, `not a`},
@@ -88,7 +90,7 @@ func TestPrint_MemberNode(t *testing.T) {
8890
Property: &ast.StringNode{Value: "b c"},
8991
Optional: true,
9092
}
91-
require.Equal(t, `get(a, "b c")`, node.String())
93+
require.Equal(t, `a?.["b c"]`, node.String())
9294
}
9395

9496
func TestPrint_ConstantNode(t *testing.T) {

0 commit comments

Comments
 (0)
Please sign in to comment.