From: Eduardo San Martin Morote Date: Sun, 2 Jul 2023 21:38:20 +0000 (+0200) Subject: refactor: code review and fixes X-Git-Tag: v4.3.0~38 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bf48bcef209f3795c474bd03168be903a0305491;p=thirdparty%2Fvuejs%2Frouter.git refactor: code review and fixes --- diff --git a/packages/router/__tests__/warnings.spec.ts b/packages/router/__tests__/warnings.spec.ts index f7be9623..82f6450d 100644 --- a/packages/router/__tests__/warnings.spec.ts +++ b/packages/router/__tests__/warnings.spec.ts @@ -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() }) @@ -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() }) diff --git a/packages/router/src/errors.ts b/packages/router/src/errors.ts index 773be27b..379bf639 100644 --- a/packages/router/src/errors.ts +++ b/packages/router/src/errors.ts @@ -190,7 +190,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 for (const key of propertiesToLog) { if (key in to) location[key] = to[key] diff --git a/packages/router/src/matcher/index.ts b/packages/router/src/matcher/index.ts index f7668dd5..39a3c24b 100644 --- a/packages/router/src/matcher/index.ts +++ b/packages/router/src/matcher/index.ts @@ -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 diff --git a/packages/router/src/router.ts b/packages/router/src/router.ts index e5beb1f1..f5cc271e 100644 --- a/packages/router/src/router.ts +++ b/packages/router/src/router.ts @@ -463,7 +463,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 && @@ -525,7 +525,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 }"` ) } @@ -606,7 +606,7 @@ export function createRouter(options: RouterOptions): Router { if ( __DEV__ && - !('path' in newTargetLocation) && + newTargetLocation.path == null && !('name' in newTargetLocation) ) { warn( @@ -626,7 +626,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 ) diff --git a/packages/router/src/types/index.ts b/packages/router/src/types/index.ts index 34692122..b7e88db1 100644 --- a/packages/router/src/types/index.ts +++ b/packages/router/src/types/index.ts @@ -58,6 +58,8 @@ export interface MatcherLocationAsPath { */ export interface MatcherLocationAsName { name: RouteRecordName + // to allow checking location.path == null + path?: undefined params?: RouteParams } @@ -65,6 +67,8 @@ export interface MatcherLocationAsName { * @internal */ export interface MatcherLocationAsRelative { + // to allow checking location.path == null + path?: undefined params?: RouteParams } @@ -73,6 +77,8 @@ export interface MatcherLocationAsRelative { */ export interface LocationAsRelativeRaw { name?: RouteRecordName + // to allow checking location.path == null + path?: undefined params?: RouteParamsRaw }