set the ast range path

This commit is contained in:
Alexander Wang 2025-01-08 16:42:44 -07:00
parent c50fab758a
commit 232c47d21a
No known key found for this signature in database
GPG key ID: BE3937D0D52D8927
3 changed files with 185 additions and 0 deletions

View file

@ -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 {

View file

@ -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)))
}

View file

@ -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
}