From f12067e5bd4960fecba7f6a77543558a7ccdbd17 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Sun, 4 Dec 2022 12:47:55 -0800 Subject: [PATCH] fix unresolved scope obj setting in compiler --- d2compiler/compile.go | 5 +- d2compiler/compile_test.go | 15 ++ .../underscore_unresolved_obj.exp.json | 197 ++++++++++++++++++ 3 files changed, 213 insertions(+), 4 deletions(-) create mode 100644 testdata/d2compiler/TestCompile/underscore_unresolved_obj.exp.json diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 775ced5ec..2d2cba269 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -252,11 +252,8 @@ func (c *compiler) compileKey(obj *d2graph.Object, m *d2ast.Map, mk *d2ast.Key) c.errorf(mk.Range.Start, mk.Range.End, err.Error()) return } - if resolvedObj != obj { - obj = resolvedObj - } - parent := obj + parent := resolvedObj if len(resolvedIDA) > 0 { unresolvedObj := obj obj = parent.EnsureChild(resolvedIDA) diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index d4f979532..fa5f12f07 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -6,6 +6,7 @@ import ( "strings" "testing" + tassert "github.com/stretchr/testify/assert" "oss.terrastruct.com/util-go/assert" "oss.terrastruct.com/util-go/diff" @@ -209,6 +210,20 @@ x: { }, }, + { + name: "underscore_unresolved_obj", + + text: ` +x: { + _.y +} +`, + assertions: func(t *testing.T, g *d2graph.Graph) { + tassert.Equal(t, "y", g.Objects[1].ID) + tassert.Equal(t, g.Root.AbsID(), g.Objects[1].References[0].ScopeObj.AbsID()) + tassert.Equal(t, g.Objects[0].AbsID(), g.Objects[1].References[0].UnresolvedScopeObj.AbsID()) + }, + }, { name: "underscore_parent_not_root", diff --git a/testdata/d2compiler/TestCompile/underscore_unresolved_obj.exp.json b/testdata/d2compiler/TestCompile/underscore_unresolved_obj.exp.json new file mode 100644 index 000000000..626cc42fc --- /dev/null +++ b/testdata/d2compiler/TestCompile/underscore_unresolved_obj.exp.json @@ -0,0 +1,197 @@ +{ + "graph": { + "ast": { + "range": "d2/testdata/d2compiler/TestCompile/underscore_unresolved_obj.d2,0:0:0-4:0:13", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile/underscore_unresolved_obj.d2,1:0:1-3:1:12", + "key": { + "range": "d2/testdata/d2compiler/TestCompile/underscore_unresolved_obj.d2,1:0:1-1:1:2", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/underscore_unresolved_obj.d2,1:0:1-1:1:2", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile/underscore_unresolved_obj.d2,1:3:4-3:0:11", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile/underscore_unresolved_obj.d2,2:1:7-2:4:10", + "key": { + "range": "d2/testdata/d2compiler/TestCompile/underscore_unresolved_obj.d2,2:1:7-2:4:10", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/underscore_unresolved_obj.d2,2:1:7-2:2:8", + "value": [ + { + "string": "_", + "raw_string": "_" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/underscore_unresolved_obj.d2,2:3:9-2:4:10", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + } + ] + }, + "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": "x", + "id_val": "x", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile/underscore_unresolved_obj.d2,1:0:1-1:1:2", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/underscore_unresolved_obj.d2,1:0:1-1:1:2", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "x" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "y", + "id_val": "y", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile/underscore_unresolved_obj.d2,2:1:7-2:4:10", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/underscore_unresolved_obj.d2,2:1:7-2:2:8", + "value": [ + { + "string": "_", + "raw_string": "_" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/underscore_unresolved_obj.d2,2:3:9-2:4:10", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + } + ] + }, + "key_path_index": 1, + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "y" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + } + }, + "zIndex": 0 + } + ] + }, + "err": null +}