Merge pull request #1332 from alixander/no-empty-map

fix `style` validation
This commit is contained in:
Alexander Wang 2023-05-16 10:54:17 -07:00 committed by GitHub
commit 266e8eee8b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 4 deletions

View file

@ -210,8 +210,8 @@ func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) {
c.compileReserved(&obj.Attributes, f) c.compileReserved(&obj.Attributes, f)
return return
} else if f.Name == "style" { } else if f.Name == "style" {
if f.Map() == nil { if f.Map() == nil || len(f.Map().Fields) == 0 {
c.errorf(f.LastRef().AST(), `"style" expected to be set to a map, or contain an additional keyword like "style.opacity: 0.4"`) c.errorf(f.LastRef().AST(), `"style" expected to be set to a map of key-values, or contain an additional keyword like "style.opacity: 0.4"`)
return return
} }
c.compileStyle(&obj.Attributes, f.Map()) c.compileStyle(&obj.Attributes, f.Map())

View file

@ -1700,7 +1700,13 @@ x.a.b`,
name: "tail-style", name: "tail-style",
text: `myobj.style: 3`, text: `myobj.style: 3`,
expErr: `d2/testdata/d2compiler/TestCompile/tail-style.d2:1:7: "style" expected to be set to a map, or contain an additional keyword like "style.opacity: 0.4"`, expErr: `d2/testdata/d2compiler/TestCompile/tail-style.d2:1:7: "style" expected to be set to a map of key-values, or contain an additional keyword like "style.opacity: 0.4"`,
},
{
name: "tail-style-map",
text: `myobj.style: {}`,
expErr: `d2/testdata/d2compiler/TestCompile/tail-style-map.d2:1:7: "style" expected to be set to a map of key-values, or contain an additional keyword like "style.opacity: 0.4"`,
}, },
{ {
name: "bad-style-nesting", name: "bad-style-nesting",

View file

@ -0,0 +1,12 @@
{
"graph": null,
"err": {
"ioerr": null,
"errs": [
{
"range": "d2/testdata/d2compiler/TestCompile/tail-style-map.d2,0:6:6-0:11:11",
"errmsg": "d2/testdata/d2compiler/TestCompile/tail-style-map.d2:1:7: \"style\" expected to be set to a map of key-values, or contain an additional keyword like \"style.opacity: 0.4\""
}
]
}
}

View file

@ -5,7 +5,7 @@
"errs": [ "errs": [
{ {
"range": "d2/testdata/d2compiler/TestCompile/tail-style.d2,0:6:6-0:11:11", "range": "d2/testdata/d2compiler/TestCompile/tail-style.d2,0:6:6-0:11:11",
"errmsg": "d2/testdata/d2compiler/TestCompile/tail-style.d2:1:7: \"style\" expected to be set to a map, or contain an additional keyword like \"style.opacity: 0.4\"" "errmsg": "d2/testdata/d2compiler/TestCompile/tail-style.d2:1:7: \"style\" expected to be set to a map of key-values, or contain an additional keyword like \"style.opacity: 0.4\""
} }
] ]
} }