From d608740b1c98a8d0a38838e5e6a55aa6f8d8687e Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Sat, 13 Jul 2024 17:07:09 -0600 Subject: [PATCH] d2compiler: fix underscore sibling edge case --- d2compiler/compile_test.go | 5 + d2ir/compile.go | 3 + .../import-link-underscore.exp.json | 347 +++++++++++++++++- 3 files changed, 346 insertions(+), 9 deletions(-) diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 37974294e7..8290f40fdb 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -2933,6 +2933,7 @@ layers: { layers: { b: { d.link: _ + s.link: _.layers.k layers: { c: { @@ -2942,6 +2943,9 @@ layers: { } } } + k: { + k + } }`, }, assertions: func(t *testing.T, g *d2graph.Graph) { @@ -2949,6 +2953,7 @@ layers: { tassert.Equal(t, "root.layers.x.layers.b", g.Layers[0].Layers[0].Layers[0].Objects[0].Link.Value) tassert.Equal(t, "root.layers.x", g.Layers[0].Layers[0].Layers[0].Objects[1].Link.Value) tassert.Equal(t, "root.layers.x.layers.b", g.Layers[0].Layers[0].Layers[0].Objects[2].Link.Value) + tassert.Equal(t, "root.layers.x.layers.k", g.Layers[0].Layers[0].Objects[1].Link.Value) }, }, { diff --git a/d2ir/compile.go b/d2ir/compile.go index 2c5326c570..9842d42959 100644 --- a/d2ir/compile.go +++ b/d2ir/compile.go @@ -873,6 +873,9 @@ func (c *compiler) updateLinks(m *Map) { prependIDA := aida[:len(aida)-len(bida)] if uplevels > 0 { prependIDA = prependIDA[:len(prependIDA)-uplevels] + } else if uplevels == 0 { + // It's a sibling, so we go up one level to find it + prependIDA = prependIDA[:len(prependIDA)-2] } fullIDA := []string{"root"} // With nested imports, a value may already have been updated with part of the absolute path diff --git a/testdata/d2compiler/TestCompile/import-link-underscore.exp.json b/testdata/d2compiler/TestCompile/import-link-underscore.exp.json index 8d75e03f29..c43d23aa7e 100644 --- a/testdata/d2compiler/TestCompile/import-link-underscore.exp.json +++ b/testdata/d2compiler/TestCompile/import-link-underscore.exp.json @@ -313,6 +313,65 @@ } } }, + { + "map_key": { + "range": ",0:0:0-0:0:0", + "key": { + "range": ",0:0:0-0:0:0", + "path": [ + { + "unquoted_string": { + "range": ",0:0:0-0:0:0", + "value": [ + { + "string": "s" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": ",0:0:0-1:0:0", + "nodes": [ + { + "map_key": { + "range": ",0:0:0-0:0:0", + "key": { + "range": ",0:0:0-0:0:0", + "path": [ + { + "unquoted_string": { + "range": ",0:0:0-0:0:0", + "value": [ + { + "string": "link" + } + ] + } + } + ] + }, + "primary": { + "unquoted_string": { + "range": ",0:0:0-0:0:0", + "value": [ + { + "string": "root.layers.x.layers.k" + } + ] + } + }, + "value": {} + } + } + ] + } + } + } + }, { "map_key": { "range": ",0:0:0-0:0:0", @@ -550,6 +609,56 @@ } } } + }, + { + "map_key": { + "range": ",0:0:0-0:0:0", + "key": { + "range": ",0:0:0-0:0:0", + "path": [ + { + "unquoted_string": { + "range": ",0:0:0-0:0:0", + "value": [ + { + "string": "k" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": ",0:0:0-1:0:0", + "nodes": [ + { + "map_key": { + "range": ",0:0:0-0:0:0", + "key": { + "range": ",0:0:0-0:0:0", + "path": [ + { + "unquoted_string": { + "range": ",0:0:0-0:0:0", + "value": [ + { + "string": "k" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + ] + } + } + } } ] } @@ -695,6 +804,65 @@ } } }, + { + "map_key": { + "range": ",0:0:0-0:0:0", + "key": { + "range": ",0:0:0-0:0:0", + "path": [ + { + "unquoted_string": { + "range": ",0:0:0-0:0:0", + "value": [ + { + "string": "s" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": ",0:0:0-1:0:0", + "nodes": [ + { + "map_key": { + "range": ",0:0:0-0:0:0", + "key": { + "range": ",0:0:0-0:0:0", + "path": [ + { + "unquoted_string": { + "range": ",0:0:0-0:0:0", + "value": [ + { + "string": "link" + } + ] + } + } + ] + }, + "primary": { + "unquoted_string": { + "range": ",0:0:0-0:0:0", + "value": [ + { + "string": "root.layers.x.layers.k" + } + ] + } + }, + "value": {} + } + } + ] + } + } + } + }, { "map_key": { "range": ",0:0:0-0:0:0", @@ -1013,6 +1181,65 @@ "constraint": null }, "zIndex": 0 + }, + { + "id": "s", + "id_val": "s", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile/x.d2,4:2:35-4:8:41", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/x.d2,4:2:35-4:3:36", + "value": [ + { + "string": "s", + "raw_string": "s" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/x.d2,4:4:37-4:8:41", + "value": [ + { + "string": "link", + "raw_string": "link" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "s" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "link": { + "value": "root.layers.x.layers.k" + }, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 } ], "layers": [ @@ -1232,11 +1459,11 @@ "references": [ { "key": { - "range": "d2/testdata/d2compiler/TestCompile/x.d2,7:8:67-7:14:73", + "range": "d2/testdata/d2compiler/TestCompile/x.d2,8:8:88-8:14:94", "path": [ { "unquoted_string": { - "range": "d2/testdata/d2compiler/TestCompile/x.d2,7:8:67-7:9:68", + "range": "d2/testdata/d2compiler/TestCompile/x.d2,8:8:88-8:9:89", "value": [ { "string": "c", @@ -1247,7 +1474,7 @@ }, { "unquoted_string": { - "range": "d2/testdata/d2compiler/TestCompile/x.d2,7:10:69-7:14:73", + "range": "d2/testdata/d2compiler/TestCompile/x.d2,8:10:90-8:14:94", "value": [ { "string": "link", @@ -1291,11 +1518,11 @@ "references": [ { "key": { - "range": "d2/testdata/d2compiler/TestCompile/x.d2,8:4:81-8:10:87", + "range": "d2/testdata/d2compiler/TestCompile/x.d2,9:4:102-9:10:108", "path": [ { "unquoted_string": { - "range": "d2/testdata/d2compiler/TestCompile/x.d2,8:4:81-8:5:82", + "range": "d2/testdata/d2compiler/TestCompile/x.d2,9:4:102-9:5:103", "value": [ { "string": "z", @@ -1306,7 +1533,7 @@ }, { "unquoted_string": { - "range": "d2/testdata/d2compiler/TestCompile/x.d2,8:6:83-8:10:87", + "range": "d2/testdata/d2compiler/TestCompile/x.d2,9:6:104-9:10:108", "value": [ { "string": "link", @@ -1350,11 +1577,11 @@ "references": [ { "key": { - "range": "d2/testdata/d2compiler/TestCompile/x.d2,9:4:97-9:10:103", + "range": "d2/testdata/d2compiler/TestCompile/x.d2,10:4:118-10:10:124", "path": [ { "unquoted_string": { - "range": "d2/testdata/d2compiler/TestCompile/x.d2,9:4:97-9:5:98", + "range": "d2/testdata/d2compiler/TestCompile/x.d2,10:4:118-10:5:119", "value": [ { "string": "f", @@ -1365,7 +1592,7 @@ }, { "unquoted_string": { - "range": "d2/testdata/d2compiler/TestCompile/x.d2,9:6:99-9:10:103", + "range": "d2/testdata/d2compiler/TestCompile/x.d2,10:6:120-10:10:124", "value": [ { "string": "link", @@ -1406,6 +1633,108 @@ ] } ] + }, + { + "name": "k", + "isFolderOnly": false, + "ast": { + "range": ",0:0:0-1:0:0", + "nodes": [ + { + "map_key": { + "range": ",0:0:0-0:0:0", + "key": { + "range": ",0:0:0-0:0:0", + "path": [ + { + "unquoted_string": { + "range": ",0:0:0-0:0:0", + "value": [ + { + "string": "k" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + ] + }, + "root": { + "id": "", + "id_val": "", + "attributes": { + "label": { + "value": "" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + }, + "edges": null, + "objects": [ + { + "id": "k", + "id_val": "k", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile/x.d2,15:4:168-15:5:169", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/x.d2,15:4:168-15:5:169", + "value": [ + { + "string": "k", + "raw_string": "k" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "k" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + } + ] } ] }