Skip to content

Commit

Permalink
refactor: code review and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Jul 2, 2023
1 parent 798a679 commit 3bac392
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
3 changes: 3 additions & 0 deletions packages/router/__tests__/warnings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('warnings', () => {
history,
routes: [{ path: '/:p', name: 'p', component }],
})
// @ts-expect-error: cannot pass params with a path
router.push({ path: '/p', params: { p: 'p' } })
expect('Path "/p" was passed with params').toHaveBeenWarned()
})
Expand All @@ -42,6 +43,8 @@ describe('warnings', () => {
history,
routes: [{ path: '/:p', name: 'p', component }],
})
// @ts-expect-error: it would be better if this didn't error but it still an
// invalid location
router.push({ path: '/p', name: 'p', params: { p: 'p' } })
expect('Path "/" was passed with params').not.toHaveBeenWarned()
})
Expand Down
2 changes: 1 addition & 1 deletion packages/router/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ const propertiesToLog = ['params', 'query', 'hash'] as const

function stringifyRoute(to: RouteLocationRaw): string {
if (typeof to === 'string') return to
if ('path' in to) return to.path
if (to.path != null) return to.path
const location = {} as Record<string, unknown>
for (const key of propertiesToLog) {
if (key in to) location[key] = to[key]
Expand Down
2 changes: 1 addition & 1 deletion packages/router/src/matcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ export function createRouterMatcher(
)
// throws if cannot be stringified
path = matcher.stringify(params)
} else if ('path' in location && location.path != null) {
} else if (location.path != null) {
// no need to resolve the path with the matcher as it was provided
// this also allows the user to control the encoding
path = location.path
Expand Down
8 changes: 4 additions & 4 deletions packages/router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ export function createRouter(options: RouterOptions): Router {
let matcherLocation: MatcherLocationRaw

// path could be relative in object as well
if ('path' in rawLocation && rawLocation.path != null) {
if (rawLocation.path != null) {
if (
__DEV__ &&
'params' in rawLocation &&
Expand Down Expand Up @@ -528,7 +528,7 @@ export function createRouter(options: RouterOptions): Router {
} else if (!matchedRoute.matched.length) {
warn(
`No match found for location with path "${
'path' in rawLocation ? rawLocation.path : rawLocation
rawLocation.path != null ? rawLocation.path : rawLocation
}"`
)
}
Expand Down Expand Up @@ -609,7 +609,7 @@ export function createRouter(options: RouterOptions): Router {

if (
__DEV__ &&
!('path' in newTargetLocation) &&
newTargetLocation.path == null &&
!('name' in newTargetLocation)
) {
warn(
Expand All @@ -629,7 +629,7 @@ export function createRouter(options: RouterOptions): Router {
query: to.query,
hash: to.hash,
// avoid transferring params if the redirect has a path
params: 'path' in newTargetLocation ? {} : to.params,
params: newTargetLocation.path != null ? {} : to.params,
},
newTargetLocation
)
Expand Down
6 changes: 6 additions & 0 deletions packages/router/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,17 @@ export interface MatcherLocationAsPath {
*/
export interface MatcherLocationAsName {
name: RouteRecordName
// to allow checking location.path == null
path?: undefined
params?: RouteParams
}

/**
* @internal
*/
export interface MatcherLocationAsRelative {
// to allow checking location.path == null
path?: undefined
params?: RouteParams
}

Expand All @@ -73,6 +77,8 @@ export interface MatcherLocationAsRelative {
*/
export interface LocationAsRelativeRaw {
name?: RouteRecordName
// to allow checking location.path == null
path?: undefined
params?: RouteParamsRaw
}

Expand Down

0 comments on commit 3bac392

Please sign in to comment.