From 731a7df195e2fe4b801c0a90a0f8a426a23803eb Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Mon, 18 Sep 2023 13:16:50 -0700 Subject: [PATCH 1/4] validate text shape labels --- d2compiler/compile.go | 12 ++++++++++++ d2compiler/compile_test.go | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 88db84d7e7..36ce88bbab 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -82,6 +82,7 @@ func (c *compiler) compileBoard(g *d2graph.Graph, ir *d2ir.Map) *d2graph.Graph { if len(c.err.Errors) == 0 { c.validateKeys(g.Root, ir) } + c.validateLabels(g) c.validateNear(g) c.validateEdges(g) @@ -998,6 +999,17 @@ func (c *compiler) validateKey(obj *d2graph.Object, f *d2ir.Field) { } } +func (c *compiler) validateLabels(g *d2graph.Graph) { + for _, obj := range g.Objects { + if obj.Shape.Value == d2target.ShapeText { + if strings.TrimSpace(obj.Label.Value) == "" { + c.errorf(obj.Label.MapKey, "text must have a label") + continue + } + } + } +} + func (c *compiler) validateNear(g *d2graph.Graph) { for _, obj := range g.Objects { if obj.NearKey != nil { diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index a8788e95e5..3c33b03ce8 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -2712,6 +2712,26 @@ object: { `, expErr: `d2/testdata/d2compiler/TestCompile/reserved-composite.d2:1:1: reserved field shape does not accept composite`, }, + { + name: "text_no_label", + text: `a: "ok" { + shape: text +} +b: " \n " { + shape: text +} + +c: "" { + shape: text +} +d: "" { + shape: circle +} +e: " \n " +`, + expErr: ` +`, + }, } for _, tc := range testCases { From 75ad59838b8c305f4238a9d041982d72db1d4595 Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Mon, 18 Sep 2023 13:57:37 -0700 Subject: [PATCH 2/4] validate empty text labels --- d2compiler/compile.go | 15 ++++++++++----- d2compiler/compile_test.go | 5 ++++- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 36ce88bbab..9a9337585f 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -1001,11 +1001,16 @@ func (c *compiler) validateKey(obj *d2graph.Object, f *d2ir.Field) { func (c *compiler) validateLabels(g *d2graph.Graph) { for _, obj := range g.Objects { - if obj.Shape.Value == d2target.ShapeText { - if strings.TrimSpace(obj.Label.Value) == "" { - c.errorf(obj.Label.MapKey, "text must have a label") - continue - } + if obj.Shape.Value != d2target.ShapeText { + continue + } + if obj.Attributes.Language != "" { + // blockstrings have already been validated + continue + } + if strings.TrimSpace(obj.Label.Value) == "" { + c.errorf(obj.Label.MapKey, "shape text must have a non-empty label") + continue } } } diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 3c33b03ce8..98045e9964 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -2720,7 +2720,6 @@ object: { b: " \n " { shape: text } - c: "" { shape: text } @@ -2728,6 +2727,10 @@ d: "" { shape: circle } e: " \n " +f: |md | +g: |md + +| `, expErr: ` `, From 095e23ddad3b7c4cab7019e9447de694aa722b30 Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Mon, 18 Sep 2023 14:03:32 -0700 Subject: [PATCH 3/4] save test --- d2compiler/compile_test.go | 6 +++-- .../TestCompile/text_no_label.exp.json | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 testdata/d2compiler/TestCompile/text_no_label.exp.json diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 98045e9964..608d92c97f 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -2732,8 +2732,10 @@ g: |md | `, - expErr: ` -`, + expErr: `d2/testdata/d2compiler/TestCompile/text_no_label.d2:14:1: block string cannot be empty +d2/testdata/d2compiler/TestCompile/text_no_label.d2:15:1: block string cannot be empty +d2/testdata/d2compiler/TestCompile/text_no_label.d2:4:1: shape text must have a non-empty label +d2/testdata/d2compiler/TestCompile/text_no_label.d2:7:1: shape text must have a non-empty label`, }, } diff --git a/testdata/d2compiler/TestCompile/text_no_label.exp.json b/testdata/d2compiler/TestCompile/text_no_label.exp.json new file mode 100644 index 0000000000..d0a3956b0c --- /dev/null +++ b/testdata/d2compiler/TestCompile/text_no_label.exp.json @@ -0,0 +1,23 @@ +{ + "graph": null, + "err": { + "errs": [ + { + "range": "d2/testdata/d2compiler/TestCompile/text_no_label.d2,13:0:110-13:9:119", + "errmsg": "d2/testdata/d2compiler/TestCompile/text_no_label.d2:14:1: block string cannot be empty" + }, + { + "range": "d2/testdata/d2compiler/TestCompile/text_no_label.d2,14:0:120-16:1:129", + "errmsg": "d2/testdata/d2compiler/TestCompile/text_no_label.d2:15:1: block string cannot be empty" + }, + { + "range": "d2/testdata/d2compiler/TestCompile/text_no_label.d2,3:0:25-5:1:51", + "errmsg": "d2/testdata/d2compiler/TestCompile/text_no_label.d2:4:1: shape text must have a non-empty label" + }, + { + "range": "d2/testdata/d2compiler/TestCompile/text_no_label.d2,6:0:52-8:1:74", + "errmsg": "d2/testdata/d2compiler/TestCompile/text_no_label.d2:7:1: shape text must have a non-empty label" + } + ] + } +} From 078559e5ea9338c1d5f256e815e2d5eb03a0570c Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Mon, 18 Sep 2023 14:07:09 -0700 Subject: [PATCH 4/4] changelog --- ci/release/changelogs/next.md | 1 + 1 file changed, 1 insertion(+) diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index 9c413894ac..cbc406562c 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -13,3 +13,4 @@ - Fixes `d2 fmt` to format all files passed as arguments rather than first non-formatted only [#1523](https://github.com/terrastruct/d2/issues/1523) - Fixes Markdown cropping last element in mixed-element blocks (e.g. em and strong) [#1543](https://github.com/terrastruct/d2/issues/1543) +- Fixes missing compile error for non-blockstring empty labels [#1590](https://github.com/terrastruct/d2/issues/1590)