Skip to content

Commit

Permalink
Merge pull request #2195 from alixander/style-board-bug
Browse files Browse the repository at this point in the history
fix double globs in multiple scenarios
  • Loading branch information
alixander authored Nov 5, 2024
2 parents a814db6 + db83fb8 commit 51a2b7b
Show file tree
Hide file tree
Showing 8 changed files with 1,568 additions and 537 deletions.
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@

- Render: fixes edge case of a 3d shape with outside label being cut off [#2132](https://github.com/terrastruct/d2/pull/2132)
- Composition: labels for boards set with shorthand `x: y` was not applied [#2182](https://github.com/terrastruct/d2/pull/2182)
- Globs: double globs (`**`) were erroring when used with multiple scenario boards [#2195](https://github.com/terrastruct/d2/pull/2195)
15 changes: 15 additions & 0 deletions d2ast/d2ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,21 @@ func (mk *Key) HasTripleGlob() bool {
return false
}

func (mk *Key) HasMultiGlob() bool {
if mk.Key.HasMultiGlob() {
return true
}
for _, e := range mk.Edges {
if e.Src.HasMultiGlob() || e.Dst.HasMultiGlob() {
return true
}
}
if mk.EdgeKey.HasMultiGlob() {
return true
}
return false
}

func (mk *Key) SupportsGlobFilters() bool {
if mk.Key.HasGlob() && len(mk.Edges) == 0 {
return true
Expand Down
34 changes: 34 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3576,6 +3576,40 @@ steps: {
`, `d2/testdata/d2compiler/TestCompile2/boards/errs/duplicate_board.d2:9:2: board name one already used by another board`)
},
},
{
name: "style-nested-boards",
run: func(t *testing.T) {
g, _ := assertCompile(t, `**.style.stroke: black
scenarios: {
a: {
x
}
b: {
x
}
}
steps: {
c: {
x
}
d: {
x
}
}
layers: {
e: {
x
}
}
`, ``)
assert.Equal(t, "black", g.Scenarios[0].Objects[0].Style.Stroke.Value)
assert.Equal(t, "black", g.Scenarios[1].Objects[0].Style.Stroke.Value)
assert.Equal(t, "black", g.Steps[0].Objects[0].Style.Stroke.Value)
assert.Equal(t, "black", g.Steps[1].Objects[0].Style.Stroke.Value)
assert.Equal(t, (*d2graph.Scalar)(nil), g.Layers[0].Objects[0].Style.Stroke)
},
},
}

for _, tc := range tca {
Expand Down
4 changes: 2 additions & 2 deletions d2ir/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ func (c *compiler) ampersandFilterMap(dst *Map, ast, scopeAST *d2ast.Map) bool {
return false
}
var ks string
if gctx.refctx.Key.HasTripleGlob() {
if gctx.refctx.Key.HasMultiGlob() {
ks = d2format.Format(d2ast.MakeKeyPath(IDA(dst)))
} else {
ks = d2format.Format(d2ast.MakeKeyPath(BoardIDA(dst)))
Expand Down Expand Up @@ -488,7 +488,7 @@ func (c *compiler) compileMap(dst *Map, ast, scopeAST *d2ast.Map) {
// We don't want globs applied in a given scenario to affect future boards
// Copying the applied fields and edges keeps the applications scoped to this board
// Note that this is different from steps, where applications carry over
if !g.refctx.Key.HasTripleGlob() {
if !g.refctx.Key.HasMultiGlob() {
// Triple globs already apply independently to each board
g2.copyApplied(g)
}
Expand Down
8 changes: 4 additions & 4 deletions d2ir/d2ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ func (m *Map) ensureField(i int, kp *d2ast.KeyPath, refctx *RefContext, create b
filter := func(f *Field, passthrough bool) bool {
if gctx != nil {
var ks string
if refctx.Key.HasTripleGlob() {
if refctx.Key.HasMultiGlob() {
ks = d2format.Format(d2ast.MakeKeyPath(IDA(f)))
} else {
ks = d2format.Format(d2ast.MakeKeyPath(BoardIDA(f)))
Expand Down Expand Up @@ -937,7 +937,7 @@ func (m *Map) ensureField(i int, kp *d2ast.KeyPath, refctx *RefContext, create b
}
for _, grefctx := range c.globRefContextStack {
var ks string
if grefctx.Key.HasTripleGlob() {
if grefctx.Key.HasMultiGlob() {
ks = d2format.Format(d2ast.MakeKeyPath(IDA(f)))
} else {
ks = d2format.Format(d2ast.MakeKeyPath(BoardIDA(f)))
Expand Down Expand Up @@ -1134,7 +1134,7 @@ func (m *Map) getEdges(eid *EdgeID, refctx *RefContext, gctx *globContext, ea *[
for _, e := range ea2 {
if gctx != nil {
var ks string
if refctx.Key.HasTripleGlob() {
if refctx.Key.HasMultiGlob() {
ks = d2format.Format(d2ast.MakeKeyPath(IDA(e)))
} else {
ks = d2format.Format(d2ast.MakeKeyPath(BoardIDA(e)))
Expand Down Expand Up @@ -1352,7 +1352,7 @@ func (m *Map) createEdge2(eid *EdgeID, refctx *RefContext, gctx *globContext, c
e2 := e.Copy(e.Parent()).(*Edge)
e2.ID = e2.ID.Copy()
e2.ID.Index = nil
if refctx.Key.HasTripleGlob() {
if refctx.Key.HasMultiGlob() {
ks = d2format.Format(d2ast.MakeKeyPath(IDA(e2)))
} else {
ks = d2format.Format(d2ast.MakeKeyPath(BoardIDA(e2)))
Expand Down
Loading

0 comments on commit 51a2b7b

Please sign in to comment.