From 7d36a57b04e5904fec022c5fd939b45f5e8c344a Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Sun, 26 Nov 2023 18:25:31 -0800 Subject: [PATCH] globs: fix ampersand outside glob --- ci/release/changelogs/next.md | 1 + d2ir/compile.go | 3 +++ d2ir/filter_test.go | 12 ++++++++++++ .../d2ir/TestCompile/filters/outside-glob.exp.json | 4 ++++ 4 files changed, 20 insertions(+) create mode 100644 testdata/d2ir/TestCompile/filters/outside-glob.exp.json diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index f0ba7471dc..4a012fd0b7 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -31,3 +31,4 @@ - Fixes a panic with a connection to a grid cell that is a container in TALA [#1729](https://github.com/terrastruct/d2/pull/1729) - Fixes incorrect grid cell positioning when the grid has a shape set and fixes content sometimes escaping circle shapes. [#1734](https://github.com/terrastruct/d2/pull/1734) - Fixes content sometimes escaping cloud shapes. [#1736](https://github.com/terrastruct/d2/pull/1736) +- Fixes panic using a glob filter (e.g. `&a`) outside globs. [#1748](https://github.com/terrastruct/d2/pull/1748) diff --git a/d2ir/compile.go b/d2ir/compile.go index ed18a6253d..7fce0c828e 100644 --- a/d2ir/compile.go +++ b/d2ir/compile.go @@ -394,6 +394,9 @@ func (c *compiler) ampersandFilterMap(dst *Map, ast, scopeAST *d2ast.Map) bool { ScopeAST: scopeAST, }) if !ok { + if len(c.mapRefContextStack) == 0 { + return false + } // Unapply glob if appropriate. gctx := c.getGlobContext(c.mapRefContextStack[len(c.mapRefContextStack)-1]) if gctx == nil { diff --git a/d2ir/filter_test.go b/d2ir/filter_test.go index 322d5b4502..63375b8167 100644 --- a/d2ir/filter_test.go +++ b/d2ir/filter_test.go @@ -233,6 +233,18 @@ classes: { TestCompile/filters/errors/bad-syntax.d2:9:1: unexpected map termination character } in file map`) }, }, + { + name: "outside-glob", + run: func(t testing.TB) { + _, err := compile(t, `jacob.style: { + fill: red + multiple: true +} +&a +`) + assert.ErrorString(t, err, `TestCompile/filters/errors/outside-glob.d2:5:1: glob filters cannot be used outside globs`) + }, + }, { name: "no-glob", run: func(t testing.TB) { diff --git a/testdata/d2ir/TestCompile/filters/outside-glob.exp.json b/testdata/d2ir/TestCompile/filters/outside-glob.exp.json new file mode 100644 index 0000000000..fbfc218497 --- /dev/null +++ b/testdata/d2ir/TestCompile/filters/outside-glob.exp.json @@ -0,0 +1,4 @@ +{ + "fields": null, + "edges": null +}