Skip to content

Commit bf48bce

Browse files
committed
refactor: code review and fixes
1 parent 0ec4862 commit bf48bce

File tree

5 files changed

+15
-6
lines changed

5 files changed

+15
-6
lines changed

packages/router/__tests__/warnings.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ describe('warnings', () => {
3232
history,
3333
routes: [{ path: '/:p', name: 'p', component }],
3434
})
35+
// @ts-expect-error: cannot pass params with a path
3536
router.push({ path: '/p', params: { p: 'p' } })
3637
expect('Path "/p" was passed with params').toHaveBeenWarned()
3738
})
@@ -42,6 +43,8 @@ describe('warnings', () => {
4243
history,
4344
routes: [{ path: '/:p', name: 'p', component }],
4445
})
46+
// @ts-expect-error: it would be better if this didn't error but it still an
47+
// invalid location
4548
router.push({ path: '/p', name: 'p', params: { p: 'p' } })
4649
expect('Path "/" was passed with params').not.toHaveBeenWarned()
4750
})

packages/router/src/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ const propertiesToLog = ['params', 'query', 'hash'] as const
190190

191191
function stringifyRoute(to: RouteLocationRaw): string {
192192
if (typeof to === 'string') return to
193-
if ('path' in to) return to.path
193+
if (to.path != null) return to.path
194194
const location = {} as Record<string, unknown>
195195
for (const key of propertiesToLog) {
196196
if (key in to) location[key] = to[key]

packages/router/src/matcher/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ export function createRouterMatcher(
290290
)
291291
// throws if cannot be stringified
292292
path = matcher.stringify(params)
293-
} else if ('path' in location && location.path != null) {
293+
} else if (location.path != null) {
294294
// no need to resolve the path with the matcher as it was provided
295295
// this also allows the user to control the encoding
296296
path = location.path

packages/router/src/router.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ export function createRouter(options: RouterOptions): Router {
463463
let matcherLocation: MatcherLocationRaw
464464

465465
// path could be relative in object as well
466-
if ('path' in rawLocation && rawLocation.path != null) {
466+
if (rawLocation.path != null) {
467467
if (
468468
__DEV__ &&
469469
'params' in rawLocation &&
@@ -525,7 +525,7 @@ export function createRouter(options: RouterOptions): Router {
525525
} else if (!matchedRoute.matched.length) {
526526
warn(
527527
`No match found for location with path "${
528-
'path' in rawLocation ? rawLocation.path : rawLocation
528+
rawLocation.path != null ? rawLocation.path : rawLocation
529529
}"`
530530
)
531531
}
@@ -606,7 +606,7 @@ export function createRouter(options: RouterOptions): Router {
606606

607607
if (
608608
__DEV__ &&
609-
!('path' in newTargetLocation) &&
609+
newTargetLocation.path == null &&
610610
!('name' in newTargetLocation)
611611
) {
612612
warn(
@@ -626,7 +626,7 @@ export function createRouter(options: RouterOptions): Router {
626626
query: to.query,
627627
hash: to.hash,
628628
// avoid transferring params if the redirect has a path
629-
params: 'path' in newTargetLocation ? {} : to.params,
629+
params: newTargetLocation.path != null ? {} : to.params,
630630
},
631631
newTargetLocation
632632
)

packages/router/src/types/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,17 @@ export interface MatcherLocationAsPath {
5858
*/
5959
export interface MatcherLocationAsName {
6060
name: RouteRecordName
61+
// to allow checking location.path == null
62+
path?: undefined
6163
params?: RouteParams
6264
}
6365

6466
/**
6567
* @internal
6668
*/
6769
export interface MatcherLocationAsRelative {
70+
// to allow checking location.path == null
71+
path?: undefined
6872
params?: RouteParams
6973
}
7074

@@ -73,6 +77,8 @@ export interface MatcherLocationAsRelative {
7377
*/
7478
export interface LocationAsRelativeRaw {
7579
name?: RouteRecordName
80+
// to allow checking location.path == null
81+
path?: undefined
7682
params?: RouteParamsRaw
7783
}
7884

0 commit comments

Comments
 (0)