]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat(warn): warn for invalid path+params and redirect
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 1 May 2020 15:30:29 +0000 (17:30 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 1 May 2020 15:30:29 +0000 (17:30 +0200)
__tests__/initialNavigation.spec.ts
src/router.ts
src/warning.ts

index 3a597436227867069889224a8817aab0506023b8..cade0b143b7137219913eec590dc341b4ee25ce2 100644 (file)
@@ -51,7 +51,7 @@ describe('Initial Navigation', () => {
   it('handles initial navigation with redirect', async () => {
     const { history, router } = newRouter('/home')
     expect(history.location.fullPath).toBe('/home')
-    // this is done automatically on mount but there is no mount here
+    // this is done automatically on install but there is none here
     await router.push(history.location.fullPath)
     expect(router.currentRoute.value).toMatchObject({ path: '/' })
     await router.push('/foo')
index 8470fafb222d46438bfed6cb1f9015c7d8ae11f0..76a90ec459f92914e8f241588aedda3565e9f898 100644 (file)
@@ -251,10 +251,16 @@ export function createRouter(options: RouterOptions): Router {
 
     // path could be relative in object as well
     if ('path' in rawLocation) {
-      if (__DEV__ && 'params' in rawLocation) {
+      if (
+        __DEV__ &&
+        'params' in rawLocation &&
+        !('name' in rawLocation) &&
+        Object.keys((rawLocation as any).params).length
+      ) {
         warn(
-          // @ts-ignore
-          `Path "${rawLocation.path}" was passed with params but they will be ignored. Use a named route instead or build the path yourself`
+          `Path "${
+            (rawLocation as any).path
+          }" was passed with params but they will be ignored. Use a named route alongside params instead.`
         )
       }
       rawLocation = {
@@ -332,8 +338,29 @@ export function createRouter(options: RouterOptions): Router {
       let newTargetLocation = locationAsObject(
         typeof redirect === 'function' ? redirect(targetLocation) : redirect
       )
+
+      if (
+        __DEV__ &&
+        !('path' in newTargetLocation) &&
+        !('name' in newTargetLocation)
+      ) {
+        warn(
+          `Invalid redirect found:\n${JSON.stringify(
+            newTargetLocation,
+            null,
+            2
+          )}\n when navigating to "${
+            targetLocation.fullPath
+          }". A redirect must contain a name or path.`
+        )
+      }
       return pushWithRedirect(
         {
+          // having a path here would be a problem with relative locations but
+          // at the same time it doesn't make sense for a redirect to be
+          // relative (no name, no path) because it would create an infinite
+          // loop. Since newTargetLocation must either have a `path` or a
+          // `name`, this will never happen
           ...targetLocation,
           ...newTargetLocation,
           state: data,
index 9b1c653b818016dc6edaaff67a7f97d06e17895f..58ad55494c24e17c1b8b66ac765ae31c49eff50b 100644 (file)
@@ -1,12 +1,3 @@
-import { warn as vueWarn } from 'vue'
-
-const originalWarn = console.warn
-function customWarn(msg: string, ...args: any[]) {
-  originalWarn(msg.replace('Vue warn', 'Vue Router warn'), ...args)
-}
-
 export function warn(msg: string, ...args: any[]) {
-  console.warn = customWarn
-  vueWarn(msg, ...args)
-  console.warn = originalWarn
+  console.warn('[Vue Router warn]: ' + msg, ...args)
 }