diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 424c2795d..8f71f1a6b 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -736,12 +736,14 @@ func (c *compiler) validateNear(g *d2graph.Graph) { continue } - descendantsMap := getNearDescendants(obj) connectToOutside := false for _, edge := range g.Edges { - if (descendantsMap[edge.Src] && !descendantsMap[edge.Dst]) || - (!descendantsMap[edge.Src] && descendantsMap[edge.Dst]) { + srcNearContainer := edge.Src.OuterNearContainer() + dstNearContainer := edge.Dst.OuterNearContainer() + + if srcNearContainer != dstNearContainer { connectToOutside = true + break } } @@ -757,24 +759,6 @@ func (c *compiler) validateNear(g *d2graph.Graph) { } } -func getNearDescendants(nearObj *d2graph.Object) map[*d2graph.Object]bool { - descendantsMap := make(map[*d2graph.Object]bool) - - var helper func(obj *d2graph.Object) - - helper = func(obj *d2graph.Object) { - if obj.ChildrenArray != nil { - for _, child := range obj.ChildrenArray { - descendantsMap[child] = true - helper(child) - } - } - } - - helper(nearObj) - return descendantsMap -} - func (c *compiler) validateBoardLinks(g *d2graph.Graph) { for _, obj := range g.Objects { if obj.Attributes.Link == nil {