From 20190c1224efeaf71248b26e5c65c118670893e5 Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Fri, 22 Sep 2023 14:16:35 -0700 Subject: [PATCH 1/4] add test --- d2compiler/compile_test.go | 14 ++ .../vars/errors/spread-mid-string.exp.json | 206 ++++++++++++++++++ 2 files changed, 220 insertions(+) create mode 100644 testdata/d2compiler/TestCompile2/vars/errors/spread-mid-string.exp.json diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 608d92c97..bf90f58c2 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -4061,6 +4061,20 @@ z: { `, `d2/testdata/d2compiler/TestCompile2/vars/errors/spread-non-solo.d2:8:2: cannot substitute composite variable "x" as part of a string`) }, }, + { + name: "spread-mid-string", + run: func(t *testing.T) { + assertCompile(t, ` +vars: { + test: hello +} + +mybox: { + label: prefix${test}suffix +} +`, "") + }, + }, } for _, tc := range tca { diff --git a/testdata/d2compiler/TestCompile2/vars/errors/spread-mid-string.exp.json b/testdata/d2compiler/TestCompile2/vars/errors/spread-mid-string.exp.json new file mode 100644 index 000000000..92963e270 --- /dev/null +++ b/testdata/d2compiler/TestCompile2/vars/errors/spread-mid-string.exp.json @@ -0,0 +1,206 @@ +{ + "graph": { + "name": "", + "isFolderOnly": false, + "ast": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/spread-mid-string.d2,0:0:0-8:0:66", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/spread-mid-string.d2,1:0:1-3:1:24", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/spread-mid-string.d2,1:0:1-1:4:5", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/spread-mid-string.d2,1:0:1-1:4:5", + "value": [ + { + "string": "vars", + "raw_string": "vars" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/spread-mid-string.d2,1:6:7-3:1:24", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/spread-mid-string.d2,2:2:11-2:13:22", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/spread-mid-string.d2,2:2:11-2:6:15", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/spread-mid-string.d2,2:2:11-2:6:15", + "value": [ + { + "string": "test", + "raw_string": "test" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/spread-mid-string.d2,2:8:17-2:13:22", + "value": [ + { + "string": "hello", + "raw_string": "hello" + } + ] + } + } + } + } + ] + } + } + } + }, + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/spread-mid-string.d2,5:0:26-7:1:65", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/spread-mid-string.d2,5:0:26-5:5:31", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/spread-mid-string.d2,5:0:26-5:5:31", + "value": [ + { + "string": "mybox", + "raw_string": "mybox" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/spread-mid-string.d2,5:7:33-7:1:65", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/spread-mid-string.d2,6:2:37-6:28:63", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/spread-mid-string.d2,6:2:37-6:7:42", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/spread-mid-string.d2,6:2:37-6:7:42", + "value": [ + { + "string": "label", + "raw_string": "label" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/spread-mid-string.d2,6:9:44-6:28:63", + "value": [ + { + "string": "prefixhellosuffix" + } + ] + } + } + } + } + ] + } + } + } + } + ] + }, + "root": { + "id": "", + "id_val": "", + "attributes": { + "label": { + "value": "" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + }, + "edges": null, + "objects": [ + { + "id": "mybox", + "id_val": "mybox", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/spread-mid-string.d2,5:0:26-5:5:31", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/vars/errors/spread-mid-string.d2,5:0:26-5:5:31", + "value": [ + { + "string": "mybox", + "raw_string": "mybox" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "prefixhellosuffix" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + } + ] + }, + "err": null +} From 938d601fbc9fa0a13f6f84753f60d61117d9a448 Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Fri, 22 Sep 2023 14:52:16 -0700 Subject: [PATCH 2/4] add format test --- d2format/format_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/d2format/format_test.go b/d2format/format_test.go index 889b01537..c6dd79296 100644 --- a/d2format/format_test.go +++ b/d2format/format_test.go @@ -810,6 +810,25 @@ steps: { step-1-content } } +`, + }, + { + name: "substitution_mid_string", + in: `vars: { + test: hello +} + +mybox: { + label: prefix${test}suffix +} +`, + exp: `vars: { + test: hello +} + +mybox: { + label: prefix${test}suffix +} `, }, } From 15a3075690d60aa207d27edce1f6dac10cf7c54e Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Fri, 22 Sep 2023 19:58:16 -0700 Subject: [PATCH 3/4] fix --- d2parser/parse.go | 1 + 1 file changed, 1 insertion(+) diff --git a/d2parser/parse.go b/d2parser/parse.go index 84d032a58..3a615cc36 100644 --- a/d2parser/parse.go +++ b/d2parser/parse.go @@ -1185,6 +1185,7 @@ func (p *parser) parseUnquotedString(inKey bool) (s *d2ast.UnquotedString) { rawv := rawb.String() s.Value = append(s.Value, d2ast.InterpolationBox{String: &sv, StringRaw: &rawv}) sb.Reset() + rawb.Reset() } s.Value = append(s.Value, d2ast.InterpolationBox{Substitution: subst}) continue From d133a359a7f0d6c68aecacdcbe4ae80e1d13df3e Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Fri, 22 Sep 2023 20:09:02 -0700 Subject: [PATCH 4/4] changelog --- ci/release/changelogs/next.md | 1 + 1 file changed, 1 insertion(+) diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index 65594b6f9..3866a8fc3 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -16,3 +16,4 @@ - Fixes missing compile error for non-blockstring empty labels [#1590](https://github.com/terrastruct/d2/issues/1590) - Fixes multiple constant nears overlapping in some cases [#1591](https://github.com/terrastruct/d2/issues/1591) - Fixes error with an empty nested grid [#1594](https://github.com/terrastruct/d2/issues/1594) +- Fixes incorrect `d2fmt` with variable substitution mid-string [#1611](https://github.com/terrastruct/d2/issues/1611)