From: Eduardo San Martin Morote Date: Fri, 1 May 2020 15:30:29 +0000 (+0200) Subject: feat(warn): warn for invalid path+params and redirect X-Git-Tag: v4.0.0-alpha.10~21 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=91f4de9aab99231fb39ed4cc5b4052979afda216;p=thirdparty%2Fvuejs%2Frouter.git feat(warn): warn for invalid path+params and redirect --- diff --git a/__tests__/initialNavigation.spec.ts b/__tests__/initialNavigation.spec.ts index 3a597436..cade0b14 100644 --- a/__tests__/initialNavigation.spec.ts +++ b/__tests__/initialNavigation.spec.ts @@ -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') diff --git a/src/router.ts b/src/router.ts index 8470fafb..76a90ec4 100644 --- a/src/router.ts +++ b/src/router.ts @@ -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, diff --git a/src/warning.ts b/src/warning.ts index 9b1c653b..58ad5549 100644 --- a/src/warning.ts +++ b/src/warning.ts @@ -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) }