d2ir/imports: fix removing parent scope of nested import fields/edges

This commit is contained in:
Alexander Wang 2024-09-11 10:34:39 -06:00
parent 47406451c2
commit 4e93f70db3
No known key found for this signature in database
GPG key ID: BE3937D0D52D8927
3 changed files with 74 additions and 54 deletions

View file

@ -2979,8 +2979,8 @@ qa: {
assertions: func(t *testing.T, g *d2graph.Graph) {
tassert.Equal(t, "dev.env", g.Objects[1].AbsID())
tassert.Equal(t, "Dev Environment", g.Objects[1].Label.Value)
tassert.Equal(t, "qa.env", g.Objects[2].AbsID())
tassert.Equal(t, "Qa Environment", g.Objects[2].Label.Value)
tassert.Equal(t, "qa.env", g.Objects[4].AbsID())
tassert.Equal(t, "Qa Environment", g.Objects[4].Label.Value)
},
},
{

View file

@ -61,15 +61,18 @@ func (c *compiler) _import(imp *d2ast.Import) (Node, bool) {
if !ok {
return nil, false
}
nilScopeMap(ir)
if len(imp.IDA()) > 0 {
f := ir.GetField(imp.IDA()...)
if f == nil {
c.errorf(imp, "import key %q doesn't exist inside import", imp.IDA())
return nil, false
}
if f.Map() != nil {
nilScopeMap(f.Map())
}
return f, true
}
nilScopeMap(ir)
return ir, true
}
@ -132,15 +135,9 @@ func nilScopeMap(n Node) {
for _, r := range n.References {
r.Context_.ScopeMap = nil
}
if n.Map() != nil {
nilScopeMap(n.Map())
}
case *Field:
for _, r := range n.References {
r.Context_.ScopeMap = nil
}
if n.Map() != nil {
nilScopeMap(n.Map())
}
}
}

View file

@ -344,51 +344,6 @@
},
"zIndex": 0
},
{
"id": "env",
"id_val": "env",
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile/template.d2,0:0:0-0:3:3",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/template.d2,0:0:0-0:3:3",
"value": [
{
"string": "env",
"raw_string": "env"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "Qa Environment"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
},
{
"id": "vm",
"id_val": "vm",
@ -434,6 +389,74 @@
},
"zIndex": 0
},
{
"id": "env",
"id_val": "env",
"attributes": {
"label": {
"value": "env"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
},
{
"id": "env",
"id_val": "env",
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile/template.d2,0:0:0-0:3:3",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/template.d2,0:0:0-0:3:3",
"value": [
{
"string": "env",
"raw_string": "env"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "Qa Environment"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
},
{
"id": "vm",
"id_val": "vm",