diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 05d05786d..9de5f12c8 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -189,7 +189,7 @@ func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) { } } scopeObjIDA := d2ir.IDA(fr.Context.ScopeMap) - scopeObj, _ := obj.Graph.Root.HasChildIDVal(scopeObjIDA) + scopeObj := obj.Graph.Root.EnsureChildIDVal(scopeObjIDA) obj.References = append(obj.References, d2graph.Reference{ Key: fr.KeyPath, KeyPathIndex: fr.KeyPathIndex(), @@ -427,7 +427,7 @@ func (c *compiler) compileEdge(obj *d2graph.Object, e *d2ir.Edge) { edge.Attributes.Label.MapKey = e.LastPrimaryKey() for _, er := range e.References { scopeObjIDA := d2ir.IDA(er.Context.ScopeMap) - scopeObj, _ := edge.Src.Graph.Root.HasChildIDVal(scopeObjIDA) + scopeObj := edge.Src.Graph.Root.EnsureChildIDVal(scopeObjIDA) edge.References = append(edge.References, d2graph.EdgeReference{ Edge: er.Context.Edge, MapKey: er.Context.Key, diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 3e2e6dc53..52da3f916 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -1378,6 +1378,21 @@ x -> y: { } }, }, + { + name: "nil_scope_obj_regression", + + text: `a +b: { + _.a +} +`, + assertions: func(t *testing.T, g *d2graph.Graph) { + tassert.Equal(t, "a", g.Objects[0].ID) + for _, ref := range g.Objects[0].References { + tassert.NotNil(t, ref.ScopeObj) + } + }, + }, { name: "path_link", diff --git a/d2graph/d2graph.go b/d2graph/d2graph.go index ae3bfc74d..cf98b5c79 100644 --- a/d2graph/d2graph.go +++ b/d2graph/d2graph.go @@ -560,21 +560,31 @@ func (obj *Object) HasChild(ids []string) (*Object, bool) { return child, true } -// Keep in sync with HasChild. -func (obj *Object) HasChildIDVal(ids []string) (*Object, bool) { +// Keep in sync with EnsureChild. +func (obj *Object) EnsureChildIDVal(ids []string) *Object { if len(ids) == 0 { - return obj, true + return obj } if len(ids) == 1 && ids[0] != "style" { _, ok := ReservedKeywords[ids[0]] if ok { - return obj, true + return obj } } id := ids[0] ids = ids[1:] + // Any IDA with layers.layer or whatever is an IR IDA. + // Such IDA's are resolved from our board root. + // See https://github.com/terrastruct/d2/pull/876 + if _, ok := BoardKeywords[id]; ok { + if len(ids) == 0 { + return nil + } + return obj.EnsureChildIDVal(ids[1:]) + } + var child *Object for _, ch2 := range obj.ChildrenArray { if ch2.IDVal == id { @@ -583,13 +593,13 @@ func (obj *Object) HasChildIDVal(ids []string) (*Object, bool) { } } if child == nil { - return nil, false + child = obj.newObject(id) } if len(ids) >= 1 { - return child.HasChildIDVal(ids) + return child.EnsureChildIDVal(ids) } - return child, true + return child } func (obj *Object) HasEdge(mk *d2ast.Key) (*Edge, bool) { diff --git a/d2ir/d2ir.go b/d2ir/d2ir.go index 9f37917e3..b105e4e72 100644 --- a/d2ir/d2ir.go +++ b/d2ir/d2ir.go @@ -1027,11 +1027,12 @@ func parentPrimaryKey(n Node) *d2ast.Key { return nil } +// IDA returns the absolute path to n from the nearest board root. func IDA(n Node) (ida []string) { for { f, ok := n.(*Field) if ok { - if f.Root() { + if f.Root() || NodeBoardKind(f) != "" { reverseIDA(ida) return ida } diff --git a/d2oracle/edit_test.go b/d2oracle/edit_test.go index f4c3d0ce4..0050b7f91 100644 --- a/d2oracle/edit_test.go +++ b/d2oracle/edit_test.go @@ -3309,6 +3309,22 @@ d exp: `a: { _.b -> c -> _.b } +`, + }, + { + name: "container_multiple_refs_with_underscore", + + text: `a +b: { + _.a +} +`, + key: `a`, + newKey: `b.a`, + + exp: `b: { + a +} `, }, } diff --git a/testdata/d2compiler/TestCompile/nil_scope_obj_regression.exp.json b/testdata/d2compiler/TestCompile/nil_scope_obj_regression.exp.json new file mode 100644 index 000000000..00ce55a57 --- /dev/null +++ b/testdata/d2compiler/TestCompile/nil_scope_obj_regression.exp.json @@ -0,0 +1,250 @@ +{ + "graph": { + "name": "", + "ast": { + "range": "d2/testdata/d2compiler/TestCompile/nil_scope_obj_regression.d2,0:0:0-4:0:15", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile/nil_scope_obj_regression.d2,0:0:0-0:1:1", + "key": { + "range": "d2/testdata/d2compiler/TestCompile/nil_scope_obj_regression.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/nil_scope_obj_regression.d2,0:0:0-0:1:1", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile/nil_scope_obj_regression.d2,1:0:2-3:1:14", + "key": { + "range": "d2/testdata/d2compiler/TestCompile/nil_scope_obj_regression.d2,1:0:2-1:1:3", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/nil_scope_obj_regression.d2,1:0:2-1:1:3", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile/nil_scope_obj_regression.d2,1:3:5-3:0:13", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile/nil_scope_obj_regression.d2,2:2:9-2:5:12", + "key": { + "range": "d2/testdata/d2compiler/TestCompile/nil_scope_obj_regression.d2,2:2:9-2:5:12", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/nil_scope_obj_regression.d2,2:2:9-2:3:10", + "value": [ + { + "string": "_", + "raw_string": "_" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/nil_scope_obj_regression.d2,2:4:11-2:5:12", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + ] + } + } + } + } + ] + }, + "root": { + "id": "", + "id_val": "", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "attributes": { + "label": { + "value": "" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + "edges": null, + "objects": [ + { + "id": "a", + "id_val": "a", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile/nil_scope_obj_regression.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/nil_scope_obj_regression.d2,0:0:0-0:1:1", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + }, + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile/nil_scope_obj_regression.d2,2:2:9-2:5:12", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/nil_scope_obj_regression.d2,2:2:9-2:3:10", + "value": [ + { + "string": "_", + "raw_string": "_" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/nil_scope_obj_regression.d2,2:4:11-2:5:12", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "key_path_index": 1, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "a" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "b", + "id_val": "b", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile/nil_scope_obj_regression.d2,1:0:2-1:1:3", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/nil_scope_obj_regression.d2,1:0:2-1:1:3", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "b" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + } + ] + }, + "err": null +} diff --git a/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.exp.json b/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.exp.json new file mode 100644 index 000000000..ef29483a3 --- /dev/null +++ b/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.exp.json @@ -0,0 +1,185 @@ +{ + "graph": { + "name": "", + "ast": { + "range": "d2/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.d2,0:0:0-3:0:11", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.d2,0:0:0-2:1:10", + "key": { + "range": "d2/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.d2,0:0:0-0:1:1", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.d2,0:3:3-2:0:9", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.d2,1:2:7-1:3:8", + "key": { + "range": "d2/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.d2,1:2:7-1:3:8", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.d2,1:2:7-1:3:8", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + ] + } + } + } + } + ] + }, + "root": { + "id": "", + "id_val": "", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "attributes": { + "label": { + "value": "" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + "edges": null, + "objects": [ + { + "id": "b", + "id_val": "b", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.d2,0:0:0-0:1:1", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "b" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "a", + "id_val": "a", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.d2,1:2:7-1:3:8", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.d2,1:2:7-1:3:8", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "a" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + } + ] + }, + "err": "" +}