From f2b9bba0df7d2ab52e7e29175203f26643e0204b Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Mon, 24 Mar 2025 18:38:55 -0600 Subject: [PATCH 1/2] fix vars fmt --- d2format/format.go | 7 ++++++- d2format/format_test.go | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/d2format/format.go b/d2format/format.go index a2acb4b6b..63bebcadd 100644 --- a/d2format/format.go +++ b/d2format/format.go @@ -127,7 +127,7 @@ func (p *printer) blockComment(bc *d2ast.BlockComment) { } func (p *printer) interpolationBoxes(boxes []d2ast.InterpolationBox, isDoubleString bool) { - for _, b := range boxes { + for i, b := range boxes { if b.Substitution != nil { p.substitution(b.Substitution) continue @@ -140,6 +140,11 @@ func (p *printer) interpolationBoxes(boxes []d2ast.InterpolationBox, isDoubleStr s = escapeUnquotedValue(*b.String, p.inKey) } b.StringRaw = &s + } else if i > 0 && boxes[i-1].Substitution != nil { + // If this string follows a substitution, we need to make sure to use + // the actual string content, not the raw value which might be incorrect + s := *b.String + b.StringRaw = &s } if !isDoubleString { if _, ok := d2ast.ReservedKeywords[strings.ToLower(*b.StringRaw)]; ok { diff --git a/d2format/format_test.go b/d2format/format_test.go index ab90ea276..ed7c9e97a 100644 --- a/d2format/format_test.go +++ b/d2format/format_test.go @@ -892,6 +892,19 @@ scenarios: {} steps: asdf `, exp: `k +`, + }, + { + name: "vars", + in: `vars: { + a: "a" + b: "X${a})" +} +`, + exp: `vars: { + a: "a" + b: "X${a})" +} `, }, } From c872acf04429d6e85cb7dcc03cf9c0697cc17e72 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Mon, 24 Mar 2025 18:40:20 -0600 Subject: [PATCH 2/2] next --- ci/release/changelogs/next.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index 282993cdc..442b2fe74 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -28,6 +28,8 @@ - fixes inconsistencies when objects were double quoted [#2390](https://github.com/terrastruct/d2/pull/2390) - fixes globs not applying to spread substitutions [#2426](https://github.com/terrastruct/d2/issues/2426) - fixes panic when classes were mixed with layers incorrectly [#2448](https://github.com/terrastruct/d2/pull/2448) +- Formatter: + - fixes substitutions in quotes surrounded by text [#2462](https://github.com/terrastruct/d2/pull/2462) - CLI: fetch and render remote images of mimetype octet-stream correctly [#2370](https://github.com/terrastruct/d2/pull/2370) - Composition: spread importing scenarios/steps was not inheriting correctly [#2460](https://github.com/terrastruct/d2/pull/2460) - Markdown: fixes nested var substitutions not working [#2456](https://github.com/terrastruct/d2/pull/2456)