diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 29927ca57..e2c8b71bb 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -342,7 +342,7 @@ func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) { Scope: fr.Context.Scope, ScopeAST: fr.Context.ScopeAST, } - if fr.Context.ScopeMap != nil { + if fr.Context.ScopeMap != nil && !d2ir.IsVar(fr.Context.ScopeMap) { scopeObjIDA := d2graphIDA(d2ir.BoardIDA(fr.Context.ScopeMap)) r.ScopeObj = obj.Graph.Root.EnsureChild(scopeObjIDA) } @@ -738,7 +738,7 @@ func (c *compiler) compileEdge(obj *d2graph.Object, e *d2ir.Edge) { Scope: er.Context.Scope, ScopeAST: er.Context.ScopeAST, } - if er.Context.ScopeMap != nil { + if er.Context.ScopeMap != nil && !d2ir.IsVar(er.Context.ScopeMap) { scopeObjIDA := d2graphIDA(d2ir.BoardIDA(er.Context.ScopeMap)) r.ScopeObj = edge.Src.Graph.Root.EnsureChild(scopeObjIDA) } diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 767cc9ac0..d716228cf 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -3407,8 +3407,9 @@ z: { c } `, "") - assert.Equal(t, "z", g.Objects[1].ID) - assert.Equal(t, 3, len(g.Objects[1].Children)) + assert.Equal(t, "z", g.Objects[2].ID) + assert.Equal(t, 4, len(g.Objects)) + assert.Equal(t, 3, len(g.Objects[2].Children)) }, }, { @@ -3484,6 +3485,22 @@ z: "${x} be my maybe" assert.Equal(t, "always be my maybe", g.Objects[0].Label.Value) }, }, + { + name: "spread-nested", + run: func(t *testing.T) { + g := assertCompile(t, ` +vars: { + disclaimer: { + I am not a lawyer + } +} +custom-disclaimer: DRAFT DISCLAIMER { + ...${disclaimer} +} +`, "") + assert.Equal(t, 2, len(g.Objects)) + }, + }, } for _, tc := range tca { diff --git a/testdata/d2compiler/TestCompile2/vars/basic/primary-and-composite.exp.json b/testdata/d2compiler/TestCompile2/vars/basic/primary-and-composite.exp.json index bceafd191..87592f6fc 100644 --- a/testdata/d2compiler/TestCompile2/vars/basic/primary-and-composite.exp.json +++ b/testdata/d2compiler/TestCompile2/vars/basic/primary-and-composite.exp.json @@ -254,52 +254,6 @@ "constraint": null }, "zIndex": 0 - }, - { - "id": "vars", - "id_val": "vars", - "attributes": { - "label": { - "value": "vars" - }, - "labelDimensions": { - "width": 0, - "height": 0 - }, - "style": {}, - "near_key": null, - "shape": { - "value": "rectangle" - }, - "direction": { - "value": "" - }, - "constraint": null - }, - "zIndex": 0 - }, - { - "id": "x", - "id_val": "x", - "attributes": { - "label": { - "value": "x" - }, - "labelDimensions": { - "width": 0, - "height": 0 - }, - "style": {}, - "near_key": null, - "shape": { - "value": "rectangle" - }, - "direction": { - "value": "" - }, - "constraint": null - }, - "zIndex": 0 } ] }, diff --git a/testdata/d2compiler/TestCompile2/vars/basic/removed.exp.json b/testdata/d2compiler/TestCompile2/vars/basic/removed.exp.json new file mode 100644 index 000000000..afa9d9a11 --- /dev/null +++ b/testdata/d2compiler/TestCompile2/vars/basic/removed.exp.json @@ -0,0 +1,212 @@ +{ + "graph": { + "name": "", + "isFolderOnly": false, + "ast": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/removed.d2,0:0:0-8:0:121", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/removed.d2,1:0:1-7:1:120", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/removed.d2,1:0:1-1:4:5", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/removed.d2,1:0:1-1:4:5", + "value": [ + { + "string": "vars", + "raw_string": "vars" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/removed.d2,1:6:7-7:1:120", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/removed.d2,2:2:11-2:35:44", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/removed.d2,2:2:11-2:18:27", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/removed.d2,2:2:11-2:18:27", + "value": [ + { + "string": "base-constraints", + "raw_string": "base-constraints" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "array": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/removed.d2,2:20:29-2:34:43", + "nodes": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/removed.d2,2:21:30-2:29:38", + "value": [ + { + "string": "NOT NULL", + "raw_string": "NOT NULL" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/removed.d2,2:31:40-2:34:43", + "value": [ + { + "string": "UNQ", + "raw_string": "UNQ" + } + ] + } + } + ] + } + } + } + }, + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/removed.d2,3:2:47-6:3:118", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/removed.d2,3:2:47-3:12:57", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/removed.d2,3:2:47-3:12:57", + "value": [ + { + "string": "disclaimer", + "raw_string": "disclaimer" + } + ] + } + } + ] + }, + "primary": { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/removed.d2,3:14:59-3:24:69", + "value": [ + { + "string": "DISCLAIMER", + "raw_string": "DISCLAIMER" + } + ] + } + }, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/removed.d2,3:25:70-6:3:118", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/removed.d2,4:4:76-4:21:93", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/removed.d2,4:4:76-4:21:93", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/removed.d2,4:4:76-4:21:93", + "value": [ + { + "string": "I am not a lawyer", + "raw_string": "I am not a lawyer" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/removed.d2,5:4:98-5:20:114", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/removed.d2,5:4:98-5:8:102", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/removed.d2,5:4:98-5:8:102", + "value": [ + { + "string": "near", + "raw_string": "near" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/removed.d2,5:10:104-5:20:114", + "value": [ + { + "string": "top-center", + "raw_string": "top-center" + } + ] + } + } + } + } + ] + } + } + } + } + ] + } + } + } + } + ] + }, + "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": null + }, + "err": null +} diff --git a/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.exp.json b/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.exp.json new file mode 100644 index 000000000..c8e6699c9 --- /dev/null +++ b/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.exp.json @@ -0,0 +1,267 @@ +{ + "graph": { + "name": "", + "isFolderOnly": false, + "ast": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.d2,0:0:0-9:0:112", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.d2,1:0:1-5:1:52", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.d2,1:0:1-1:4:5", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.d2,1:0:1-1:4:5", + "value": [ + { + "string": "vars", + "raw_string": "vars" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.d2,1:6:7-5:1:52", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.d2,2:2:11-4:3:50", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.d2,2:2:11-2:12:21", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.d2,2:2:11-2:12:21", + "value": [ + { + "string": "disclaimer", + "raw_string": "disclaimer" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.d2,2:14:23-4:3:50", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.d2,3:4:29-3:21:46", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.d2,3:4:29-3:21:46", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.d2,3:4:29-3:21:46", + "value": [ + { + "string": "I am not a lawyer", + "raw_string": "I am not a lawyer" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + ] + } + } + } + } + ] + } + } + } + }, + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.d2,6:0:53-8:1:111", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.d2,6:0:53-6:17:70", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.d2,6:0:53-6:17:70", + "value": [ + { + "string": "custom-disclaimer", + "raw_string": "custom-disclaimer" + } + ] + } + } + ] + }, + "primary": { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.d2,6:19:72-6:35:88", + "value": [ + { + "string": "DRAFT DISCLAIMER", + "raw_string": "DRAFT DISCLAIMER" + } + ] + } + }, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.d2,6:36:89-8:1:111", + "nodes": [ + { + "substitution": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.d2,7:2:93-7:18:109", + "spread": true, + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.d2,7:7:98-7:17:108", + "value": [ + { + "string": "disclaimer", + "raw_string": "disclaimer" + } + ] + } + } + ] + } + } + ] + } + } + } + } + ] + }, + "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": "I am not a lawyer", + "id_val": "I am not a lawyer", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.d2,3:4:29-3:21:46", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.d2,3:4:29-3:21:46", + "value": [ + { + "string": "I am not a lawyer", + "raw_string": "I am not a lawyer" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "I am not a lawyer" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + }, + { + "id": "custom-disclaimer", + "id_val": "custom-disclaimer", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.d2,6:0:53-6:17:70", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread-nested.d2,6:0:53-6:17:70", + "value": [ + { + "string": "custom-disclaimer", + "raw_string": "custom-disclaimer" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "DRAFT DISCLAIMER" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + } + ] + }, + "err": null +} diff --git a/testdata/d2compiler/TestCompile2/vars/basic/spread.exp.json b/testdata/d2compiler/TestCompile2/vars/basic/spread.exp.json index 9dbadf0d4..8789c60c7 100644 --- a/testdata/d2compiler/TestCompile2/vars/basic/spread.exp.json +++ b/testdata/d2compiler/TestCompile2/vars/basic/spread.exp.json @@ -282,6 +282,51 @@ }, "zIndex": 0 }, + { + "id": "b", + "id_val": "b", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread.d2,4:4:30-4:5:31", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread.d2,4:4:30-4:5:31", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "c" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + }, { "id": "z", "id_val": "z", @@ -371,97 +416,6 @@ "constraint": null }, "zIndex": 0 - }, - { - "id": "vars", - "id_val": "vars", - "attributes": { - "label": { - "value": "vars" - }, - "labelDimensions": { - "width": 0, - "height": 0 - }, - "style": {}, - "near_key": null, - "shape": { - "value": "rectangle" - }, - "direction": { - "value": "" - }, - "constraint": null - }, - "zIndex": 0 - }, - { - "id": "x", - "id_val": "x", - "attributes": { - "label": { - "value": "x" - }, - "labelDimensions": { - "width": 0, - "height": 0 - }, - "style": {}, - "near_key": null, - "shape": { - "value": "rectangle" - }, - "direction": { - "value": "" - }, - "constraint": null - }, - "zIndex": 0 - }, - { - "id": "b", - "id_val": "b", - "references": [ - { - "key": { - "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread.d2,4:4:30-4:5:31", - "path": [ - { - "unquoted_string": { - "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/spread.d2,4:4:30-4:5:31", - "value": [ - { - "string": "b", - "raw_string": "b" - } - ] - } - } - ] - }, - "key_path_index": 0, - "map_key_edge_index": -1 - } - ], - "attributes": { - "label": { - "value": "c" - }, - "labelDimensions": { - "width": 0, - "height": 0 - }, - "style": {}, - "near_key": null, - "shape": { - "value": "rectangle" - }, - "direction": { - "value": "" - }, - "constraint": null - }, - "zIndex": 0 } ] },