From 3563b15933e1799cfda4605a89238aee590255c6 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Wed, 14 Feb 2024 17:59:34 -0800 Subject: [PATCH] no inheriting label --- ci/release/changelogs/next.md | 2 + d2compiler/compile_test.go | 16 ++ d2ir/compile.go | 3 + .../boards/no-inherit-label.exp.json | 255 ++++++++++++++++++ 4 files changed, 276 insertions(+) create mode 100644 testdata/d2compiler/TestCompile2/boards/no-inherit-label.exp.json diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index d0bb47702..dc7a43bea 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -4,6 +4,8 @@ #### Improvements 🧹 +- Boards no longer inherit `label` fields from parents. [#1838](https://github.com/terrastruct/d2/pull/1838) + #### Bugfixes ⛑️ - Fixes `null` being set on a nested shape not working in certain cases when connections also pointed to that shape [#1830](https://github.com/terrastruct/d2/pull/1830) diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 8e2304049..e14aacc03 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -2991,6 +2991,22 @@ steps: { assert.True(t, g.IsFolderOnly) }, }, + { + name: "no-inherit-label", + run: func(t *testing.T) { + g, _ := assertCompile(t, ` +label: hi + +steps: { + 1: { + RJ + } +} +`, "") + assert.True(t, g.Root.Label.MapKey != nil) + assert.True(t, g.Steps[0].Root.Label.MapKey == nil) + }, + }, { name: "scenarios_edge_index", run: func(t *testing.T) { diff --git a/d2ir/compile.go b/d2ir/compile.go index 37d3445a9..c3e05f4fc 100644 --- a/d2ir/compile.go +++ b/d2ir/compile.go @@ -361,6 +361,9 @@ func (c *compiler) overlay(base *Map, f *Field) { return } base = base.CopyBase(f) + // Certain fields should never carry forward. + // If you give your scenario a label, you don't want all steps in a scenario to be labeled the same. + base.DeleteField("label") OverlayMap(base, f.Map()) f.Composite = base } diff --git a/testdata/d2compiler/TestCompile2/boards/no-inherit-label.exp.json b/testdata/d2compiler/TestCompile2/boards/no-inherit-label.exp.json new file mode 100644 index 000000000..bfffea2c7 --- /dev/null +++ b/testdata/d2compiler/TestCompile2/boards/no-inherit-label.exp.json @@ -0,0 +1,255 @@ +{ + "graph": { + "name": "", + "isFolderOnly": true, + "ast": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/no-inherit-label.d2,0:0:0-8:0:41", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/no-inherit-label.d2,1:0:1-1:9:10", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/no-inherit-label.d2,1:0:1-1:5:6", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/no-inherit-label.d2,1:0:1-1:5:6", + "value": [ + { + "string": "label", + "raw_string": "label" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/no-inherit-label.d2,1:7:8-1:9:10", + "value": [ + { + "string": "hi", + "raw_string": "hi" + } + ] + } + } + } + }, + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/no-inherit-label.d2,3:0:12-7:1:40", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/no-inherit-label.d2,3:0:12-3:5:17", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/no-inherit-label.d2,3:0:12-3:5:17", + "value": [ + { + "string": "steps", + "raw_string": "steps" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/no-inherit-label.d2,3:7:19-7:1:40", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/no-inherit-label.d2,4:2:23-6:3:38", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/no-inherit-label.d2,4:2:23-4:3:24", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/no-inherit-label.d2,4:2:23-4:3:24", + "value": [ + { + "string": "1", + "raw_string": "1" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/no-inherit-label.d2,4:5:26-6:3:38", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/no-inherit-label.d2,5:4:32-5:6:34", + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/no-inherit-label.d2,5:4:32-5:6:34", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/no-inherit-label.d2,5:4:32-5:6:34", + "value": [ + { + "string": "RJ", + "raw_string": "RJ" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + ] + } + } + } + } + ] + } + } + } + } + ] + }, + "root": { + "id": "", + "id_val": "", + "attributes": { + "label": { + "value": "hi" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + }, + "edges": null, + "objects": null, + "steps": [ + { + "name": "1", + "isFolderOnly": false, + "ast": { + "range": ",1:0:0-2:0:0", + "nodes": [ + { + "map_key": { + "range": ",0:0:0-0:0:0", + "key": { + "range": ",0:0:0-0:0:0", + "path": [ + { + "unquoted_string": { + "range": ",0:0:0-0:0:0", + "value": [ + { + "string": "RJ" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + ] + }, + "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": "RJ", + "id_val": "RJ", + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/no-inherit-label.d2,5:4:32-5:6:34", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile2/boards/no-inherit-label.d2,5:4:32-5:6:34", + "value": [ + { + "string": "RJ", + "raw_string": "RJ" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "RJ" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + } + ] + } + ] + }, + "err": null +}