This commit is contained in:
Alexander Wang 2023-07-11 14:56:09 -07:00
parent a6125c46ec
commit 85cf491d74
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
16 changed files with 622 additions and 47 deletions

View file

@ -3290,6 +3290,41 @@ a -> b: ${x}
assert.Equal(t, "im a var", g.Edges[0].Label.Value) assert.Equal(t, "im a var", g.Edges[0].Label.Value)
}, },
}, },
{
name: "quoted-var",
run: func(t *testing.T) {
g := assertCompile(t, `
vars: {
primaryColors: {
button: {
active: "#4baae5"
}
}
}
button: {
style: {
border-radius: 5
fill: ${primaryColors.button.active}
}
}
`, "")
assert.Equal(t, `#4baae5`, g.Objects[0].Style.Fill.Value)
},
},
{
name: "quoted-var-quoted-sub",
run: func(t *testing.T) {
g := assertCompile(t, `
vars: {
x: "hi"
}
y: "hey ${x}"
`, "")
assert.Equal(t, `hey "hi"`, g.Objects[0].Label.Value)
},
},
} }
for _, tc := range tca { for _, tc := range tca {

View file

@ -1651,6 +1651,7 @@ var SimpleReservedKeywords = map[string]struct{}{
"vertical-gap": {}, "vertical-gap": {},
"horizontal-gap": {}, "horizontal-gap": {},
"class": {}, "class": {},
"vars": {},
} }
// ReservedKeywordHolders are reserved keywords that are meaningless on its own and must hold composites // ReservedKeywordHolders are reserved keywords that are meaningless on its own and must hold composites

View file

@ -116,11 +116,16 @@ func (c *compiler) resolveSubstitutions(refctx *RefContext) {
if box.Substitution != nil { if box.Substitution != nil {
resolvedField := c.resolveSubstitution(varsMap, refctx.Key, box.Substitution) resolvedField := c.resolveSubstitution(varsMap, refctx.Key, box.Substitution)
if resolvedField != nil { if resolvedField != nil {
// If lone and unquoted, replace with value of sub
if len(refctx.Key.Value.UnquotedString.Value) == 1 {
refctx.Key.Value = d2ast.MakeValueBox(resolvedField.Primary().Value)
} else {
refctx.Key.Value.UnquotedString.Value[i].String = go2.Pointer(resolvedField.Primary().String()) refctx.Key.Value.UnquotedString.Value[i].String = go2.Pointer(resolvedField.Primary().String())
subbed = true subbed = true
} }
} }
} }
}
if subbed { if subbed {
refctx.Key.Value.UnquotedString.Coalesce() refctx.Key.Value.UnquotedString.Coalesce()
} }

View file

@ -112,10 +112,11 @@
"primary": {}, "primary": {},
"value": { "value": {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/edge_label.d2,4:8:33-4:9:34", "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/edge_label.d2,2:5:14-2:13:22",
"value": [ "value": [
{ {
"string": "im a var" "string": "im a var",
"raw_string": "im a var"
} }
] ]
} }

View file

@ -89,10 +89,11 @@
"primary": {}, "primary": {},
"value": { "value": {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/label.d2,4:4:29-4:5:30", "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/label.d2,2:5:14-2:13:22",
"value": [ "value": [
{ {
"string": "im a var" "string": "im a var",
"raw_string": "im a var"
} }
] ]
} }

View file

@ -182,10 +182,11 @@
"primary": {}, "primary": {},
"value": { "value": {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/nested.d2,9:14:85-9:15:86", "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/nested.d2,4:14:49-4:17:52",
"value": [ "value": [
{ {
"string": "red" "string": "red",
"raw_string": "red"
} }
] ]
} }

View file

@ -108,13 +108,10 @@
}, },
"primary": {}, "primary": {},
"value": { "value": {
"unquoted_string": { "number": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/number.d2,5:15:44-5:16:45", "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/number.d2,2:10:19-2:11:20",
"value": [ "raw": "2",
{ "value": "2"
"string": "2"
}
]
} }
} }
} }

