diff --git a/d2oracle/edit.go b/d2oracle/edit.go index 1132ec2ac..f1d56cb8f 100644 --- a/d2oracle/edit.go +++ b/d2oracle/edit.go @@ -643,6 +643,15 @@ func renameConflictsToParent(g *d2graph.Graph, key *d2ast.KeyPath) (*d2graph.Gra return g, nil } + // Usually ignore the object when generating, but if a sibling has the same ID, can't ignore + ignored := obj + for _, ch := range obj.ChildrenArray { + if ch.ID == obj.ID { + ignored = nil + break + } + } + // Keep a list of newly generated IDs, so that generateUniqueKey considers them for conflict var newIDs []string // If we already renamed the key from another reference, no need to touch @@ -710,7 +719,7 @@ func renameConflictsToParent(g *d2graph.Graph, key *d2ast.KeyPath) (*d2graph.Gra hoistedAbsKey.Path = append(hoistedAbsKey.Path, ref.Key.Path[:ref.KeyPathIndex]...) hoistedAbsKey.Path = append(hoistedAbsKey.Path, absKey.Path[len(absKey.Path)-1]) - uniqueKeyStr, _, err := generateUniqueKey(g, strings.Join(d2graph.Key(hoistedAbsKey), "."), obj, newIDs) + uniqueKeyStr, _, err := generateUniqueKey(g, strings.Join(d2graph.Key(hoistedAbsKey), "."), ignored, newIDs) if err != nil { return nil, err } @@ -1862,6 +1871,14 @@ func MoveIDDeltas(g *d2graph.Graph, key, newKey string) (deltas map[string]strin return nil, nil } + ignored := obj + for _, ch := range obj.ChildrenArray { + if ch.ID == obj.ID { + ignored = nil + break + } + } + for _, ch := range obj.ChildrenArray { chMK, err := d2parser.ParseMapKey(ch.AbsID()) if err != nil { @@ -1890,7 +1907,7 @@ func MoveIDDeltas(g *d2graph.Graph, key, newKey string) (deltas map[string]strin } if _, ok := g.Root.HasChild(d2graph.Key(hoistedMK.Key)); ok || conflictsWithNewID { - newKey, _, err := generateUniqueKey(g, hoistedAbsID, obj, newIDs) + newKey, _, err := generateUniqueKey(g, hoistedAbsID, ignored, newIDs) if err != nil { return nil, err } diff --git a/d2oracle/edit_test.go b/d2oracle/edit_test.go index a04413679..697526413 100644 --- a/d2oracle/edit_test.go +++ b/d2oracle/edit_test.go @@ -1903,6 +1903,33 @@ a b: { shape: cylinder } +`, + }, + { + name: "duplicate_generated", + + text: `x +x 2 +x 3: { + x 3 + x 4 +} +x 4 +y +`, + key: `x 3`, + newKey: `y.x 3`, + + exp: `x +x 2 + +x 3 +x 5 + +x 4 +y: { + x 3 +} `, }, { @@ -5350,6 +5377,27 @@ Square 3 exp: `{ "Square 2": "Square 3.Square 2", "Square 2.Square": "Square 2" +}`, + }, + { + name: "duplicate_generated", + + text: `x +x 2 +x 3: { + x 3 + x 4 +} +x 4 +y +`, + key: `x 3`, + newKey: `y.x 3`, + + exp: `{ + "x 3": "y.x 3", + "x 3.x 3": "x 3", + "x 3.x 4": "x 5" }`, }, } diff --git a/testdata/d2oracle/TestMove/duplicate_generated.exp.json b/testdata/d2oracle/TestMove/duplicate_generated.exp.json new file mode 100644 index 000000000..a900813d3 --- /dev/null +++ b/testdata/d2oracle/TestMove/duplicate_generated.exp.json @@ -0,0 +1,536 @@ +{ + "graph": { + "name": "", + "isFolderOnly": false, + "ast": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,0:0:0-10:0:33", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,0:0:0-0:1:1", + "key": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,1:0:2-1:3:5", + "key": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,1:0:2-1:3:5", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,1:0:2-1:3:5", + "value": [ + { + "string": "x 2", + "raw_string": "x 2" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,3:0:7-3:3:10", + "key": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,3:0:7-3:3:10", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,3:0:7-3:3:10", + "value": [ + { + "string": "x 3", + "raw_string": "x 3" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,4:0:11-4:3:14", + "key": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,4:0:11-4:3:14", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,4:0:11-4:3:14", + "value": [ + { + "string": "x 5", + "raw_string": "x 5" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,6:0:16-6:3:19", + "key": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,6:0:16-6:3:19", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,6:0:16-6:3:19", + "value": [ + { + "string": "x 4", + "raw_string": "x 4" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + }, + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,7:0:20-9:1:32", + "key": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,7:0:20-7:1:21", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,7:0:20-7:1:21", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,7:3:23-9:0:31", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,8:2:27-8:5:30", + "key": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,8:2:27-8:5:30", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,8:2:27-8:5:30", + "value": [ + { + "string": "x 3", + "raw_string": "x 3" + } + ] + } + } + ] + }, + "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": "x", + "id_val": "x", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,0:0:0-0:1:1", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "x" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "x 2", + "id_val": "x 2", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,1:0:2-1:3:5", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,1:0:2-1:3:5", + "value": [ + { + "string": "x 2", + "raw_string": "x 2" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "x 2" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "x 3", + "id_val": "x 3", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,3:0:7-3:3:10", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,3:0:7-3:3:10", + "value": [ + { + "string": "x 3", + "raw_string": "x 3" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "x 3" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "x 5", + "id_val": "x 5", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,4:0:11-4:3:14", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,4:0:11-4:3:14", + "value": [ + { + "string": "x 5", + "raw_string": "x 5" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "x 5" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "x 4", + "id_val": "x 4", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,6:0:16-6:3:19", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,6:0:16-6:3:19", + "value": [ + { + "string": "x 4", + "raw_string": "x 4" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "x 4" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "y", + "id_val": "y", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,7:0:20-7:1:21", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,7:0:20-7:1:21", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "y" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "x 3", + "id_val": "x 3", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,8:2:27-8:5:30", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/duplicate_generated.d2,8:2:27-8:5:30", + "value": [ + { + "string": "x 3", + "raw_string": "x 3" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "x 3" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + } + ] + }, + "err": "" +}