From 63770832d01a86a7464fde744e94058aae07de2e Mon Sep 17 00:00:00 2001 From: skirtle <65301168+skirtles-code@users.noreply.github.com> Date: Sun, 3 Mar 2024 09:01:47 +0000 Subject: [PATCH] feat(dx): warn when `addRoute` cannot find the parent (#2157) --- packages/router/__tests__/router.spec.ts | 17 +++++++++++++++++ packages/router/src/router.ts | 8 ++++++++ 2 files changed, 25 insertions(+) diff --git a/packages/router/__tests__/router.spec.ts b/packages/router/__tests__/router.spec.ts index 951f2f0db..e762971b5 100644 --- a/packages/router/__tests__/router.spec.ts +++ b/packages/router/__tests__/router.spec.ts @@ -1076,5 +1076,22 @@ describe('Router', () => { name: 'Param', }) }) + + it('warns when the parent route is missing', async () => { + const { router } = await newRouter() + router.addRoute('parent-route', { + path: '/p', + component: components.Foo, + }) + expect( + 'Parent route "parent-route" not found when adding child route' + ).toHaveBeenWarned() + }) + + it('warns when removing a missing route', async () => { + const { router } = await newRouter() + router.removeRoute('route-name') + expect('Cannot remove non-existent route "route-name"').toHaveBeenWarned() + }) }) }) diff --git a/packages/router/src/router.ts b/packages/router/src/router.ts index b1ec39dfd..8fec39d82 100644 --- a/packages/router/src/router.ts +++ b/packages/router/src/router.ts @@ -397,6 +397,14 @@ export function createRouter(options: RouterOptions): Router { let record: RouteRecordRaw if (isRouteName(parentOrRoute)) { parent = matcher.getRecordMatcher(parentOrRoute) + if (__DEV__ && !parent) { + warn( + `Parent route "${String( + parentOrRoute + )}" not found when adding child route`, + route + ) + } record = route! } else { record = parentOrRoute