Skip to content

Commit

Permalink
Better map ast printing
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Mar 27, 2024
1 parent 3452f5b commit d523107
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
8 changes: 7 additions & 1 deletion ast/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,11 @@ func (n *MapNode) String() string {
}

func (n *PairNode) String() string {
return fmt.Sprintf("%s: %s", n.Key.String(), n.Value.String())
if str, ok := n.Key.(*StringNode); ok {
if utils.IsValidIdentifier(str.Value) {
return fmt.Sprintf("%s: %s", str.Value, n.Value.String())
}
return fmt.Sprintf("%q: %s", str.String(), n.Value.String())
}
return fmt.Sprintf("(%s): %s", n.Key.String(), n.Value.String())
}
5 changes: 3 additions & 2 deletions ast/print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func TestPrint(t *testing.T) {
{`func(a)`, `func(a)`},
{`func(a, b)`, `func(a, b)`},
{`{}`, `{}`},
{`{a: b}`, `{"a": b}`},
{`{a: b, c: d}`, `{"a": b, "c": d}`},
{`{a: b}`, `{a: b}`},
{`{a: b, c: d}`, `{a: b, c: d}`},
{`[]`, `[]`},
{`[a]`, `[a]`},
{`[a, b]`, `[a, b]`},
Expand All @@ -71,6 +71,7 @@ func TestPrint(t *testing.T) {
{`a[1:]`, `a[1:]`},
{`a[:]`, `a[:]`},
{`(nil ?? 1) > 0`, `(nil ?? 1) > 0`},
{`{("a" + "b"): 42}`, `{("a" + "b"): 42}`},
}

for _, tt := range tests {
Expand Down

0 comments on commit d523107

Please sign in to comment.