validate position keywords

This commit is contained in:
Alexander Wang 2024-03-15 15:26:00 -07:00
parent fcb0678abb
commit 4ffe2409a7
No known key found for this signature in database
GPG key ID: BE3937D0D52D8927
3 changed files with 48 additions and 0 deletions

View file

@ -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

View file

@ -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 {

View file

@ -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\""
}
]
}
}