diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index bbcda170f..150fc23d1 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -31,3 +31,4 @@ Hope everyone is enjoying the holidays this week! - Connections at the boundaries no longer get part of its stroke clipped. [#493](https://github.com/terrastruct/d2/pull/493) - Fixes edge case where `style` being defined in same scope as `sql_table` causes compiler to skip compiling `sql_table`. [#506](https://github.com/terrastruct/d2/issues/506) - Fixes panic passing a non-string value to `constraint`. [#248](https://github.com/terrastruct/d2/issues/248) +- Fixes edge case where the key `null` was compiling wrongly. [#507](https://github.com/terrastruct/d2/issues/507) diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index d6459f34d..29d99cee0 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -1672,6 +1672,16 @@ choo: { } }, }, + { + name: "null", + + text: `null +`, + assertions: func(t *testing.T, g *d2graph.Graph) { + tassert.Equal(t, "'null'", g.Objects[0].ID) + tassert.Equal(t, "null", g.Objects[0].IDVal) + }, + }, { name: "sql-regression", diff --git a/d2format/escape.go b/d2format/escape.go index b6267ea80..9789a9c2d 100644 --- a/d2format/escape.go +++ b/d2format/escape.go @@ -48,7 +48,7 @@ func escapeUnquotedValue(s string, inKey bool) string { } if strings.EqualFold(s, "null") { - return "\\null" + return `'null'` } var b strings.Builder diff --git a/d2format/escape_test.go b/d2format/escape_test.go index c0090c03f..b7e67837c 100644 --- a/d2format/escape_test.go +++ b/d2format/escape_test.go @@ -170,7 +170,7 @@ func TestEscapeUnquoted(t *testing.T) { { name: "null", str: `null`, - exp: `\null`, + exp: `'null'`, }, { name: "empty", diff --git a/testdata/d2compiler/TestCompile/null.exp.json b/testdata/d2compiler/TestCompile/null.exp.json new file mode 100644 index 000000000..f38e28de6 --- /dev/null +++ b/testdata/d2compiler/TestCompile/null.exp.json @@ -0,0 +1,102 @@ +{ + "graph": { + "ast": { + "range": "d2/testdata/d2compiler/TestCompile/null.d2,0:0:0-1:0:5", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile/null.d2,0:0:0-0:4:4", + "key": { + "range": "d2/testdata/d2compiler/TestCompile/null.d2,0:0:0-0:4:4", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/null.d2,0:0:0-0:4:4", + "value": [ + { + "string": "null", + "raw_string": "null" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + ] + }, + "root": { + "id": "", + "id_val": "", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "attributes": { + "label": { + "value": "" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + } + }, + "zIndex": 0 + }, + "edges": null, + "objects": [ + { + "id": "'null'", + "id_val": "null", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile/null.d2,0:0:0-0:4:4", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/null.d2,0:0:0-0:4:4", + "value": [ + { + "string": "null", + "raw_string": "null" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "null" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + } + }, + "zIndex": 0 + } + ] + }, + "err": null +}