fix dimension with style

This commit is contained in:
Alexander Wang 2023-01-06 11:01:45 -08:00
parent 80892f9ff9
commit 54368d8672
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
3 changed files with 193 additions and 1 deletions

View file

@ -836,7 +836,7 @@ func (c *compiler) validateKey(obj *d2graph.Object, m *d2ast.Map, mk *d2ast.Key)
switch strings.ToLower(obj.Attributes.Shape.Value) {
case d2target.ShapeSQLTable, d2target.ShapeClass:
default:
if len(obj.Children) > 0 && (reserved == "width" || reserved == "height") {
if len(obj.Children) > 0 && !(len(obj.Children) == 1 && obj.ChildrenArray[0].ID == "style") && (reserved == "width" || reserved == "height") {
c.errorf(mk.Range.Start, mk.Range.End, fmt.Sprintf("%s cannot be used on container: %s", reserved, obj.AbsID()))
}
}

View file

@ -208,6 +208,15 @@ d2/testdata/d2compiler/TestCompile/no_dimensions_on_containers.d2:25:3: width ca
d2/testdata/d2compiler/TestCompile/no_dimensions_on_containers.d2:26:3: height cannot be used on container: containers.oval container
d2/testdata/d2compiler/TestCompile/no_dimensions_on_containers.d2:36:3: width cannot be used on container: containers.hexagon container
d2/testdata/d2compiler/TestCompile/no_dimensions_on_containers.d2:37:3: height cannot be used on container: containers.hexagon container
`,
},
{
name: "dimension_with_style",
text: `x: {
width: 200
style.multiple: true
}
`,
},
{

View file

@ -0,0 +1,183 @@
{
"graph": {
"ast": {
"range": "d2/testdata/d2compiler/TestCompile/dimension_with_style.d2,0:0:0-4:0:43",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile/dimension_with_style.d2,0:0:0-3:1:42",
"key": {
"range": "d2/testdata/d2compiler/TestCompile/dimension_with_style.d2,0:0:0-0:1:1",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/dimension_with_style.d2,0:0:0-0:1:1",
"value": [
{
"string": "x",
"raw_string": "x"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile/dimension_with_style.d2,0:3:3-3:0:41",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile/dimension_with_style.d2,1:2:7-1:12:17",
"key": {
"range": "d2/testdata/d2compiler/TestCompile/dimension_with_style.d2,1:2:7-1:7:12",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/dimension_with_style.d2,1:2:7-1:7:12",
"value": [
{
"string": "width",
"raw_string": "width"
}
]
}
}
]
},
"primary": {},
"value": {
"number": {
"range": "d2/testdata/d2compiler/TestCompile/dimension_with_style.d2,1:9:14-1:12:17",
"raw": "200",
"value": "200"
}
}
}
},
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile/dimension_with_style.d2,2:2:20-2:22:40",
"key": {
"range": "d2/testdata/d2compiler/TestCompile/dimension_with_style.d2,2:2:20-2:16:34",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/dimension_with_style.d2,2:2:20-2:7:25",
"value": [
{
"string": "style",
"raw_string": "style"
}
]
}
},
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/dimension_with_style.d2,2:8:26-2:16:34",
"value": [
{
"string": "multiple",
"raw_string": "multiple"
}
]
}
}
]
},
"primary": {},
"value": {
"boolean": {
"range": "d2/testdata/d2compiler/TestCompile/dimension_with_style.d2,2:18:36-2:22:40",
"value": true
}
}
}
}
]
}
}
}
}
]
},
"root": {
"id": "",
"id_val": "",
"label_dimensions": {
"width": 0,
"height": 0
},
"attributes": {
"label": {
"value": ""
},
"style": {},
"near_key": null,
"shape": {
"value": ""
},
"direction": {
"value": ""
}
},
"zIndex": 0
},
"edges": [],
"objects": [
{
"id": "x",
"id_val": "x",
"label_dimensions": {
"width": 0,
"height": 0
},
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile/dimension_with_style.d2,0:0:0-0:1:1",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile/dimension_with_style.d2,0:0:0-0:1:1",
"value": [
{
"string": "x",
"raw_string": "x"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": 0
}
],
"attributes": {
"label": {
"value": "x"
},
"style": {
"multiple": {
"value": "true"
}
},
"width": {
"value": "200"
},
"near_key": null,
"shape": {
"value": ""
},
"direction": {
"value": ""
}
},
"zIndex": 0
}
]
},
"err": null
}