diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 8290f40fd..f22171af6 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -2978,6 +2978,22 @@ layers: { }`, }, }, + { + name: "multiple-import-nested-layers", + text: `k + +layers: { + x: {...@y/x} +}`, + files: map[string]string{ + "y/x.d2": `a.c.link: layers.b + +layers: { + b: {...@n} +}`, + "y/n.d2": "p", + }, + }, } for _, tc := range testCases { diff --git a/d2ir/compile.go b/d2ir/compile.go index 9842d4295..53c4920da 100644 --- a/d2ir/compile.go +++ b/d2ir/compile.go @@ -864,10 +864,13 @@ func (c *compiler) updateLinks(m *Map) { bida := BoardIDA(f) aida := IDA(f) + uplevels := -1 // The id path from that board to field - relaida := aida[len(bida)+1 : len(aida)-len(bida)] - // If the link value has underscores, the path length can be less than the path length of the field - uplevels := len(relaida) - len(linkIDA) + 1 + if len(aida)-len(bida) > len(bida)+1 { + relaida := aida[len(bida)+1 : len(aida)-len(bida)] + // If the link value has underscores, the path length can be less than the path length of the field + uplevels = len(relaida) - len(linkIDA) + 1 + } if len(bida) != len(aida) { prependIDA := aida[:len(aida)-len(bida)] diff --git a/testdata/d2compiler/TestCompile/multiple-import-nested-layers.exp.json b/testdata/d2compiler/TestCompile/multiple-import-nested-layers.exp.json new file mode 100644 index 000000000..e9db7f592 --- /dev/null +++ b/testdata/d2compiler/TestCompile/multiple-import-nested-layers.exp.json @@ -0,0 +1,627 @@ +{ + "graph": { + "name": "", + "isFolderOnly": false, + "ast": { + "range": "d2/testdata/d2compiler/TestCompile/multiple-import-nested-layers.d2,0:0:0-4:1:29", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile/multiple-import-nested-layers.d2,0:0:0-0:1:1", + "key": { + "range": "d2/testdata/d2compiler/TestCompile/multiple-import-nested-layers.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/multiple-import-nested-layers.d2,0:0:0-0:1:1", + "value": [ + { + "string": "k", + "raw_string": "k" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile/multiple-import-nested-layers.d2,2:0:3-4:1:29", + "key": { + "range": "d2/testdata/d2compiler/TestCompile/multiple-import-nested-layers.d2,2:0:3-2:6:9", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/multiple-import-nested-layers.d2,2:0:3-2:6:9", + "value": [ + { + "string": "layers", + "raw_string": "layers" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile/multiple-import-nested-layers.d2,2:8:11-4:1:29", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile/multiple-import-nested-layers.d2,3:2:15-3:14:27", + "key": { + "range": "d2/testdata/d2compiler/TestCompile/multiple-import-nested-layers.d2,3:2:15-3:3:16", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/multiple-import-nested-layers.d2,3:2:15-3:3:16", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile/multiple-import-nested-layers.d2,3:5:18-3:14:27", + "nodes": [ + { + "import": { + "range": "d2/testdata/d2compiler/TestCompile/multiple-import-nested-layers.d2,3:6:19-3:13:26", + "spread": true, + "pre": "", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/multiple-import-nested-layers.d2,3:10:23-3:13:26", + "value": [ + { + "string": "y/x", + "raw_string": "y/x" + } + ] + } + } + ] + } + } + ] + } + } + } + } + ] + } + } + } + } + ] + }, + "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/multiple-import-nested-layers.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/multiple-import-nested-layers.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": { + "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": "c" + } + ] + } + } + ] + }, + "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.x.layers.b" + } + ] + } + }, + "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": "b" + } + ] + } + } + ] + }, + "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": "p" + } + ] + } + } + ] + }, + "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": "a", + "id_val": "a", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile/y/x.d2,0:0:0-0:8:8", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/y/x.d2,0:0:0-0:1:1", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/y/x.d2,0:2:2-0:3:3", + "value": [ + { + "string": "c", + "raw_string": "c" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/y/x.d2,0:4:4-0:8:8", + "value": [ + { + "string": "link", + "raw_string": "link" + } + ] + } + } + ] + }, + "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 + }, + { + "id": "c", + "id_val": "c", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile/y/x.d2,0:0:0-0:8:8", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/y/x.d2,0:0:0-0:1:1", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/y/x.d2,0:2:2-0:3:3", + "value": [ + { + "string": "c", + "raw_string": "c" + } + ] + } + }, + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/y/x.d2,0:4:4-0:8:8", + "value": [ + { + "string": "link", + "raw_string": "link" + } + ] + } + } + ] + }, + "key_path_index": 1, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "c" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "link": { + "value": "root.layers.x.layers.b" + }, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + } + ], + "layers": [ + { + "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": "p" + } + ] + } + } + ] + }, + "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": "p", + "id_val": "p", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile/y/n.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/y/n.d2,0:0:0-0:1:1", + "value": [ + { + "string": "p", + "raw_string": "p" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "p" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + } + ] + } + ] + } + ] + }, + "err": null +}