View file

@ -0,0 +1,177 @@
{
"graph": {
"name": "",
"isFolderOnly": false,
"ast": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var-quoted-sub.d2,0:0:0-6:0:36",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var-quoted-sub.d2,1:0:1-3:1:20",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var-quoted-sub.d2,1:0:1-1:4:5",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var-quoted-sub.d2,1:0:1-1:4:5",
"value": [
{
"string": "vars",
"raw_string": "vars"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var-quoted-sub.d2,1:6:7-3:1:20",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var-quoted-sub.d2,2:2:11-2:9:18",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var-quoted-sub.d2,2:2:11-2:3:12",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var-quoted-sub.d2,2:2:11-2:3:12",
"value": [
{
"string": "x",
"raw_string": "x"
}
]
}
}
]
},
"primary": {},
"value": {
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var-quoted-sub.d2,2:5:14-2:9:18",
"value": [
{
"string": "hi",
"raw_string": "hi"
}
]
}
}
}
}
]
}
}
}
},
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var-quoted-sub.d2,5:0:22-5:13:35",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var-quoted-sub.d2,5:0:22-5:1:23",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var-quoted-sub.d2,5:0:22-5:1:23",
"value": [
{
"string": "y",
"raw_string": "y"
}
]
}
}
]
},
"primary": {},
"value": {
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var-quoted-sub.d2,5:3:25-5:13:35",
"value": [
{
"string": "hey \"hi\""
}
]
}
}
}
}
]
},
"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/TestCompile2/vars/basic/quoted-var-quoted-sub.d2,5:0:22-5:1:23",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var-quoted-sub.d2,5:0:22-5:1:23",
"value": [
{
"string": "y",
"raw_string": "y"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "hey \"hi\""
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
}
]
},
"err": null
}

View file

@ -0,0 +1,330 @@
{
"graph": {
"name": "",
"isFolderOnly": false,
"ast": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,0:0:0-15:0:168",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,1:0:1-7:1:77",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,1:0:1-1:4:5",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,1:0:1-1:4:5",
"value": [
{
"string": "vars",
"raw_string": "vars"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,1:6:7-7:1:77",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,2:2:11-6:3:75",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,2:2:11-2:15:24",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,2:2:11-2:15:24",
"value": [
{
"string": "primaryColors",
"raw_string": "primaryColors"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,2:17:26-6:3:75",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,3:4:32-5:5:71",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,3:4:32-3:10:38",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,3:4:32-3:10:38",
"value": [
{
"string": "button",
"raw_string": "button"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,3:12:40-5:5:71",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,4:6:48-4:23:65",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,4:6:48-4:12:54",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,4:6:48-4:12:54",
"value": [
{
"string": "active",
"raw_string": "active"
}
]
}
}
]
},
"primary": {},
"value": {
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,4:14:56-4:23:65",
"value": [
{
"string": "#4baae5",
"raw_string": "#4baae5"
}
]
}
}
}
}
]
}
}
}
}
]
}
}
}
}
]
}
}
}
},
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,9:0:79-14:1:167",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,9:0:79-9:6:85",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,9:0:79-9:6:85",
"value": [
{
"string": "button",
"raw_string": "button"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,9:8:87-14:1:167",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,10:2:91-13:3:165",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,10:2:91-10:7:96",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,10:2:91-10:7:96",
"value": [
{
"string": "style",
"raw_string": "style"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,10:9:98-13:3:165",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,11:4:104-11:20:120",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,11:4:104-11:17:117",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,11:4:104-11:17:117",
"value": [
{
"string": "border-radius",
"raw_string": "border-radius"
}
]
}
}
]
},
"primary": {},
"value": {
"number": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,11:19:119-11:20:120",
"raw": "5",
"value": "5"
}
}
}
},
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,12:4:125-12:40:161",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,12:4:125-12:8:129",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,12:4:125-12:8:129",
"value": [
{
"string": "fill",
"raw_string": "fill"
}
]
}
}
]
},
"primary": {},
"value": {
"double_quoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,4:14:56-4:23:65",
"value": [
{
"string": "#4baae5",
"raw_string": "#4baae5"
}
]
}
}
}
}
]
}
}
}
}
]
}
}
}
}
]
},
"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": "button",
"id_val": "button",
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,9:0:79-9:6:85",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/quoted-var.d2,9:0:79-9:6:85",
"value": [
{
"string": "button",
"raw_string": "button"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "button"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {
"fill": {
"value": "#4baae5"
},
"borderRadius": {
"value": "5"
}
},
"near_key": null,
"shape": {
"value": "rectangle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
}
]
},
"err": null
}

