Merge pull request #2448 from alixander/fix-classes-panic

d2ir: fix classes panic
This commit is contained in:
Alexander Wang 2025-03-23 06:39:47 -07:00 committed by GitHub
commit f22d8a43c7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 52 additions and 6 deletions

View file

@ -25,6 +25,7 @@
- fixes panic when comment lines appear in arrays [#2378](https://github.com/terrastruct/d2/pull/2378)
- fixes inconsistencies when objects were double quoted [#2390](https://github.com/terrastruct/d2/pull/2390)
- fixes globs not applying to spread substitutions [#2426](https://github.com/terrastruct/d2/issues/2426)
- fixes panic when classes were mixed with layers incorrectly [#2448](https://github.com/terrastruct/d2/pull/2448)
- CLI: fetch and render remote images of mimetype octet-stream correctly [#2370](https://github.com/terrastruct/d2/pull/2370)
- d2js: handle unicode characters [#2393](https://github.com/terrastruct/d2/pull/2393)

View file

@ -328,7 +328,7 @@ containers: {
Steps
}
`,
expErr: `d2/testdata/d2compiler/TestCompile/image_children_Steps.d2:4:3: steps is only allowed at a board root`,
expErr: `d2/testdata/d2compiler/TestCompile/image_children_Steps.d2:4:3: steps must be declared at a board root scope`,
},
{
name: "name-with-dot-underscore",
@ -1714,6 +1714,40 @@ steps: {
assert.Equal(t, 1, len(g.Layers[0].Steps))
},
},
{
name: "import-classes-boards",
text: `classes: {
a: {
label: hi
}
}
layers: {
asdf: {
qwer: {
layers: {
ok: {
bok
}
}
}
}
wert: {
classes: @classes
}
}
`,
files: map[string]string{
"classes.d2": `
c: {
label: bye
}
`,
},
expErr: `d2/testdata/d2compiler/TestCompile/import-classes-boards.d2:10:7: layers must be declared at a board root scope`,
},
{
name: "import_url_link",

View file

@ -113,7 +113,7 @@ func (c *compiler) overlayClasses(m *Map) {
if lClasses == nil {
lClasses = classes.Copy(l).(*Field)
l.Fields = append(l.Fields, lClasses)
} else {
} else if lClasses.Map() != nil {
base := classes.Copy(l).(*Field)
OverlayMap(base.Map(), lClasses.Map())
l.DeleteField("classes")

View file

@ -593,7 +593,7 @@ classes: {
}
}
`)
assert.ErrorString(t, err, `TestCompile/classes/nonroot.d2:2:3: classes is only allowed at a board root`)
assert.ErrorString(t, err, `TestCompile/classes/nonroot.d2:2:3: classes must be declared at a board root scope`)
},
},
{

View file

@ -942,11 +942,11 @@ func (m *Map) ensureField(i int, kp *d2ast.KeyPath, refctx *RefContext, create b
}
if headString == "classes" && head.IsUnquoted() && NodeBoardKind(m) == "" {
return d2parser.Errorf(kp.Path[i].Unbox(), "%s is only allowed at a board root", headString)
return d2parser.Errorf(kp.Path[i].Unbox(), "%s must be declared at a board root scope", headString)
}
if findBoardKeyword(head) != -1 && head.IsUnquoted() && NodeBoardKind(m) == "" {
return d2parser.Errorf(kp.Path[i].Unbox(), "%s is only allowed at a board root", headString)
return d2parser.Errorf(kp.Path[i].Unbox(), "%s must be declared at a board root scope", headString)
}
for _, f := range m.Fields {

View file

@ -4,7 +4,7 @@
"errs": [
{
"range": "d2/testdata/d2compiler/TestCompile/image_children_Steps.d2,3:2:115-3:7:120",
"errmsg": "d2/testdata/d2compiler/TestCompile/image_children_Steps.d2:4:3: steps is only allowed at a board root"
"errmsg": "d2/testdata/d2compiler/TestCompile/image_children_Steps.d2:4:3: steps must be declared at a board root scope"
}
]
}

View file

@ -0,0 +1,11 @@
{
"graph": null,
"err": {
"errs": [
{
"range": "d2/testdata/d2compiler/TestCompile/import-classes-boards.d2,9:6:75-9:12:81",
"errmsg": "d2/testdata/d2compiler/TestCompile/import-classes-boards.d2:10:7: layers must be declared at a board root scope"
}
]
}
}