From 780345beeb3e3ad89e70b2f940a5a4f04b684b8a Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Thu, 13 Jul 2023 10:11:08 -0700 Subject: [PATCH] fix double quote primary --- d2compiler/compile_test.go | 15 ++ d2ir/compile.go | 5 +- .../vars/basic/double-quote-primary.exp.json | 216 ++++++++++++++++++ 3 files changed, 233 insertions(+), 3 deletions(-) create mode 100644 testdata/d2compiler/TestCompile2/vars/basic/double-quote-primary.exp.json diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 33336b247..fdf1cc82f 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -3469,6 +3469,21 @@ z.class: [a; ${x}together] assert.Equal(t, "alltogether", g.Objects[0].Attributes.Classes[1]) }, }, + { + name: "double-quote-primary", + run: func(t *testing.T) { + g := assertCompile(t, ` +vars: { + x: always { + a: b + } +} +z: "${x} be my maybe" +`, "") + assert.Equal(t, "z", g.Objects[0].ID) + assert.Equal(t, "always be my maybe", g.Objects[0].Label.Value) + }, + }, } for _, tc := range tca { diff --git a/d2ir/compile.go b/d2ir/compile.go index 2f28ce9b6..0cdbe5c45 100644 --- a/d2ir/compile.go +++ b/d2ir/compile.go @@ -112,8 +112,7 @@ func (c *compiler) compileSubstitutions(m *Map, varsStack []*Map) { c.resolveSubstitutions(varsStack, scalar) } } - } - if f.Map() != nil { + } else if f.Map() != nil { // don't resolve substitutions in vars with the current scope of vars if f.Name == "vars" { c.compileSubstitutions(f.Map(), varsStack[1:]) @@ -238,7 +237,7 @@ func (c *compiler) resolveSubstitutions(varsStack []*Map, node Node) { c.errorf(node.LastRef().AST(), `could not resolve variable "%s"`, strings.Join(box.Substitution.IDA(), ".")) return } - if resolvedField.Composite != nil { + if resolvedField.Primary() == nil && resolvedField.Composite != nil { c.errorf(node.LastRef().AST(), `cannot substitute map variable "%s" in quotes`, strings.Join(box.Substitution.IDA(), ".")) return } diff --git a/testdata/d2compiler/TestCompile2/vars/basic/double-quote-primary.exp.json b/testdata/d2compiler/TestCompile2/vars/basic/double-quote-primary.exp.json new file mode 100644 index 000000000..d23e46259 --- /dev/null +++ b/testdata/d2compiler/TestCompile2/vars/basic/double-quote-primary.exp.json @@ -0,0 +1,216 @@ +{ + "graph": { + "name": "", + "isFolderOnly": false, + "ast": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/double-quote-primary.d2,0:0:0-7:0:59", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/double-quote-primary.d2,1:0:1-5:1:36", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/double-quote-primary.d2,1:0:1-1:4:5", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/double-quote-primary.d2,1:0:1-1:4:5", + "value": [ + { + "string": "vars", + "raw_string": "vars" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/double-quote-primary.d2,1:6:7-5:1:36", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/double-quote-primary.d2,2:1:10-4:3:34", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/double-quote-primary.d2,2:1:10-2:2:11", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/double-quote-primary.d2,2:1:10-2:2:11", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "primary": { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/double-quote-primary.d2,2:4:13-2:10:19", + "value": [ + { + "string": "always", + "raw_string": "always" + } + ] + } + }, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/double-quote-primary.d2,2:11:20-4:3:34", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/double-quote-primary.d2,3:4:26-3:8:30", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/double-quote-primary.d2,3:4:26-3:5:27", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/double-quote-primary.d2,3:4:26-3:5:27", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/double-quote-primary.d2,3:7:29-3:8:30", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + } + } + ] + } + } + } + } + ] + } + } + } + }, + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/double-quote-primary.d2,6:0:37-6:21:58", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/double-quote-primary.d2,6:0:37-6:1:38", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/double-quote-primary.d2,6:0:37-6:1:38", + "value": [ + { + "string": "z", + "raw_string": "z" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "double_quoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/double-quote-primary.d2,6:3:40-6:21:58", + "value": [ + { + "string": "always be my maybe" + } + ] + } + } + } + } + ] + }, + "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": "z", + "id_val": "z", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/double-quote-primary.d2,6:0:37-6:1:38", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/double-quote-primary.d2,6:0:37-6:1:38", + "value": [ + { + "string": "z", + "raw_string": "z" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "always be my maybe" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + } + ] + }, + "err": null +}