From 7363608615f2c5563586e252be347568c385cb2a Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Mon, 26 Aug 2024 14:12:58 -0600 Subject: [PATCH] fix importing icon.near field --- d2compiler/compile_test.go | 10 + d2ir/compile.go | 2 +- .../TestCompile/import-icon-near.exp.json | 173 ++++++++++++++++++ 3 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 testdata/d2compiler/TestCompile/import-icon-near.exp.json diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index abcc12780..50ac80cc4 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -2396,6 +2396,16 @@ layers: { }`, expErr: `d2/testdata/d2compiler/TestCompile/link-board-underscore-not-found.d2:7:9: linked board not found`, }, + { + name: "import-icon-near", + text: `y: @y +`, + files: map[string]string{ + "y.d2": `syslog +*.icon.near: center-left +`, + }, + }, { name: "border-radius-negative", text: `x diff --git a/d2ir/compile.go b/d2ir/compile.go index d291edb73..8f8a16ca0 100644 --- a/d2ir/compile.go +++ b/d2ir/compile.go @@ -934,7 +934,7 @@ func (c *compiler) extendLinks(m *Map, importF *Field, importDir string) { s := d2format.Format(kp) f.Primary_.Value = d2ast.MakeValueBox(d2ast.FlatUnquotedString(s)).ScalarBox().Unbox() } - if f.Name == "icon" { + if f.Name == "icon" && f.Primary() != nil { val := f.Primary().Value.ScalarString() u, err := url.Parse(html.UnescapeString(val)) isRemoteImg := err == nil && strings.HasPrefix(u.Scheme, "http") diff --git a/testdata/d2compiler/TestCompile/import-icon-near.exp.json b/testdata/d2compiler/TestCompile/import-icon-near.exp.json new file mode 100644 index 000000000..ae9301419 --- /dev/null +++ b/testdata/d2compiler/TestCompile/import-icon-near.exp.json @@ -0,0 +1,173 @@ +{ + "graph": { + "name": "", + "isFolderOnly": false, + "ast": { + "range": "d2/testdata/d2compiler/TestCompile/import-icon-near.d2,0:0:0-1:0:6", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile/import-icon-near.d2,0:0:0-0:5:5", + "key": { + "range": "d2/testdata/d2compiler/TestCompile/import-icon-near.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/import-icon-near.d2,0:0:0-0:1:1", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "import": { + "range": "d2/testdata/d2compiler/TestCompile/import-icon-near.d2,0:3:3-0:5:5", + "spread": false, + "pre": "", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/import-icon-near.d2,0:4:4-0:5:5", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + } + ] + } + } + } + } + ] + }, + "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": "y", + "id_val": "y", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile/import-icon-near.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/import-icon-near.d2,0:0:0-0:1:1", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "y" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + }, + { + "id": "syslog", + "id_val": "syslog", + "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:6:6", + "value": [ + { + "string": "syslog", + "raw_string": "syslog" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "syslog" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null, + "iconPosition": { + "value": "center-left" + } + }, + "zIndex": 0 + } + ] + }, + "err": null +}