From 2dfa0bef86c7e0ac2020c83c87ae3e5296924e1d Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Mon, 13 Feb 2023 14:47:35 -0800 Subject: [PATCH 1/2] add failing test --- d2oracle/edit_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/d2oracle/edit_test.go b/d2oracle/edit_test.go index dd3b430fc..02669c453 100644 --- a/d2oracle/edit_test.go +++ b/d2oracle/edit_test.go @@ -1755,6 +1755,20 @@ b assert.JSON(t, 0, len(g.Objects[0].Children)) }, }, + { + name: "out_of_newline_container", + + text: `"a\n": { + b +} +`, + key: `"a\n".b`, + newKey: `b`, + + exp: `"a\n" +b +`, + }, { name: "partial_slice", From 11d1c111d51d993d60966c56e4dc9ed0ce85eece Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Mon, 13 Feb 2023 17:30:10 -0800 Subject: [PATCH 2/2] d2oracle: Fix out_of_newline_container --- d2compiler/compile.go | 4 +- d2graph/d2graph.go | 32 ++++ .../out_of_newline_container.exp.json | 179 ++++++++++++++++++ 3 files changed, 213 insertions(+), 2 deletions(-) create mode 100644 testdata/d2oracle/TestMove/out_of_newline_container.exp.json diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 584dd20af..8442e5417 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -178,7 +178,7 @@ func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) { } } scopeObjIDA := d2ir.IDA(fr.Context.ScopeMap) - scopeObj, _ := obj.Graph.Root.HasChild(scopeObjIDA) + scopeObj, _ := obj.Graph.Root.HasChildIDVal(scopeObjIDA) obj.References = append(obj.References, d2graph.Reference{ Key: fr.KeyPath, KeyPathIndex: fr.KeyPathIndex(), @@ -382,7 +382,7 @@ func (c *compiler) compileEdge(obj *d2graph.Object, e *d2ir.Edge) { edge.Attributes.Label.MapKey = e.LastPrimaryKey() for _, er := range e.References { scopeObjIDA := d2ir.IDA(er.Context.ScopeMap) - scopeObj, _ := edge.Src.Graph.Root.HasChild(d2graphIDA(scopeObjIDA)) + scopeObj, _ := edge.Src.Graph.Root.HasChildIDVal(d2graphIDA(scopeObjIDA)) edge.References = append(edge.References, d2graph.EdgeReference{ Edge: er.Context.Edge, MapKey: er.Context.Key, diff --git a/d2graph/d2graph.go b/d2graph/d2graph.go index 317e30eee..1300289d6 100644 --- a/d2graph/d2graph.go +++ b/d2graph/d2graph.go @@ -558,6 +558,38 @@ func (obj *Object) HasChild(ids []string) (*Object, bool) { return child, true } +// Keep in sync with HasChild. +func (obj *Object) HasChildIDVal(ids []string) (*Object, bool) { + if len(ids) == 0 { + return obj, true + } + if len(ids) == 1 && ids[0] != "style" { + _, ok := ReservedKeywords[ids[0]] + if ok { + return obj, true + } + } + + id := ids[0] + ids = ids[1:] + + var child *Object + for _, ch2 := range obj.ChildrenArray { + if ch2.IDVal == id { + child = ch2 + break + } + } + if child == nil { + return nil, false + } + + if len(ids) >= 1 { + return child.HasChildIDVal(ids) + } + return child, true +} + func (obj *Object) HasEdge(mk *d2ast.Key) (*Edge, bool) { ea, ok := obj.FindEdges(mk) if !ok { diff --git a/testdata/d2oracle/TestMove/out_of_newline_container.exp.json b/testdata/d2oracle/TestMove/out_of_newline_container.exp.json new file mode 100644 index 000000000..53bcddb77 --- /dev/null +++ b/testdata/d2oracle/TestMove/out_of_newline_container.exp.json @@ -0,0 +1,179 @@ +{ + "graph": { + "name": "", + "ast": { + "range": "d2/testdata/d2oracle/TestMove/out_of_newline_container.d2,0:0:0-2:0:8", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/out_of_newline_container.d2,0:0:0-0:5:5", + "key": { + "range": "d2/testdata/d2oracle/TestMove/out_of_newline_container.d2,0:0:0-0:5:5", + "path": [ + { + "double_quoted_string": { + "range": "d2/testdata/d2oracle/TestMove/out_of_newline_container.d2,0:0:0-0:5:5", + "value": [ + { + "string": "a\n", + "raw_string": "a\\n" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/out_of_newline_container.d2,1:0:6-1:1:7", + "key": { + "range": "d2/testdata/d2oracle/TestMove/out_of_newline_container.d2,1:0:6-1:1:7", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/out_of_newline_container.d2,1:0:6-1:1:7", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + ] + }, + "root": { + "id": "", + "id_val": "", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "attributes": { + "label": { + "value": "" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + "edges": null, + "objects": [ + { + "id": "\"a\\n\"", + "id_val": "a\n", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/out_of_newline_container.d2,0:0:0-0:5:5", + "path": [ + { + "double_quoted_string": { + "range": "d2/testdata/d2oracle/TestMove/out_of_newline_container.d2,0:0:0-0:5:5", + "value": [ + { + "string": "a\n", + "raw_string": "a\\n" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "a\n" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "b", + "id_val": "b", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/out_of_newline_container.d2,1:0:6-1:1:7", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/out_of_newline_container.d2,1:0:6-1:1:7", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "b" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + } + ] + }, + "err": "" +}