From ad2e3af9007d09726d5ec95dc8c88e44f88b7703 Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Tue, 28 Mar 2023 11:37:40 -0700 Subject: [PATCH 1/2] add compiler error for neara set to title with constant near --- d2compiler/compile.go | 7 +++++++ d2compiler/compile_test.go | 13 +++++++++++++ .../d2compiler/TestCompile/near_near_const.exp.json | 12 ++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 testdata/d2compiler/TestCompile/near_near_const.exp.json diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 06dc6578f..28288795d 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -726,6 +726,13 @@ func (c *compiler) validateNear(g *d2graph.Graph) { c.errorf(obj.Attributes.NearKey, "near keys cannot be set to an object within sequence diagrams") continue } + if nearObj.Attributes.NearKey != nil { + _, nearObjNearIsConst := d2graph.NearConstants[d2graph.Key(nearObj.Attributes.NearKey)[0]] + if nearObjNearIsConst { + c.errorf(obj.Attributes.NearKey, "near keys cannot be set to an object with a constant near key") + continue + } + } } else if isConst { is := false for _, e := range g.Edges { diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 845e0c41a..53e33e22f 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -2255,6 +2255,19 @@ layers: { }`, expErr: `d2/testdata/d2compiler/TestCompile/link-board-underscore-not-found.d2:7:9: invalid underscore usage`, }, + { + name: "near_near_const", + text: ` +title: Title { + near: top-center +} + +obj { + near: title +} +`, + expErr: `d2/testdata/d2compiler/TestCompile/near_near_const.d2:7:8: near keys cannot be set to an object with a constant near key`, + }, } for _, tc := range testCases { diff --git a/testdata/d2compiler/TestCompile/near_near_const.exp.json b/testdata/d2compiler/TestCompile/near_near_const.exp.json new file mode 100644 index 000000000..7b5fe99af --- /dev/null +++ b/testdata/d2compiler/TestCompile/near_near_const.exp.json @@ -0,0 +1,12 @@ +{ + "graph": null, + "err": { + "ioerr": null, + "errs": [ + { + "range": "d2/testdata/d2compiler/TestCompile/near_near_const.d2,6:7:50-6:12:55", + "errmsg": "d2/testdata/d2compiler/TestCompile/near_near_const.d2:7:8: near keys cannot be set to an object with a constant near key" + } + ] + } +} From 75536ebf8c74ceebe509f5fdd9aaea05962a8d6a Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Tue, 28 Mar 2023 11:41:59 -0700 Subject: [PATCH 2/2] changelog --- ci/release/changelogs/next.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index 71960dc02..5a1d75909 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -8,3 +8,5 @@ - prevent `tooltip` to be an URL when `link` is already set. [#1091](https://github.com/terrastruct/d2/pull/1091) #### Bugfixes ⛑️ + +- prevent an object's `near` from targeting a text near a constant. [#1100](https://github.com/terrastruct/d2/pull/1100)