From 5afc712455bd58774539f2991177273f1ffabd0c Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Sun, 13 Aug 2023 10:15:13 -0700 Subject: [PATCH] dont modify ast when modifying links --- d2ir/compile.go | 12 +++++------ .../link-board-key-nested.exp.json | 5 +++-- .../TestCompile/link-board-mixed.exp.json | 5 +++-- .../TestCompile/link-board-nested.exp.json | 5 +++-- .../TestCompile/link-board-ok.exp.json | 5 +++-- .../link-board-underscore.exp.json | 10 ++++++---- .../d2ir/TestCompile/imports/boards.exp.json | 20 +++++++++++-------- 7 files changed, 36 insertions(+), 26 deletions(-) diff --git a/d2ir/compile.go b/d2ir/compile.go index 9bb92b2538..7d5e614932 100644 --- a/d2ir/compile.go +++ b/d2ir/compile.go @@ -580,14 +580,14 @@ func (c *compiler) _compileField(f *Field, refctx *RefContext) { } } } else if refctx.Key.Value.ScalarBox().Unbox() != nil { - // If the link is a board, we need to transform it into an absolute path. - if f.Name == "link" { - c.compileLink(refctx) - } f.Primary_ = &Scalar{ parent: f, Value: refctx.Key.Value.ScalarBox().Unbox(), } + // If the link is a board, we need to transform it into an absolute path. + if f.Name == "link" { + c.compileLink(f, refctx) + } } } @@ -624,7 +624,7 @@ func (c *compiler) updateLinks(m *Map) { } } -func (c *compiler) compileLink(refctx *RefContext) { +func (c *compiler) compileLink(f *Field, refctx *RefContext) { val := refctx.Key.Value.ScalarBox().Unbox().ScalarString() link, err := d2parser.ParseKey(val) if err != nil { @@ -683,7 +683,7 @@ func (c *compiler) compileLink(refctx *RefContext) { // Create the absolute path by appending scope path with value specified scopeIDA = append(scopeIDA, linkIDA...) kp := d2ast.MakeKeyPath(scopeIDA) - refctx.Key.Value = d2ast.MakeValueBox(d2ast.FlatUnquotedString(d2format.Format(kp))) + f.Primary_.Value = d2ast.FlatUnquotedString(d2format.Format(kp)) } func (c *compiler) compileEdges(refctx *RefContext) { diff --git a/testdata/d2compiler/TestCompile/link-board-key-nested.exp.json b/testdata/d2compiler/TestCompile/link-board-key-nested.exp.json index 66fe44602b..971dbe9050 100644 --- a/testdata/d2compiler/TestCompile/link-board-key-nested.exp.json +++ b/testdata/d2compiler/TestCompile/link-board-key-nested.exp.json @@ -62,10 +62,11 @@ "primary": {}, "value": { "unquoted_string": { - "range": ",0:0:0-0:0:0", + "range": "d2/testdata/d2compiler/TestCompile/link-board-key-nested.d2,1:10:15-1:18:23", "value": [ { - "string": "root.layers.x" + "string": "layers.x", + "raw_string": "layers.x" } ] } diff --git a/testdata/d2compiler/TestCompile/link-board-mixed.exp.json b/testdata/d2compiler/TestCompile/link-board-mixed.exp.json index 974fc5fbc4..04736c7819 100644 --- a/testdata/d2compiler/TestCompile/link-board-mixed.exp.json +++ b/testdata/d2compiler/TestCompile/link-board-mixed.exp.json @@ -71,10 +71,11 @@ "primary": {}, "value": { "unquoted_string": { - "range": ",0:0:0-0:0:0", + "range": "d2/testdata/d2compiler/TestCompile/link-board-mixed.d2,1:15:46-1:25:56", "value": [ { - "string": "root.layers.cat" + "string": "layers.cat", + "raw_string": "layers.cat" } ] } diff --git a/testdata/d2compiler/TestCompile/link-board-nested.exp.json b/testdata/d2compiler/TestCompile/link-board-nested.exp.json index e1ba86106d..54bf396b16 100644 --- a/testdata/d2compiler/TestCompile/link-board-nested.exp.json +++ b/testdata/d2compiler/TestCompile/link-board-nested.exp.json @@ -38,10 +38,11 @@ "primary": {}, "value": { "unquoted_string": { - "range": ",0:0:0-0:0:0", + "range": "d2/testdata/d2compiler/TestCompile/link-board-nested.d2,0:8:8-0:25:25", "value": [ { - "string": "root.layers.x.layers.x" + "string": "layers.x.layers.x", + "raw_string": "layers.x.layers.x" } ] } diff --git a/testdata/d2compiler/TestCompile/link-board-ok.exp.json b/testdata/d2compiler/TestCompile/link-board-ok.exp.json index 1f8db41934..f092c5be67 100644 --- a/testdata/d2compiler/TestCompile/link-board-ok.exp.json +++ b/testdata/d2compiler/TestCompile/link-board-ok.exp.json @@ -38,10 +38,11 @@ "primary": {}, "value": { "unquoted_string": { - "range": ",0:0:0-0:0:0", + "range": "d2/testdata/d2compiler/TestCompile/link-board-ok.d2,0:8:8-0:16:16", "value": [ { - "string": "root.layers.x" + "string": "layers.x", + "raw_string": "layers.x" } ] } diff --git a/testdata/d2compiler/TestCompile/link-board-underscore.exp.json b/testdata/d2compiler/TestCompile/link-board-underscore.exp.json index a8ba7e5097..58829cf3d2 100644 --- a/testdata/d2compiler/TestCompile/link-board-underscore.exp.json +++ b/testdata/d2compiler/TestCompile/link-board-underscore.exp.json @@ -180,10 +180,11 @@ "primary": {}, "value": { "unquoted_string": { - "range": ",0:0:0-0:0:0", + "range": "d2/testdata/d2compiler/TestCompile/link-board-underscore.d2,6:20:69-6:32:81", "value": [ { - "string": "root.layers.x" + "string": "_._.layers.x", + "raw_string": "_._.layers.x" } ] } @@ -223,10 +224,11 @@ "primary": {}, "value": { "unquoted_string": { - "range": ",0:0:0-0:0:0", + "range": "d2/testdata/d2compiler/TestCompile/link-board-underscore.d2,7:18:100-7:19:101", "value": [ { - "string": "root.layers.x" + "string": "_", + "raw_string": "_" } ] } diff --git a/testdata/d2ir/TestCompile/imports/boards.exp.json b/testdata/d2ir/TestCompile/imports/boards.exp.json index 6b5e2d61c8..236a496dc0 100644 --- a/testdata/d2ir/TestCompile/imports/boards.exp.json +++ b/testdata/d2ir/TestCompile/imports/boards.exp.json @@ -88,10 +88,11 @@ "primary": {}, "value": { "unquoted_string": { - "range": ",0:0:0-0:0:0", + "range": "index.d2,0:8:8-0:16:16", "value": [ { - "string": "root.layers.x" + "string": "layers.x", + "raw_string": "layers.x" } ] } @@ -176,10 +177,11 @@ "primary": {}, "value": { "unquoted_string": { - "range": ",0:0:0-0:0:0", + "range": "index.d2,0:8:8-0:16:16", "value": [ { - "string": "root.layers.x" + "string": "layers.x", + "raw_string": "layers.x" } ] } @@ -285,10 +287,11 @@ "primary": {}, "value": { "unquoted_string": { - "range": ",0:0:0-0:0:0", + "range": "x.d2,0:8:8-0:16:16", "value": [ { - "string": "root.layers.y" + "string": "layers.y", + "raw_string": "layers.y" } ] } @@ -373,10 +376,11 @@ "primary": {}, "value": { "unquoted_string": { - "range": ",0:0:0-0:0:0", + "range": "x.d2,0:8:8-0:16:16", "value": [ { - "string": "root.layers.y" + "string": "layers.y", + "raw_string": "layers.y" } ] }