Merge pull request #1851 from alixander/prevent-grid-near

prevent `near` to special objects
This commit is contained in:
Alexander Wang 2024-02-27 13:24:32 -08:00 committed by GitHub
commit 82070f7721
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 32 additions and 1 deletions

View file

@ -4,7 +4,8 @@
#### Improvements 🧹 #### 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 ⛑️ #### Bugfixes ⛑️

View file

@ -1097,6 +1097,14 @@ func (c *compiler) validateNear(g *d2graph.Graph) {
continue 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 { } else if isConst {
if obj.Parent != g.Root { if obj.Parent != g.Root {
c.errorf(obj.NearKey, "constant near keys can only be set on root level shapes") c.errorf(obj.NearKey, "constant near keys can only be set on root level shapes")

View file

@ -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`, 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", name: "near_bad_connected",

11
testdata/d2compiler/TestCompile/near_special.exp.json generated vendored Normal file
View file

@ -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"
}
]
}
}