]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor: code review and fixes
authorEduardo San Martin Morote <posva13@gmail.com>
Sun, 2 Jul 2023 21:38:20 +0000 (23:38 +0200)
committerEduardo San Martin Morote <posva@users.noreply.github.com>
Fri, 15 Dec 2023 14:31:15 +0000 (15:31 +0100)
packages/router/__tests__/warnings.spec.ts
packages/router/src/errors.ts
packages/router/src/matcher/index.ts
packages/router/src/router.ts
packages/router/src/types/index.ts

index f7be962375242debcdeabbff0288eb0e84934d32..82f6450d560c7119e85444b35daab47065d4a2e7 100644 (file)
@@ -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()
   })
index 773be27be26356ea494450ceac7a0bf83acc8b2a..379bf6392ca04839a2384f677c508d86df5fdcc5 100644 (file)
@@ -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<string, unknown>
   for (const key of propertiesToLog) {
     if (key in to) location[key] = to[key]
index f7668dd5e51fb2c63f42ae0831bf5ac8c0029fb3..39a3c24b11c4d80dce72f85479cc15f6238a08dd 100644 (file)
@@ -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
index e5beb1f1b6ccd09e940a2a7e2f5b72084c5344e0..f5cc271eefe00379b15f481c179ed6956beb1a53 100644 (file)
@@ -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
       )
index 346921224fdd7f409b04a46cadd60071a03f2b67..b7e88db138acc4d82a95750f4b723e1abfaaf6da 100644 (file)
@@ -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
 }