diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 9610ea6c3..664512b5a 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -90,6 +90,7 @@ func (c *compiler) compileBoard(g *d2graph.Graph, ir *d2ir.Map) *d2graph.Graph { c.validateLabels(g) c.validateNear(g) c.validateEdges(g) + c.validatePositionsCompatibility(g) c.compileBoardsField(g, ir, "layers") c.compileBoardsField(g, ir, "scenarios") @@ -1130,6 +1131,26 @@ func (c *compiler) validateNear(g *d2graph.Graph) { } +func (c *compiler) validatePositionsCompatibility(g *d2graph.Graph) { + for _, o := range g.Objects { + for _, pos := range []*d2graph.Scalar{o.Top, o.Left} { + if pos != nil { + if o.Parent != nil { + if strings.EqualFold(o.Parent.Shape.Value, d2target.ShapeHierarchy) { + c.errorf(pos.MapKey, `position keywords cannot be used with shape "hierarchy"`) + } + if o.OuterSequenceDiagram() != nil { + c.errorf(pos.MapKey, `position keywords cannot be used inside shape "sequence_diagram"`) + } + if o.Parent.GridColumns != nil || o.Parent.GridRows != nil { + c.errorf(pos.MapKey, `position keywords cannot be used with grids`) + } + } + } + } + } +} + func (c *compiler) validateEdges(g *d2graph.Graph) { for _, edge := range g.Edges { // edges from a grid to something outside is ok diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 1ec2b7c0b..b43dec1e1 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -2856,6 +2856,18 @@ d2/testdata/d2compiler/TestCompile/no_arrowheads_in_shape.d2:2:3: "source-arrowh } `, }, + { + name: "fixed-pos-shape-hierarchy", + text: `x: { + shape: hierarchy + a -> b + a.top: 20 + a.left: 20 +} +`, + expErr: `d2/testdata/d2compiler/TestCompile/fixed-pos-shape-hierarchy.d2:4:2: position keywords cannot be used with shape "hierarchy" +d2/testdata/d2compiler/TestCompile/fixed-pos-shape-hierarchy.d2:5:2: position keywords cannot be used with shape "hierarchy"`, + }, } for _, tc := range testCases { diff --git a/testdata/d2compiler/TestCompile/fixed-pos-shape-hierarchy.exp.json b/testdata/d2compiler/TestCompile/fixed-pos-shape-hierarchy.exp.json new file mode 100644 index 000000000..e2ac1e6ee --- /dev/null +++ b/testdata/d2compiler/TestCompile/fixed-pos-shape-hierarchy.exp.json @@ -0,0 +1,15 @@ +{ + "graph": null, + "err": { + "errs": [ + { + "range": "d2/testdata/d2compiler/TestCompile/fixed-pos-shape-hierarchy.d2,3:1:34-3:10:43", + "errmsg": "d2/testdata/d2compiler/TestCompile/fixed-pos-shape-hierarchy.d2:4:2: position keywords cannot be used with shape \"hierarchy\"" + }, + { + "range": "d2/testdata/d2compiler/TestCompile/fixed-pos-shape-hierarchy.d2,4:1:45-4:11:55", + "errmsg": "d2/testdata/d2compiler/TestCompile/fixed-pos-shape-hierarchy.d2:5:2: position keywords cannot be used with shape \"hierarchy\"" + } + ] + } +}