Skip to content

Commit 57b2ad6

Browse files
authored
fix(unplugin): keep parent nodes with components when removing children (#2991)
1 parent e9bec97 commit 57b2ad6

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎packages/router/src/unplugin/core/tree.spec.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,6 +2266,28 @@ describe('Tree', () => {
22662266
expect(nested.children.has('index')).toBe(true)
22672267
expect(nested.children.has('other')).toBe(true)
22682268
})
2269+
2270+
it('keeps _parent node when removing its last child', () => {
2271+
const tree = new PrefixTree(RESOLVED_OPTIONS)
2272+
tree.insert('users/_parent', 'users/_parent.vue')
2273+
tree.insert('users/[id]', 'users/[id].vue')
2274+
2275+
tree.removeChild('users/[id].vue')
2276+
2277+
expect(tree.children.get('users')?.value.components.get('default')).toBe(
2278+
'users/_parent.vue'
2279+
)
2280+
})
2281+
2282+
it('keeps _parent node when its last child deletes itself', () => {
2283+
const tree = new PrefixTree(RESOLVED_OPTIONS)
2284+
tree.insert('users/_parent', 'users/_parent.vue')
2285+
tree.insert('users/[id]', 'users/[id].vue').delete()
2286+
2287+
expect(tree.children.get('users')?.value.components.get('default')).toBe(
2288+
'users/_parent.vue'
2289+
)
2290+
})
22692291
})
22702292

22712293
describe('Invalid param names in file names', () => {

‎packages/router/src/unplugin/core/tree.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ export class TreeNode {
243243
protected deleteChild(child: TreeNode): void {
244244
this.children.delete(child.value.rawSegment)
245245
// recursively delete empty parents
246-
if (!this.isRoot() && !this.isMatchable() && this.children.size === 0) {
246+
if (!this.isRoot() && !this.hasComponents && this.children.size === 0) {
247247
this.delete()
248248
}
249249
}

0 commit comments

Comments
 (0)