View file

@ -124,10 +124,11 @@
"primary": {}, "primary": {},
"value": { "value": {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/basic/style.d2,5:14:52-5:15:53", "range": "d2/testdata/d2compiler/TestCompile2/vars/basic/style.d2,2:17:26-2:20:29",
"value": [ "value": [
{ {
"string": "red" "string": "red",
"raw_string": "red"
} }
] ]
} }

View file

@ -137,10 +137,11 @@
"primary": {}, "primary": {},
"value": { "value": {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/boards/layer.d2,7:8:51-7:9:52", "range": "d2/testdata/d2compiler/TestCompile2/vars/boards/layer.d2,2:5:14-2:13:22",
"value": [ "value": [
{ {
"string": "im a var" "string": "im a var",
"raw_string": "im a var"
} }
] ]
} }
@ -271,10 +272,11 @@
}, },
"primary": { "primary": {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/boards/layer.d2,7:8:51-7:9:52", "range": "d2/testdata/d2compiler/TestCompile2/vars/boards/layer.d2,2:5:14-2:13:22",
"value": [ "value": [
{ {
"string": "im a var" "string": "im a var",
"raw_string": "im a var"
} }
] ]
} }

View file

@ -199,10 +199,11 @@
"primary": {}, "primary": {},
"value": { "value": {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/boards/overlay.d2,10:7:89-10:8:90", "range": "d2/testdata/d2compiler/TestCompile2/vars/boards/overlay.d2,2:5:14-2:13:22",
"value": [ "value": [
{ {
"string": "im x var" "string": "im x var",
"raw_string": "im x var"
} }
] ]
} }
@ -231,10 +232,11 @@
"primary": {}, "primary": {},
"value": { "value": {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/boards/overlay.d2,11:7:101-11:8:102", "range": "d2/testdata/d2compiler/TestCompile2/vars/boards/overlay.d2,8:9:67-8:17:75",
"value": [ "value": [
{ {
"string": "im y var" "string": "im y var",
"raw_string": "im y var"
} }
] ]
} }
@ -383,10 +385,11 @@
"primary": {}, "primary": {},
"value": { "value": {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/boards/overlay.d2,19:7:173-19:8:174", "range": "d2/testdata/d2compiler/TestCompile2/vars/boards/overlay.d2,2:5:14-2:13:22",
"value": [ "value": [
{ {
"string": "im x var" "string": "im x var",
"raw_string": "im x var"
} }
] ]
} }
@ -415,10 +418,11 @@
"primary": {}, "primary": {},
"value": { "value": {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/boards/overlay.d2,20:7:185-20:8:186", "range": "d2/testdata/d2compiler/TestCompile2/vars/boards/overlay.d2,17:9:151-17:17:159",
"value": [ "value": [
{ {
"string": "im y var" "string": "im y var",
"raw_string": "im y var"
} }
] ]
} }
@ -581,10 +585,11 @@
}, },
"primary": { "primary": {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/boards/overlay.d2,19:7:173-19:8:174", "range": "d2/testdata/d2compiler/TestCompile2/vars/boards/overlay.d2,2:5:14-2:13:22",
"value": [ "value": [
{ {
"string": "im x var" "string": "im x var",
"raw_string": "im x var"
} }
] ]
} }
@ -612,10 +617,11 @@
}, },
"primary": { "primary": {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/boards/overlay.d2,20:7:185-20:8:186", "range": "d2/testdata/d2compiler/TestCompile2/vars/boards/overlay.d2,17:9:151-17:17:159",
"value": [ "value": [
{ {
"string": "im y var" "string": "im y var",
"raw_string": "im y var"
} }
] ]
} }
@ -862,10 +868,11 @@
}, },
"primary": { "primary": {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/boards/overlay.d2,10:7:89-10:8:90", "range": "d2/testdata/d2compiler/TestCompile2/vars/boards/overlay.d2,2:5:14-2:13:22",
"value": [ "value": [
{ {
"string": "im x var" "string": "im x var",
"raw_string": "im x var"
} }
] ]
} }
@ -893,10 +900,11 @@
}, },
"primary": { "primary": {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/boards/overlay.d2,11:7:101-11:8:102", "range": "d2/testdata/d2compiler/TestCompile2/vars/boards/overlay.d2,8:9:67-8:17:75",
"value": [ "value": [
{ {
"string": "im y var" "string": "im y var",
"raw_string": "im y var"
} }
] ]
} }

