From ae4e6364ea544dad24aaa9123805b56a8309130d Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Wed, 28 Dec 2022 21:14:25 -0800 Subject: [PATCH] add compiler error for using width or height on a container --- d2compiler/compile.go | 8 +++ d2compiler/compile_test.go | 58 +++++++++++++++++++ e2etests/todo_test.go | 7 --- .../no_dimensions_on_containers.exp.json | 36 ++++++++++++ 4 files changed, 102 insertions(+), 7 deletions(-) create mode 100644 testdata/d2compiler/TestCompile/no_dimensions_on_containers.exp.json diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 7737950e1..2e4d75c3b 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -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 } diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index f0f729be7..52a2e3149 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -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", diff --git a/e2etests/todo_test.go b/e2etests/todo_test.go index 3397dcb60..d70d98367 100644 --- a/e2etests/todo_test.go +++ b/e2etests/todo_test.go @@ -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 diff --git a/testdata/d2compiler/TestCompile/no_dimensions_on_containers.exp.json b/testdata/d2compiler/TestCompile/no_dimensions_on_containers.exp.json new file mode 100644 index 000000000..ec21bacd7 --- /dev/null +++ b/testdata/d2compiler/TestCompile/no_dimensions_on_containers.exp.json @@ -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" + } + ] + } +}