d2ir: fix link panic

This commit is contained in:
Alexander Wang 2024-07-19 15:03:22 -06:00
parent e5bdb5f740
commit cf81d7c033
No known key found for this signature in database
GPG key ID: BE3937D0D52D8927
6 changed files with 76 additions and 0 deletions

View file

@ -18,3 +18,4 @@
- Board links imported with spread imports work [#1972](https://github.com/terrastruct/d2/pull/1972)
- Fix importing a file with nested boards [#1998](https://github.com/terrastruct/d2/pull/1998)
- Fix importing a file with underscores in links [#1999](https://github.com/terrastruct/d2/pull/1999)
- Replace a panic with an error message resulting from invalid `link` usage [#2011](https://github.com/terrastruct/d2/pull/2011)

View file

@ -2986,6 +2986,38 @@ layers: {
tassert.Equal(t, "root.layers.x.layers.k", g.Layers[0].Layers[0].Objects[1].Link.Value)
},
},
{
name: "invalid-link-1",
text: `k
layers: {
x: {...@x}
}`,
files: map[string]string{
"x.d2": `k
layers: {
y.link: @n
}`,
"n.d2": `n`,
},
expErr: `d2/testdata/d2compiler/TestCompile/x.d2:3:5: a board itself cannot be linked; only objects within a board can be linked`,
},
{
name: "invalid-link-2",
text: `k
layers: {
x: @x
}`,
files: map[string]string{
"x.d2": `k
layers: {
y.link: @n
}`,
"n.d2": `n`,
},
expErr: `d2/testdata/d2compiler/TestCompile/x.d2:3:5: a board itself cannot be linked; only objects within a board can be linked`,
},
{
name: "import-nested-layers",
text: `k

View file

@ -854,8 +854,13 @@ func (c *compiler) ignoreLazyGlob(n Node) bool {
// When importing a file, all of its board links need to be extended to reflect their new path
func (c *compiler) extendLinks(m *Map, importF *Field) {
nodeBoardKind := NodeBoardKind(m)
for _, f := range m.Fields {
if f.Name == "link" {
if nodeBoardKind != "" {
c.errorf(f.LastRef().AST(), "a board itself cannot be linked; only objects within a board can be linked")
continue
}
val := f.Primary().Value.ScalarString()
link, err := d2parser.ParseKey(val)
if err != nil {
@ -879,8 +884,13 @@ func (c *compiler) extendLinks(m *Map, importF *Field) {
}
func (c *compiler) updateLinks(m *Map) {
nodeBoardKind := NodeBoardKind(m)
for _, f := range m.Fields {
if f.Name == "link" {
if nodeBoardKind != "" {
c.errorf(f.LastRef().AST(), "a board itself cannot be linked; only objects within a board can be linked")
continue
}
val := f.Primary().Value.ScalarString()
link, err := d2parser.ParseKey(val)
if err != nil {

View file

@ -0,0 +1,11 @@
{
"graph": null,
"err": {
"errs": [
{
"range": "d2/testdata/d2compiler/TestCompile/x.d2,2:4:16-2:8:20",
"errmsg": "d2/testdata/d2compiler/TestCompile/x.d2:3:5: a board itself cannot be linked; only objects within a board can be linked"
}
]
}
}

View file

@ -0,0 +1,11 @@
{
"graph": null,
"err": {
"errs": [
{
"range": "d2/testdata/d2compiler/TestCompile/x.d2,2:4:16-2:8:20",
"errmsg": "d2/testdata/d2compiler/TestCompile/x.d2:3:5: a board itself cannot be linked; only objects within a board can be linked"
}
]
}
}

11
testdata/d2compiler/TestCompile/invalid-link.exp.json generated vendored Normal file
View file

@ -0,0 +1,11 @@
{
"graph": null,
"err": {
"errs": [
{
"range": "d2/testdata/d2compiler/TestCompile/x.d2,2:4:16-2:8:20",
"errmsg": "d2/testdata/d2compiler/TestCompile/x.d2:3:5: a board itself cannot be linked; only objects within a board can be linked"
}
]
}
}