fix double quote primary

This commit is contained in:
Alexander Wang 2023-07-13 10:11:08 -07:00
parent ffdb5121f4
commit 780345beeb
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
3 changed files with 233 additions and 3 deletions

View file

@ -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 {

View file

@ -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
}

View file

@ -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
}