add compiler error for using width or height on a container

This commit is contained in:
Gavin Nishizawa 2022-12-28 21:14:25 -08:00
parent fbf48fa0ff
commit ae4e6364ea
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD
4 changed files with 102 additions and 7 deletions

View file

@ -833,6 +833,14 @@ func (c *compiler) validateKey(obj *d2graph.Object, m *d2ast.Map, mk *d2ast.Key)
return
}
switch strings.ToLower(obj.Attributes.Shape.Value) {
case d2target.ShapeSQLTable, d2target.ShapeClass:
default:
if len(obj.Children) > 0 && (reserved == "width" || reserved == "height") {
c.errorf(mk.Range.Start, mk.Range.End, fmt.Sprintf("%s cannot be used on container: %s", reserved, obj.AbsID()))
}
}
if len(mk.Edges) > 0 {
return
}

View file

@ -152,6 +152,64 @@ d2/testdata/d2compiler/TestCompile/equal_dimensions_on_circle.d2:4:2: width and
}
},
},
{
name: "no_dimensions_on_containers",
text: `
containers: {
circle container: {
shape: circle
width: 512
diamond: {
shape: diamond
width: 128
height: 64
}
}
diamond container: {
shape: diamond
width: 512
height: 256
circle: {
shape: circle
width: 128
}
}
oval container: {
shape: oval
width: 512
height: 256
hexagon: {
shape: hexagon
width: 128
height: 64
}
}
hexagon container: {
shape: hexagon
width: 512
height: 256
oval: {
shape: oval
width: 128
height: 64
}
}
}
`,
expErr: `d2/testdata/d2compiler/TestCompile/no_dimensions_on_containers.d2:5:3: width cannot be used on container: containers.circle container
d2/testdata/d2compiler/TestCompile/no_dimensions_on_containers.d2:15:3: width cannot be used on container: containers.diamond container
d2/testdata/d2compiler/TestCompile/no_dimensions_on_containers.d2:16:3: height cannot be used on container: containers.diamond container
d2/testdata/d2compiler/TestCompile/no_dimensions_on_containers.d2:25:3: width cannot be used on container: containers.oval container
d2/testdata/d2compiler/TestCompile/no_dimensions_on_containers.d2:26:3: height cannot be used on container: containers.oval container
d2/testdata/d2compiler/TestCompile/no_dimensions_on_containers.d2:36:3: width cannot be used on container: containers.hexagon container
d2/testdata/d2compiler/TestCompile/no_dimensions_on_containers.d2:37:3: height cannot be used on container: containers.hexagon container
`,
},
{
name: "basic_icon",

View file

@ -97,7 +97,6 @@ this is a message group: {
containers: {
circle container: {
shape: circle
width: 512
diamond: {
shape: diamond
@ -107,8 +106,6 @@ containers: {
}
diamond container: {
shape: diamond
width: 512
height: 256
circle: {
shape: circle
@ -117,8 +114,6 @@ containers: {
}
oval container: {
shape: oval
width: 512
height: 256
hexagon: {
shape: hexagon
@ -128,8 +123,6 @@ containers: {
}
hexagon container: {
shape: hexagon
width: 512
height: 256
oval: {
shape: oval

View file

@ -0,0 +1,36 @@
{
"graph": null,
"err": {
"ioerr": null,
"errs": [
{
"range": "d2/testdata/d2compiler/TestCompile/no_dimensions_on_containers.d2,4:2:54-4:12:64",
"errmsg": "d2/testdata/d2compiler/TestCompile/no_dimensions_on_containers.d2:5:3: width cannot be used on container: containers.circle container"
},
{
"range": "d2/testdata/d2compiler/TestCompile/no_dimensions_on_containers.d2,14:2:173-14:12:183",
"errmsg": "d2/testdata/d2compiler/TestCompile/no_dimensions_on_containers.d2:15:3: width cannot be used on container: containers.diamond container"
},
{
"range": "d2/testdata/d2compiler/TestCompile/no_dimensions_on_containers.d2,15:2:186-15:13:197",
"errmsg": "d2/testdata/d2compiler/TestCompile/no_dimensions_on_containers.d2:16:3: height cannot be used on container: containers.diamond container"
},
{
"range": "d2/testdata/d2compiler/TestCompile/no_dimensions_on_containers.d2,24:2:284-24:12:294",
"errmsg": "d2/testdata/d2compiler/TestCompile/no_dimensions_on_containers.d2:25:3: width cannot be used on container: containers.oval container"
},
{
"range": "d2/testdata/d2compiler/TestCompile/no_dimensions_on_containers.d2,25:2:297-25:13:308",
"errmsg": "d2/testdata/d2compiler/TestCompile/no_dimensions_on_containers.d2:26:3: height cannot be used on container: containers.oval container"
},
{
"range": "d2/testdata/d2compiler/TestCompile/no_dimensions_on_containers.d2,35:2:417-35:12:427",
"errmsg": "d2/testdata/d2compiler/TestCompile/no_dimensions_on_containers.d2:36:3: width cannot be used on container: containers.hexagon container"
},
{
"range": "d2/testdata/d2compiler/TestCompile/no_dimensions_on_containers.d2,36:2:430-36:13:441",
"errmsg": "d2/testdata/d2compiler/TestCompile/no_dimensions_on_containers.d2:37:3: height cannot be used on container: containers.hexagon container"
}
]
}
}