add class array footgun

This commit is contained in:
Alexander Wang 2023-06-06 20:24:47 -07:00
parent 7d56c2dbb4
commit 0d4392eac2
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
3 changed files with 44 additions and 0 deletions

View file

@ -147,6 +147,21 @@ func (c *compiler) compileMap(obj *d2graph.Object, m *d2ir.Map) {
classMap := m.GetClassMap(className)
if classMap != nil {
c.compileMap(obj, classMap)
} else {
if strings.Contains(className, ",") {
split := strings.Split(className, ",")
allFound := true
for _, maybeClassName := range split {
maybeClassName = strings.TrimSpace(maybeClassName)
if m.GetClassMap(maybeClassName) == nil {
allFound = false
break
}
}
if allFound {
c.errorf(class.LastRef().AST(), `class "%s" not found. Did you mean to use ";" to separate array items?`, className)
}
}
}
}
}

View file

@ -2482,6 +2482,23 @@ nostar -> 1star: { class: [path; path2] }
tassert.Equal(t, "2", g.Edges[0].Style.StrokeWidth.Value)
},
},
{
name: "comma-array-class",
text: `classes: {
dragon_ball: {
label: ""
shape: circle
style.fill: orange
}
path: {
label: "then"
style.stroke-width: 4
}
}
nostar: { class: [dragon_ball, path] }`,
expErr: `d2/testdata/d2compiler/TestCompile/comma-array-class.d2:12:11: class "dragon_ball, path" not found. Did you mean to use ";" to separate array items?`,
},
{
name: "reordered-classes",
text: `classes: {

View file

@ -0,0 +1,12 @@
{
"graph": null,
"err": {
"ioerr": null,
"errs": [
{
"range": "d2/testdata/d2compiler/TestCompile/comma-array-class.d2,11:10:157-11:15:162",
"errmsg": "d2/testdata/d2compiler/TestCompile/comma-array-class.d2:12:11: class \"dragon_ball, path\" not found. Did you mean to use \";\" to separate array items?"
}
]
}
}