Skip to content

Commit

Permalink
Fix ast printing of ?.[
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Feb 12, 2024
1 parent 84ac0b8 commit ffaec9e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ast/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (n *MemberNode) String() string {
if str, ok := n.Property.(*StringNode); ok && utils.IsValidIdentifier(str.Value) {
return fmt.Sprintf("%s?.%s", n.Node.String(), str.Value)
} else {
return fmt.Sprintf("get(%s, %s)", n.Node.String(), n.Property.String())
return fmt.Sprintf("%s?.[%s]", n.Node.String(), n.Property.String())
}
}
if str, ok := n.Property.(*StringNode); ok && utils.IsValidIdentifier(str.Value) {
Expand Down
4 changes: 3 additions & 1 deletion ast/print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func TestPrint(t *testing.T) {
{`a["the b"]`, `a["the b"]`},
{`a.b[0]`, `a.b[0]`},
{`a?.b`, `a?.b`},
{`x[0][1]`, `x[0][1]`},
{`x?.[0]?.[1]`, `x?.[0]?.[1]`},
{`-a`, `-a`},
{`!a`, `!a`},
{`not a`, `not a`},
Expand Down Expand Up @@ -88,7 +90,7 @@ func TestPrint_MemberNode(t *testing.T) {
Property: &ast.StringNode{Value: "b c"},
Optional: true,
}
require.Equal(t, `get(a, "b c")`, node.String())
require.Equal(t, `a?.["b c"]`, node.String())
}

func TestPrint_ConstantNode(t *testing.T) {
Expand Down

0 comments on commit ffaec9e

Please sign in to comment.