From 49a42edeec5a9fbb0ed698de41f03aacf73180da Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Tue, 27 Feb 2024 13:16:22 -0800 Subject: [PATCH] prevent near special --- ci/release/changelogs/next.md | 3 ++- d2compiler/compile.go | 8 ++++++++ d2compiler/compile_test.go | 11 +++++++++++ testdata/d2compiler/TestCompile/near_special.exp.json | 11 +++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 testdata/d2compiler/TestCompile/near_special.exp.json diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index dc7a43beac..94b2f7ad6f 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -4,7 +4,8 @@ #### Improvements 🧹 -- Boards no longer inherit `label` fields from parents. [#1838](https://github.com/terrastruct/d2/pull/1838) +- Boards no longer inherit `label` fields from parents [#1838](https://github.com/terrastruct/d2/pull/1838) +- Prevents `near` targeting a child of a special object like grid cells, which wasn't doing anything [#1851](https://github.com/terrastruct/d2/pull/1851) #### Bugfixes ⛑️ diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 5b314470fd..9610ea6c3f 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -1097,6 +1097,14 @@ func (c *compiler) validateNear(g *d2graph.Graph) { continue } } + if nearObj.ClosestGridDiagram() != nil { + c.errorf(obj.NearKey, "near keys cannot be set to descendants of special objects, like grid cells") + continue + } + if nearObj.OuterSequenceDiagram() != nil { + c.errorf(obj.NearKey, "near keys cannot be set to descendants of special objects, like sequence diagram actors") + continue + } } else if isConst { if obj.Parent != g.Root { c.errorf(obj.NearKey, "constant near keys can only be set on root level shapes") diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index e14aacc031..38d1fe0514 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -1607,6 +1607,17 @@ d2/testdata/d2compiler/TestCompile/near-invalid.d2:14:9: near keys cannot be set `, expErr: `d2/testdata/d2compiler/TestCompile/near_bad_constant.d2:1:9: near key "txop-center" must be the absolute path to a shape or one of the following constants: top-left, top-center, top-right, center-left, center-right, bottom-left, bottom-center, bottom-right`, }, + { + name: "near_special", + + text: `x.near: z.x +z: { + grid-rows: 1 + x +} +`, + expErr: `d2/testdata/d2compiler/TestCompile/near_special.d2:1:9: near keys cannot be set to descendants of special objects, like grid cells`, + }, { name: "near_bad_connected", diff --git a/testdata/d2compiler/TestCompile/near_special.exp.json b/testdata/d2compiler/TestCompile/near_special.exp.json new file mode 100644 index 0000000000..ac5aadb335 --- /dev/null +++ b/testdata/d2compiler/TestCompile/near_special.exp.json @@ -0,0 +1,11 @@ +{ + "graph": null, + "err": { + "errs": [ + { + "range": "d2/testdata/d2compiler/TestCompile/near_special.d2,0:8:8-0:11:11", + "errmsg": "d2/testdata/d2compiler/TestCompile/near_special.d2:1:9: near keys cannot be set to descendants of special objects, like grid cells" + } + ] + } +}