diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 4a15e1678..f51e723a2 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -3515,6 +3515,23 @@ svc_1."think about A" svc_1.t2 -> b: do with B `, }, + { + name: "layer-import-nested-layer", + + text: `...@meow +`, + files: map[string]string{ + "meow.d2": `layers: { + 1: { + asdf + } +} +`, + }, + assertions: func(t *testing.T, g *d2graph.Graph) { + tassert.Equal(t, "d2/testdata/d2compiler/TestCompile/meow.d2", g.Layers[0].AST.Range.Path) + }, + }, } for _, tc := range testCases { diff --git a/d2ir/d2ir.go b/d2ir/d2ir.go index 18a8b5b9e..550269afa 100644 --- a/d2ir/d2ir.go +++ b/d2ir/d2ir.go @@ -1458,6 +1458,12 @@ func (m *Map) AST() d2ast.Node { astMap := &d2ast.Map{ Range: d2ast.MakeRange(",0:0:0-1:0:0"), } + if m.parent != nil { + f, ok := m.parent.(*Field) + if ok { + astMap.Range.Path = f.Name.GetRange().Path + } + } for _, f := range m.Fields { astMap.Nodes = append(astMap.Nodes, d2ast.MakeMapNodeBox(f.AST().(d2ast.MapNode))) } diff --git a/testdata/d2compiler/TestCompile/layer-import-nested-layer.exp.json b/testdata/d2compiler/TestCompile/layer-import-nested-layer.exp.json new file mode 100644 index 000000000..07506ea7a --- /dev/null +++ b/testdata/d2compiler/TestCompile/layer-import-nested-layer.exp.json @@ -0,0 +1,162 @@ +{ + "graph": { + "name": "", + "isFolderOnly": true, + "ast": { + "range": "d2/testdata/d2compiler/TestCompile/layer-import-nested-layer.d2,0:0:0-1:0:9", + "nodes": [ + { + "import": { + "range": "d2/testdata/d2compiler/TestCompile/layer-import-nested-layer.d2,0:0:0-0:8:8", + "spread": true, + "pre": "", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/layer-import-nested-layer.d2,0:4:4-0:8:8", + "value": [ + { + "string": "meow", + "raw_string": "meow" + } + ] + } + } + ] + } + } + ] + }, + "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, + "layers": [ + { + "name": "1", + "isFolderOnly": false, + "ast": { + "range": "d2/testdata/d2compiler/TestCompile/meow.d2,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": "d2/testdata/d2compiler/TestCompile/meow.d2,2:4:21-2:8:25", + "value": [ + { + "string": "asdf", + "raw_string": "asdf" + } + ] + } + } + ] + }, + "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": "asdf", + "id_val": "asdf", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile/meow.d2,2:4:21-2:8:25", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/meow.d2,2:4:21-2:8:25", + "value": [ + { + "string": "asdf", + "raw_string": "asdf" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "asdf" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + } + ] + } + ] + }, + "err": null +}