From 8a8ddf1a5e5f2d29733da4fe25e4ddb447b0df30 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 1 May 2020 16:46:29 +0200 Subject: [PATCH] feat(warn): warn when params are provided alongside path --- src/encoding.ts | 2 +- src/history/html5.ts | 4 ++-- src/matcher/index.ts | 2 +- src/router.ts | 11 ++++++++--- src/scrollBehavior.ts | 2 +- src/warning.ts | 12 ++++++++++++ 6 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 src/warning.ts diff --git a/src/encoding.ts b/src/encoding.ts index 2d87ecf2..c863c25e 100644 --- a/src/encoding.ts +++ b/src/encoding.ts @@ -1,4 +1,4 @@ -import { warn } from 'vue' +import { warn } from './warning' /** * Encoding Rules ␣ = Space Path: ␣ " < > # ? { } Query: ␣ " < > # & = Hash: ␣ " diff --git a/src/history/html5.ts b/src/history/html5.ts index 3a0ff3f3..e51378a7 100644 --- a/src/history/html5.ts +++ b/src/history/html5.ts @@ -14,7 +14,7 @@ import { computeScrollPosition, ScrollPositionCoordinates, } from '../scrollBehavior' -import { warn } from 'vue' +import { warn } from '../warning' import { stripBase } from '../location' type PopStateListener = (this: Window, ev: PopStateEvent) => any @@ -205,7 +205,7 @@ function useHistoryStateNavigation(base: string) { history[replace ? 'replaceState' : 'pushState'](state, '', url) historyState.value = state } catch (err) { - warn('[vue-router]: Error with push/replace State', err) + warn('Error with push/replace State', err) // Force the navigation, this also resets the call count window.location[replace ? 'replace' : 'assign'](url) } diff --git a/src/matcher/index.ts b/src/matcher/index.ts index 0630b25f..1b0d0da5 100644 --- a/src/matcher/index.ts +++ b/src/matcher/index.ts @@ -14,7 +14,7 @@ import { PathParserOptions, _PathParserOptions, } from './pathParserRanker' -import { warn } from 'vue' +import { warn } from '../warning' let noop = () => {} diff --git a/src/router.ts b/src/router.ts index 2795a42b..8470fafb 100644 --- a/src/router.ts +++ b/src/router.ts @@ -38,11 +38,12 @@ import { parseQuery as originalParseQuery, stringifyQuery as originalStringifyQuery, } from './query' -import { shallowRef, Ref, nextTick, App, warn } from 'vue' +import { shallowRef, Ref, nextTick, App } from 'vue' import { RouteRecord, RouteRecordNormalized } from './matcher/types' import { parseURL, stringifyURL, isSameRouteLocation } from './location' import { extractComponentsGuards, guardToPromiseFn } from './navigationGuards' import { applyRouterPlugin } from './install' +import { warn } from './warning' /** * Internal type to define an ErrorHandler @@ -248,10 +249,14 @@ export function createRouter(options: RouterOptions): Router { } } - // TODO: dev warning if params and path at the same time - // path could be relative in object as well if ('path' in rawLocation) { + if (__DEV__ && 'params' in rawLocation) { + 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` + ) + } rawLocation = { ...rawLocation, path: parseURL(parseQuery, rawLocation.path, currentLocation.path).path, diff --git a/src/scrollBehavior.ts b/src/scrollBehavior.ts index c0fe79f4..f0c69c04 100644 --- a/src/scrollBehavior.ts +++ b/src/scrollBehavior.ts @@ -1,5 +1,5 @@ import { RouteLocationNormalized, RouteLocationNormalizedLoaded } from './types' -import { warn } from 'vue' +import { warn } from './warning' export type ScrollPositionCoordinates = { /** diff --git a/src/warning.ts b/src/warning.ts new file mode 100644 index 00000000..9b1c653b --- /dev/null +++ b/src/warning.ts @@ -0,0 +1,12 @@ +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 +} -- 2.47.3