Skip to content

Commit

Permalink
feat(warn): warn against removed params
Browse files Browse the repository at this point in the history
See #1527
  • Loading branch information
posva committed Aug 26, 2022
1 parent d56ba0a commit fdbaf83
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
9 changes: 6 additions & 3 deletions packages/router/__tests__/matcher/resolve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const component: RouteComponent = defineComponent({})
const components = { default: component }

describe('RouterMatcher.resolve', () => {
mockWarn()

function assertRecordMatch(
record: RouteRecordRaw | RouteRecordRaw[],
location: MatcherLocationRaw,
Expand Down Expand Up @@ -775,17 +777,19 @@ describe('RouterMatcher.resolve', () => {
)
})

it('drops non existent params', () => {
it('discards non existent params', () => {
assertRecordMatch(
{ path: '/', name: 'home', components },
{ name: 'home', params: { a: 'b' } },
{ name: 'home', params: { a: 'a', b: 'b' } },
{ name: 'home', path: '/', params: {} }
)
expect('invalid param(s) "a", "b" ').toHaveBeenWarned()
assertRecordMatch(
{ path: '/:b', name: 'a', components },
{ name: 'a', params: { a: 'a', b: 'b' } },
{ name: 'a', path: '/b', params: { b: 'b' } }
)
expect('invalid param(s) "a"').toHaveBeenWarned()
})

it('drops optional params', () => {
Expand Down Expand Up @@ -818,7 +822,6 @@ describe('RouterMatcher.resolve', () => {
})

describe('LocationAsRelative', () => {
mockWarn()
it('warns if a path isn not absolute', () => {
const record = {
path: '/parent',
Expand Down
24 changes: 23 additions & 1 deletion packages/router/__tests__/warnings.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mockWarn } from 'jest-mock-warn'
import { createMemoryHistory, createRouter } from '../src'
import { createMemoryHistory, createRouter, createRouterMatcher } from '../src'
import {
defineAsyncComponent,
defineComponent,
Expand Down Expand Up @@ -287,4 +287,26 @@ describe('warnings', () => {
'It should be called exactly one time in each navigation guard'
).toHaveBeenWarned()
})

it('warns when discarding params', () => {
const record = {
path: '/a',
name: 'a',
components: {},
}
const matcher = createRouterMatcher([record], {})
matcher.resolve(
{ name: 'a', params: { no: 'a', foo: '35' } },
{
path: '/parent/one',
name: undefined,
params: { old: 'one' },
matched: [] as any,
meta: {},
}
)
expect('invalid param(s) "no", "foo" ').toHaveBeenWarned()
// from the previous location
expect('"one"').not.toHaveBeenWarned()
})
})
15 changes: 15 additions & 0 deletions packages/router/src/matcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,21 @@ export function createRouterMatcher(
location,
})

// warn if the user is passing invalid params so they can debug it better when they get removed
if (__DEV__) {
const invalidParams: string[] = Object.keys(
location.params || {}
).filter(paramName => !matcher!.keys.find(k => k.name === paramName))

if (invalidParams.length) {
warn(
`Discarded invalid param(s) "${invalidParams.join(
'", "'
)}" when navigating. See https://github.com/vuejs/router/blob/main/packages/router/CHANGELOG.md#414-2022-08-22 for more details.`
)
}
}

name = matcher.record.name
params = assign(
// paramsFromLocation is a new object
Expand Down

0 comments on commit fdbaf83

Please sign in to comment.