Merge pull request #2011 from alixander/fix-link-panic
d2ir: fix link panic
This commit is contained in:
commit
e269a0dc1d
6 changed files with 76 additions and 0 deletions
|
|
@ -18,3 +18,4 @@
|
||||||
- Board links imported with spread imports work [#1972](https://github.com/terrastruct/d2/pull/1972)
|
- 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 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)
|
- 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)
|
||||||
|
|
|
||||||
|
|
@ -2986,6 +2986,38 @@ layers: {
|
||||||
tassert.Equal(t, "root.layers.x.layers.k", g.Layers[0].Layers[0].Objects[1].Link.Value)
|
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",
|
name: "import-nested-layers",
|
||||||
text: `k
|
text: `k
|
||||||
|
|
|
||||||
|
|
@ -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
|
// 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) {
|
func (c *compiler) extendLinks(m *Map, importF *Field) {
|
||||||
|
nodeBoardKind := NodeBoardKind(m)
|
||||||
for _, f := range m.Fields {
|
for _, f := range m.Fields {
|
||||||
if f.Name == "link" {
|
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()
|
val := f.Primary().Value.ScalarString()
|
||||||
link, err := d2parser.ParseKey(val)
|
link, err := d2parser.ParseKey(val)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -879,8 +884,13 @@ func (c *compiler) extendLinks(m *Map, importF *Field) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *compiler) updateLinks(m *Map) {
|
func (c *compiler) updateLinks(m *Map) {
|
||||||
|
nodeBoardKind := NodeBoardKind(m)
|
||||||
for _, f := range m.Fields {
|
for _, f := range m.Fields {
|
||||||
if f.Name == "link" {
|
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()
|
val := f.Primary().Value.ScalarString()
|
||||||
link, err := d2parser.ParseKey(val)
|
link, err := d2parser.ParseKey(val)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
11
testdata/d2compiler/TestCompile/invalid-link-1.exp.json
generated
vendored
Normal file
11
testdata/d2compiler/TestCompile/invalid-link-1.exp.json
generated
vendored
Normal 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-2.exp.json
generated
vendored
Normal file
11
testdata/d2compiler/TestCompile/invalid-link-2.exp.json
generated
vendored
Normal 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
11
testdata/d2compiler/TestCompile/invalid-link.exp.json
generated
vendored
Normal 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"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue