From 794e86498844866f2714f975f5642c35bcc95ca3 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Sun, 11 Dec 2022 16:46:09 -0800 Subject: [PATCH] fix constraint keyword --- d2compiler/compile.go | 3 + d2compiler/compile_test.go | 11 ++ .../TestCompile/constraint_label.exp.json | 174 ++++++++++++++++++ 3 files changed, 188 insertions(+) create mode 100644 testdata/d2compiler/TestCompile/constraint_label.exp.json diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 2d2cba269..c49a95a43 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -363,6 +363,9 @@ func (c *compiler) applyScalar(attrs *d2graph.Attributes, reserved string, box d } attrs.Direction.Value = scalar.ScalarString() return + case "constraint": + // Compilation for shape-specific keywords happens elsewhere + return } if _, ok := d2graph.StyleKeywords[reserved]; ok { diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index c6cc1c25c..f29db2f16 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -1618,6 +1618,17 @@ choo: { assert.String(t, "left", g.Objects[0].Attributes.Direction.Value) }, }, + { + name: "constraint_label", + + text: `foo { + label: bar + constraint: BIZ +}`, + assertions: func(t *testing.T, g *d2graph.Graph) { + assert.String(t, "bar", g.Objects[0].Attributes.Label.Value) + }, + }, { name: "invalid_direction", diff --git a/testdata/d2compiler/TestCompile/constraint_label.exp.json b/testdata/d2compiler/TestCompile/constraint_label.exp.json new file mode 100644 index 000000000..dcaadd251 --- /dev/null +++ b/testdata/d2compiler/TestCompile/constraint_label.exp.json @@ -0,0 +1,174 @@ +{ + "graph": { + "ast": { + "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,0:0:0-3:1:38", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,0:0:0-3:1:38", + "key": { + "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,0:0:0-0:4:4", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,0:0:0-0:3:3", + "value": [ + { + "string": "foo", + "raw_string": "foo" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,0:4:4-3:0:37", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,1:2:8-1:12:18", + "key": { + "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,1:2:8-1:7:13", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,1:2:8-1:7:13", + "value": [ + { + "string": "label", + "raw_string": "label" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,1:9:15-1:12:18", + "value": [ + { + "string": "bar", + "raw_string": "bar" + } + ] + } + } + } + }, + { + "map_key": { + "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,2:2:21-2:17:36", + "key": { + "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,2:2:21-2:12:31", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,2:2:21-2:12:31", + "value": [ + { + "string": "constraint", + "raw_string": "constraint" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,2:14:33-2:17:36", + "value": [ + { + "string": "BIZ", + "raw_string": "BIZ" + } + ] + } + } + } + } + ] + } + } + } + } + ] + }, + "root": { + "id": "", + "id_val": "", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "attributes": { + "label": { + "value": "" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + } + }, + "zIndex": 0 + }, + "edges": null, + "objects": [ + { + "id": "foo", + "id_val": "foo", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,0:0:0-0:4:4", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/constraint_label.d2,0:0:0-0:3:3", + "value": [ + { + "string": "foo", + "raw_string": "foo" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": 0 + } + ], + "attributes": { + "label": { + "value": "bar" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + } + }, + "zIndex": 0 + } + ] + }, + "err": null +}