Skip to content

Commit

Permalink
feat(warn): warn when params are provided alongside path
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed May 1, 2020
1 parent fda1a62 commit 8a8ddf1
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/encoding.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { warn } from 'vue'
import { warn } from './warning'

/**
* Encoding Rules ␣ = Space Path: ␣ " < > # ? { } Query: ␣ " < > # & = Hash: ␣ "
Expand Down
4 changes: 2 additions & 2 deletions src/history/html5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion src/matcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
PathParserOptions,
_PathParserOptions,
} from './pathParserRanker'
import { warn } from 'vue'
import { warn } from '../warning'

let noop = () => {}

Expand Down
11 changes: 8 additions & 3 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/scrollBehavior.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RouteLocationNormalized, RouteLocationNormalizedLoaded } from './types'
import { warn } from 'vue'
import { warn } from './warning'

export type ScrollPositionCoordinates = {
/**
Expand Down
12 changes: 12 additions & 0 deletions src/warning.ts
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 8a8ddf1

Please sign in to comment.