Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

d2compiler: disallow non-sibling near keys #2002

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 3 additions & 21 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -1067,27 +1067,9 @@ func (c *compiler) validateNear(g *d2graph.Graph) {
nearObj, isKey := g.Root.HasChild(d2graph.Key(obj.NearKey))
_, isConst := d2graph.NearConstants[d2graph.Key(obj.NearKey)[0]]
if isKey {
// Doesn't make sense to set near to an ancestor or descendant
nearIsAncestor := false
for curr := obj; curr != nil; curr = curr.Parent {
if curr == nearObj {
nearIsAncestor = true
break
}
}
if nearIsAncestor {
c.errorf(obj.NearKey, "near keys cannot be set to an ancestor")
continue
}
nearIsDescendant := false
for curr := nearObj; curr != nil; curr = curr.Parent {
if curr == obj {
nearIsDescendant = true
break
}
}
if nearIsDescendant {
c.errorf(obj.NearKey, "near keys cannot be set to an descendant")
// Doesn't make sense to set near to a non-sibling
if obj.Parent != nearObj.Parent {
c.errorf(obj.NearKey, "near keys cannot be set to a non-sibling")
continue
}
if nearObj.OuterSequenceDiagram() != nil {
Expand Down
10 changes: 6 additions & 4 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1598,10 +1598,12 @@ b: {
a: {
near: a.b
b
c.near: mongodb
}
`,
expErr: `d2/testdata/d2compiler/TestCompile/near-invalid.d2:9:11: near keys cannot be set to an ancestor
d2/testdata/d2compiler/TestCompile/near-invalid.d2:14:9: near keys cannot be set to an descendant`,
expErr: `d2/testdata/d2compiler/TestCompile/near-invalid.d2:9:11: near keys cannot be set to a non-sibling
d2/testdata/d2compiler/TestCompile/near-invalid.d2:14:9: near keys cannot be set to a non-sibling
d2/testdata/d2compiler/TestCompile/near-invalid.d2:16:10: near keys cannot be set to a non-sibling`,
},
{
name: "near_bad_constant",
Expand All @@ -1619,7 +1621,7 @@ z: {
x
}
`,
expErr: `d2/testdata/d2compiler/TestCompile/near_special.d2:1:9: near keys cannot be set to descendants of special objects, like grid cells`,
expErr: `d2/testdata/d2compiler/TestCompile/near_special.d2:1:9: near keys cannot be set to a non-sibling`,
},
{
name: "near_bad_connected",
Expand Down Expand Up @@ -2007,7 +2009,7 @@ dst.id <-> src.dst_id
}
b.near: x.a
`,
expErr: `d2/testdata/d2compiler/TestCompile/near_sequence.d2:5:9: near keys cannot be set to an object within sequence diagrams`,
expErr: `d2/testdata/d2compiler/TestCompile/near_sequence.d2:5:9: near keys cannot be set to a non-sibling`,
},
{
name: "sequence-timestamp",
Expand Down
8 changes: 6 additions & 2 deletions testdata/d2compiler/TestCompile/near-invalid.exp.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testdata/d2compiler/TestCompile/near_sequence.exp.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testdata/d2compiler/TestCompile/near_special.exp.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading