fix chained imports and underscore links

This commit is contained in:
Alexander Wang 2024-09-06 14:58:29 -06:00
parent e2e8878d48
commit 84ee4ef4a2
No known key found for this signature in database
GPG key ID: BE3937D0D52D8927
3 changed files with 753 additions and 2 deletions

View file

@ -3072,6 +3072,28 @@ layers: {
tassert.Equal(t, "root.layers.x.layers.k", g.Layers[0].Layers[0].Objects[1].Link.Value)
},
},
{
name: "import-link-underscore-3",
text: `k
layers: {
x: @x
b: {
b
}
}`,
files: map[string]string{
"x.d2": `a
layers: {
y: @y
}`,
"y.d2": `o.link: _._.layers.b
`,
},
assertions: func(t *testing.T, g *d2graph.Graph) {
tassert.Equal(t, "root.layers.b", g.Layers[0].Layers[0].Objects[0].Link.Value)
},
},
{
name: "invalid-link-1",
text: `k

View file

@ -904,7 +904,6 @@ func (c *compiler) ignoreLazyGlob(n Node) bool {
func (c *compiler) extendLinks(m *Map, importF *Field, importDir string) {
nodeBoardKind := NodeBoardKind(m)
importIDA := IDA(importF)
FIELDS_LOOP:
for _, f := range m.Fields {
if f.Name == "link" {
if nodeBoardKind != "" {
@ -924,7 +923,7 @@ FIELDS_LOOP:
for _, id := range linkIDA[1:] {
if id == "_" {
if len(linkIDA) < 2 || len(importIDA) < 2 {
continue FIELDS_LOOP
break
}
linkIDA = append([]string{linkIDA[0]}, linkIDA[2:]...)
importIDA = importIDA[:len(importIDA)-2]

View file

@ -0,0 +1,730 @@
{
"graph": {
"name": "",
"isFolderOnly": false,
"ast": {
"range": "d2/testdata/d2compiler/TestCompile/import-link-underscore-3.d2,0:0:0-7:1:38",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile/import-link-underscore-3.d2,0:0:0-0:1:1",
"key": {
"range": "d2/testdata/d2compiler/TestCompile/import-link-underscore-3.d2,0:0:0-0:1:1",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/import-link-underscore-3.d2,0:0:0-0:1:1",
"value": [
{
"string": "k",
"raw_string": "k"
}
]
}
}
]
},
"primary": {},
"value": {}
}
},
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile/import-link-underscore-3.d2,2:0:3-7:1:38",
"key": {
"range": "d2/testdata/d2compiler/TestCompile/import-link-underscore-3.d2,2:0:3-2:6:9",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/import-link-underscore-3.d2,2:0:3-2:6:9",
"value": [
{
"string": "layers",
"raw_string": "layers"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile/import-link-underscore-3.d2,2:8:11-7:1:38",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile/import-link-underscore-3.d2,3:2:15-3:7:20",
"key": {
"range": "d2/testdata/d2compiler/TestCompile/import-link-underscore-3.d2,3:2:15-3:3:16",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/import-link-underscore-3.d2,3:2:15-3:3:16",
"value": [
{
"string": "x",
"raw_string": "x"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": ",1:0:0-1:0:0",
"nodes": [
{
"import": {
"range": ",1:0:0-1:0:0",
"spread": true,
"pre": "",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/import-link-underscore-3.d2,3:6:19-3:7:20",
"value": [
{
"string": "x",
"raw_string": "x"
}
]
}
}
]
}
}
]
},
"import": {
"range": "d2/testdata/d2compiler/TestCompile/import-link-underscore-3.d2,3:5:18-3:7:20",
"spread": false,
"pre": "",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/import-link-underscore-3.d2,3:6:19-3:7:20",
"value": [
{
"string": "x",
"raw_string": "x"
}
]
}
}
]
}
}
}
},
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile/import-link-underscore-3.d2,4:1:22-6:3:36",
"key": {
"range": "d2/testdata/d2compiler/TestCompile/import-link-underscore-3.d2,4:1:22-4:2:23",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/import-link-underscore-3.d2,4:1:22-4:2:23",
"value": [
{
"string": "b",
"raw_string": "b"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile/import-link-underscore-3.d2,4:4:25-6:3:36",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile/import-link-underscore-3.d2,5:4:31-5:5:32",
"key": {
"range": "d2/testdata/d2compiler/TestCompile/import-link-underscore-3.d2,5:4:31-5:5:32",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/import-link-underscore-3.d2,5:4:31-5:5:32",
"value": [
{
"string": "b",
"raw_string": "b"
}
]
}
}
]
},
"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": "k",
"id_val": "k",
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile/import-link-underscore-3.d2,0:0:0-0:1:1",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/import-link-underscore-3.d2,0:0:0-0:1:1",
"value": [
{
"string": "k",
"raw_string": "k"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "k"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
}
],
"layers": [
{
"name": "x",
"isFolderOnly": false,
"ast": {
"range": ",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": ",0:0:0-0:0:0",
"value": [
{
"string": "a"
}
]
}
}
]
},
"primary": {},
"value": {}
}
},
{
"map_key": {
"range": ",0:0:0-0:0:0",
"key": {
"range": ",0:0:0-0:0:0",
"path": [
{
"unquoted_string": {
"range": ",0:0:0-0:0:0",
"value": [
{
"string": "layers"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": ",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": ",0:0:0-0:0:0",
"value": [
{
"string": "y"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": ",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": ",0:0:0-0:0:0",
"value": [
{
"string": "o"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": ",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": ",0:0:0-0:0:0",
"value": [
{
"string": "link"
}
]
}
}
]
},
"primary": {
"unquoted_string": {
"range": ",0:0:0-0:0:0",
"value": [
{
"string": "root.layers.b"
}
]
}
},
"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": "a",
"id_val": "a",
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile/x.d2,0:0:0-0:1:1",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/x.d2,0:0:0-0:1:1",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "a"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
}
],
"layers": [
{
"name": "y",
"isFolderOnly": false,
"ast": {
"range": ",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": ",0:0:0-0:0:0",
"value": [
{
"string": "o"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": ",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": ",0:0:0-0:0:0",
"value": [
{
"string": "link"
}
]
}
}
]
},
"primary": {
"unquoted_string": {
"range": ",0:0:0-0:0:0",
"value": [
{
"string": "root.layers.b"
}
]
}
},
"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": "o",
"id_val": "o",
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile/y.d2,0:0:0-0:6:6",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/y.d2,0:0:0-0:1:1",
"value": [
{
"string": "o",
"raw_string": "o"
}
]
}
},
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/y.d2,0:2:2-0:6:6",
"value": [
{
"string": "link",
"raw_string": "link"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "o"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"link": {
"value": "root.layers.b"
},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
}
]
}
]
},
{
"name": "b",
"isFolderOnly": false,
"ast": {
"range": ",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": ",0:0:0-0:0:0",
"value": [
{
"string": "b"
}
]
}
}
]
},
"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": "b",
"id_val": "b",
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile/import-link-underscore-3.d2,5:4:31-5:5:32",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/import-link-underscore-3.d2,5:4:31-5:5:32",
"value": [
{
"string": "b",
"raw_string": "b"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "b"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
}
]
}
]
},
"err": null
}