From 17846b41690d12e20a2525d831190ea6fd66fb06 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Sat, 26 Oct 2024 23:21:30 -0600 Subject: [PATCH] remove empty board keywords --- ci/release/changelogs/next.md | 1 + d2format/format.go | 13 ++++++++++--- d2format/format_test.go | 11 +++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index 41f1becc9c..7b05b6262d 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -7,6 +7,7 @@ - Lib: removes a dependency on external slog that was causing troubles with installation [#2137](https://github.com/terrastruct/d2/pull/2137) - CLI: attempts writing to path atomically, falling back to non-atomic if failed [#2141](https://github.com/terrastruct/d2/pull/2141) - Export: pptx has "created at" metadata removed, so successive runs yield the same result [#2169](https://github.com/terrastruct/d2/pull/2160) +- Formatter: empty board keywords (e.g. `layers`) are removed [#2178](https://github.com/terrastruct/d2/pull/2178) #### Bugfixes ⛑️ diff --git a/d2format/format.go b/d2format/format.go index e72800e680..1fe745c1db 100644 --- a/d2format/format.go +++ b/d2format/format.go @@ -293,11 +293,18 @@ func (p *printer) _map(m *d2ast.Map) { if nb.IsBoardNode() { switch nb.MapKey.Key.Path[0].Unbox().ScalarString() { case "layers": - layerNodes = append(layerNodes, nb) + // remove useless + if nb.MapKey.Value.Map != nil && len(nb.MapKey.Value.Map.Nodes) > 0 { + layerNodes = append(layerNodes, nb) + } case "scenarios": - scenarioNodes = append(scenarioNodes, nb) + if nb.MapKey.Value.Map != nil && len(nb.MapKey.Value.Map.Nodes) > 0 { + scenarioNodes = append(scenarioNodes, nb) + } case "steps": - stepNodes = append(stepNodes, nb) + if nb.MapKey.Value.Map != nil && len(nb.MapKey.Value.Map.Nodes) > 0 { + stepNodes = append(stepNodes, nb) + } } prev = n continue diff --git a/d2format/format_test.go b/d2format/format_test.go index e70783311f..ab90ea276d 100644 --- a/d2format/format_test.go +++ b/d2format/format_test.go @@ -881,6 +881,17 @@ coop: { fill: blue } } +`, + }, + { + name: "remove-empty-boards", + in: `k + +layers +scenarios: {} +steps: asdf +`, + exp: `k `, }, }