diff --git a/d2oracle/edit.go b/d2oracle/edit.go index 9fe1c5389..3537677b3 100644 --- a/d2oracle/edit.go +++ b/d2oracle/edit.go @@ -773,13 +773,14 @@ func deleteObject(g *d2graph.Graph, key *d2ast.KeyPath, obj *d2graph.Object) (*d if len(ref.MapKey.Edges) == 0 { isSuffix := ref.KeyPathIndex == len(ref.Key.Path)-1 ref.Key.Path = append(ref.Key.Path[:ref.KeyPathIndex], ref.Key.Path[ref.KeyPathIndex+1:]...) - withoutReserved := go2.Filter(ref.Key.Path, func(x *d2ast.StringBox) bool { - _, ok := d2graph.ReservedKeywords[x.Unbox().ScalarString()] - return !ok + withoutSpecial := go2.Filter(ref.Key.Path, func(x *d2ast.StringBox) bool { + _, isReserved := d2graph.ReservedKeywords[x.Unbox().ScalarString()] + isSpecial := isReserved || x.Unbox().ScalarString() == "_" + return !isSpecial }) if obj.Attributes.Shape.Value == d2target.ShapeSQLTable || obj.Attributes.Shape.Value == d2target.ShapeClass { ref.MapKey.Value.Map = nil - } else if len(withoutReserved) == 0 { + } else if len(withoutSpecial) == 0 { hoistRefChildren(g, key, ref) deleteFromMap(ref.Scope, ref.MapKey) } else if ref.MapKey.Value.Unbox() == nil && @@ -1167,7 +1168,7 @@ func move(g *d2graph.Graph, key, newKey string) (*d2graph.Graph, error) { } ida := d2graph.Key(ref.Key) - resolvedObj, resolvedIDA, err := d2graph.ResolveUnderscoreKey(ida, obj) + resolvedObj, resolvedIDA, err := d2graph.ResolveUnderscoreKey(ida, ref.ScopeObj) if err != nil { return nil, err } @@ -2086,11 +2087,11 @@ func getMostNestedRefs(obj *d2graph.Object) []d2graph.Reference { if err != nil { mostKey = &d2ast.KeyPath{} } - _, resolvedScopeKey, err := d2graph.ResolveUnderscoreKey(d2graph.Key(scopeKey), obj) + _, resolvedScopeKey, err := d2graph.ResolveUnderscoreKey(d2graph.Key(scopeKey), ref.ScopeObj) if err != nil { continue } - _, resolvedMostKey, err := d2graph.ResolveUnderscoreKey(d2graph.Key(mostKey), obj) + _, resolvedMostKey, err := d2graph.ResolveUnderscoreKey(d2graph.Key(mostKey), ref.ScopeObj) if err != nil { continue } diff --git a/d2oracle/edit_test.go b/d2oracle/edit_test.go index 4c6a16cae..489f3384b 100644 --- a/d2oracle/edit_test.go +++ b/d2oracle/edit_test.go @@ -2025,6 +2025,24 @@ c: { d b } +`, + }, + + { + name: "nested-underscore-move-out", + text: `guitar: { + books: { + _._.pipe + } +} +`, + key: `pipe`, + newKey: `guitar.pipe`, + + exp: `guitar: { + books + pipe +} `, }, { @@ -3189,6 +3207,41 @@ c -> d exp: `books: { _.pipe } +`, + }, + { + name: "only-underscore", + + text: `guitar: { + books: { + _._.pipe + } +} +`, + key: `pipe`, + + exp: `guitar: { + books +} +`, + }, + { + name: "only-underscore-nested", + + text: `guitar: { + books: { + _._.pipe: { + a + } + } +} +`, + key: `pipe`, + + exp: `guitar: { + books +} +a `, }, { @@ -4743,6 +4796,23 @@ x.y.z.w.e.p.l -> x.y.z.1.2.3.4 "x.x": "x" }`, }, + + { + name: "only-reserved", + text: `guitar: { + books: { + _._.pipe: { + a + } + } +} +`, + key: `pipe`, + + exp: `{ + "pipe.a": "a" +}`, + }, { name: "delete_container_with_conflicts", diff --git a/testdata/d2oracle/TestDelete/only-underscore-nested.exp.json b/testdata/d2oracle/TestDelete/only-underscore-nested.exp.json new file mode 100644 index 000000000..483e50180 --- /dev/null +++ b/testdata/d2oracle/TestDelete/only-underscore-nested.exp.json @@ -0,0 +1,255 @@ +{ + "graph": { + "name": "", + "ast": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,0:0:0-4:0:22", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,0:0:0-2:1:19", + "key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,0:0:0-0:6:6", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,0:0:0-0:6:6", + "value": [ + { + "string": "guitar", + "raw_string": "guitar" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,0:8:8-2:0:18", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,1:2:12-1:7:17", + "key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,1:2:12-1:7:17", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,1:2:12-1:7:17", + "value": [ + { + "string": "books", + "raw_string": "books" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + ] + } + } + } + }, + { + "map_key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,3:0:20-3:1:21", + "key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,3:0:20-3:1:21", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,3:0:20-3:1:21", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "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": "guitar", + "id_val": "guitar", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,0:0:0-0:6:6", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,0:0:0-0:6:6", + "value": [ + { + "string": "guitar", + "raw_string": "guitar" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "guitar" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "books", + "id_val": "books", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,1:2:12-1:7:17", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,1:2:12-1:7:17", + "value": [ + { + "string": "books", + "raw_string": "books" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "books" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "a", + "id_val": "a", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,3:0:20-3:1:21", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore-nested.d2,3:0:20-3:1:21", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "a" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + } + ] + }, + "err": "" +} diff --git a/testdata/d2oracle/TestDelete/only-underscore.exp.json b/testdata/d2oracle/TestDelete/only-underscore.exp.json new file mode 100644 index 000000000..ce90b0614 --- /dev/null +++ b/testdata/d2oracle/TestDelete/only-underscore.exp.json @@ -0,0 +1,185 @@ +{ + "graph": { + "name": "", + "ast": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore.d2,0:0:0-3:0:20", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore.d2,0:0:0-2:1:19", + "key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore.d2,0:0:0-0:6:6", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore.d2,0:0:0-0:6:6", + "value": [ + { + "string": "guitar", + "raw_string": "guitar" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore.d2,0:8:8-2:0:18", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore.d2,1:2:12-1:7:17", + "key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore.d2,1:2:12-1:7:17", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore.d2,1:2:12-1:7:17", + "value": [ + { + "string": "books", + "raw_string": "books" + } + ] + } + } + ] + }, + "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": "guitar", + "id_val": "guitar", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore.d2,0:0:0-0:6:6", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore.d2,0:0:0-0:6:6", + "value": [ + { + "string": "guitar", + "raw_string": "guitar" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "guitar" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "books", + "id_val": "books", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore.d2,1:2:12-1:7:17", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestDelete/only-underscore.d2,1:2:12-1:7:17", + "value": [ + { + "string": "books", + "raw_string": "books" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "books" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + } + ] + }, + "err": "" +} diff --git a/testdata/d2oracle/TestMove/nested-underscore-move-out.exp.json b/testdata/d2oracle/TestMove/nested-underscore-move-out.exp.json new file mode 100644 index 000000000..79df7f9c5 --- /dev/null +++ b/testdata/d2oracle/TestMove/nested-underscore-move-out.exp.json @@ -0,0 +1,255 @@ +{ + "graph": { + "name": "", + "ast": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,0:0:0-4:0:27", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,0:0:0-3:1:26", + "key": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,0:0:0-0:6:6", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,0:0:0-0:6:6", + "value": [ + { + "string": "guitar", + "raw_string": "guitar" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,0:8:8-3:0:25", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,1:2:12-1:7:17", + "key": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,1:2:12-1:7:17", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,1:2:12-1:7:17", + "value": [ + { + "string": "books", + "raw_string": "books" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,2:2:20-2:6:24", + "key": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,2:2:20-2:6:24", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,2:2:20-2:6:24", + "value": [ + { + "string": "pipe", + "raw_string": "pipe" + } + ] + } + } + ] + }, + "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": "guitar", + "id_val": "guitar", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,0:0:0-0:6:6", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,0:0:0-0:6:6", + "value": [ + { + "string": "guitar", + "raw_string": "guitar" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "guitar" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "books", + "id_val": "books", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,1:2:12-1:7:17", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,1:2:12-1:7:17", + "value": [ + { + "string": "books", + "raw_string": "books" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "books" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "pipe", + "id_val": "pipe", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,2:2:20-2:6:24", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/nested-underscore-move-out.d2,2:2:20-2:6:24", + "value": [ + { + "string": "pipe", + "raw_string": "pipe" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "pipe" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + } + ] + }, + "err": "" +}