View file

@ -199,10 +199,11 @@
"primary": {}, "primary": {},
"value": { "value": {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/boards/replace.d2,10:7:98-10:8:99", "range": "d2/testdata/d2compiler/TestCompile2/vars/boards/replace.d2,8:9:67-8:26:84",
"value": [ "value": [
{ {
"string": "im replaced x var" "string": "im replaced x var",
"raw_string": "im replaced x var"
} }
] ]
} }
@ -333,10 +334,11 @@
}, },
"primary": { "primary": {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/boards/replace.d2,10:7:98-10:8:99", "range": "d2/testdata/d2compiler/TestCompile2/vars/boards/replace.d2,8:9:67-8:26:84",
"value": [ "value": [
{ {
"string": "im replaced x var" "string": "im replaced x var",
"raw_string": "im replaced x var"
} }
] ]
} }

View file

@ -137,10 +137,11 @@
"primary": {}, "primary": {},
"value": { "value": {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/boards/scenario.d2,7:8:54-7:9:55", "range": "d2/testdata/d2compiler/TestCompile2/vars/boards/scenario.d2,2:5:14-2:13:22",
"value": [ "value": [
{ {
"string": "im a var" "string": "im a var",
"raw_string": "im a var"
} }
] ]
} }
@ -271,10 +272,11 @@
}, },
"primary": { "primary": {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/boards/scenario.d2,7:8:54-7:9:55", "range": "d2/testdata/d2compiler/TestCompile2/vars/boards/scenario.d2,2:5:14-2:13:22",
"value": [ "value": [
{ {
"string": "im a var" "string": "im a var",
"raw_string": "im a var"
} }
] ]
} }

View file

@ -0,0 +1,11 @@
{
"graph": null,
"err": {
"errs": [
{
"range": "d2/testdata/d2compiler/TestCompile2/vars/errors/nested-missing.d2,6:0:33-6:10:43",
"errmsg": "d2/testdata/d2compiler/TestCompile2/vars/errors/nested-missing.d2:7:1: could not resolve variable \"x.z\""
}
]
}
}

View file

@ -89,10 +89,11 @@
"primary": {}, "primary": {},
"value": { "value": {
"unquoted_string": { "unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/vars/override/label.d2,4:4:29-4:5:30", "range": "d2/testdata/d2compiler/TestCompile2/vars/override/label.d2,2:5:14-2:13:22",
"value": [ "value": [
{ {
"string": "im a var" "string": "im a var",
"raw_string": "im a var"
} }
] ]
} }