Skip to content

Commit d523107

Browse files
committed
Better map ast printing
1 parent 3452f5b commit d523107

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

ast/print.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -202,5 +202,11 @@ func (n *MapNode) String() string {
202202
}
203203

204204
func (n *PairNode) String() string {
205-
return fmt.Sprintf("%s: %s", n.Key.String(), n.Value.String())
205+
if str, ok := n.Key.(*StringNode); ok {
206+
if utils.IsValidIdentifier(str.Value) {
207+
return fmt.Sprintf("%s: %s", str.Value, n.Value.String())
208+
}
209+
return fmt.Sprintf("%q: %s", str.String(), n.Value.String())
210+
}
211+
return fmt.Sprintf("(%s): %s", n.Key.String(), n.Value.String())
206212
}

ast/print_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ func TestPrint(t *testing.T) {
5555
{`func(a)`, `func(a)`},
5656
{`func(a, b)`, `func(a, b)`},
5757
{`{}`, `{}`},
58-
{`{a: b}`, `{"a": b}`},
59-
{`{a: b, c: d}`, `{"a": b, "c": d}`},
58+
{`{a: b}`, `{a: b}`},
59+
{`{a: b, c: d}`, `{a: b, c: d}`},
6060
{`[]`, `[]`},
6161
{`[a]`, `[a]`},
6262
{`[a, b]`, `[a, b]`},
@@ -71,6 +71,7 @@ func TestPrint(t *testing.T) {
7171
{`a[1:]`, `a[1:]`},
7272
{`a[:]`, `a[:]`},
7373
{`(nil ?? 1) > 0`, `(nil ?? 1) > 0`},
74+
{`{("a" + "b"): 42}`, `{("a" + "b"): 42}`},
7475
}
7576

7677
for _, tt := range tests {

0 commit comments

Comments
 (0)