Skip to content

Commit

Permalink
feat(dx): warn when addRoute cannot find the parent (#2157)
Browse files Browse the repository at this point in the history
  • Loading branch information
skirtles-code committed Mar 3, 2024
1 parent 13303bd commit 6377083
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/router/__tests__/router.spec.ts
Expand Up @@ -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()
})
})
})
8 changes: 8 additions & 0 deletions packages/router/src/router.ts
Expand Up @@ -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
Expand Down

0 comments on commit 6377083

Please sign in to comment.