delete only underscore

This commit is contained in:
Alexander Wang 2023-02-15 14:09:56 -08:00
parent 7584467834
commit 28f61ad4e6
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
4 changed files with 480 additions and 4 deletions

View file

@ -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 &&

View file

@ -3189,6 +3189,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
`,
},
{

View file

@ -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": "<nil>"
}

185
testdata/d2oracle/TestDelete/only-underscore.exp.json generated vendored Normal file
View file

@ -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": "<nil>"
}