Skip to content

Commit fdbaf83

Browse files
committed
feat(warn): warn against removed params
See #1527
1 parent d56ba0a commit fdbaf83

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

packages/router/__tests__/matcher/resolve.spec.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const component: RouteComponent = defineComponent({})
1616
const components = { default: component }
1717

1818
describe('RouterMatcher.resolve', () => {
19+
mockWarn()
20+
1921
function assertRecordMatch(
2022
record: RouteRecordRaw | RouteRecordRaw[],
2123
location: MatcherLocationRaw,
@@ -775,17 +777,19 @@ describe('RouterMatcher.resolve', () => {
775777
)
776778
})
777779

778-
it('drops non existent params', () => {
780+
it('discards non existent params', () => {
779781
assertRecordMatch(
780782
{ path: '/', name: 'home', components },
781-
{ name: 'home', params: { a: 'b' } },
783+
{ name: 'home', params: { a: 'a', b: 'b' } },
782784
{ name: 'home', path: '/', params: {} }
783785
)
786+
expect('invalid param(s) "a", "b" ').toHaveBeenWarned()
784787
assertRecordMatch(
785788
{ path: '/:b', name: 'a', components },
786789
{ name: 'a', params: { a: 'a', b: 'b' } },
787790
{ name: 'a', path: '/b', params: { b: 'b' } }
788791
)
792+
expect('invalid param(s) "a"').toHaveBeenWarned()
789793
})
790794

791795
it('drops optional params', () => {
@@ -818,7 +822,6 @@ describe('RouterMatcher.resolve', () => {
818822
})
819823

820824
describe('LocationAsRelative', () => {
821-
mockWarn()
822825
it('warns if a path isn not absolute', () => {
823826
const record = {
824827
path: '/parent',

packages/router/__tests__/warnings.spec.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { mockWarn } from 'jest-mock-warn'
2-
import { createMemoryHistory, createRouter } from '../src'
2+
import { createMemoryHistory, createRouter, createRouterMatcher } from '../src'
33
import {
44
defineAsyncComponent,
55
defineComponent,
@@ -287,4 +287,26 @@ describe('warnings', () => {
287287
'It should be called exactly one time in each navigation guard'
288288
).toHaveBeenWarned()
289289
})
290+
291+
it('warns when discarding params', () => {
292+
const record = {
293+
path: '/a',
294+
name: 'a',
295+
components: {},
296+
}
297+
const matcher = createRouterMatcher([record], {})
298+
matcher.resolve(
299+
{ name: 'a', params: { no: 'a', foo: '35' } },
300+
{
301+
path: '/parent/one',
302+
name: undefined,
303+
params: { old: 'one' },
304+
matched: [] as any,
305+
meta: {},
306+
}
307+
)
308+
expect('invalid param(s) "no", "foo" ').toHaveBeenWarned()
309+
// from the previous location
310+
expect('"one"').not.toHaveBeenWarned()
311+
})
290312
})

packages/router/src/matcher/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,21 @@ export function createRouterMatcher(
247247
location,
248248
})
249249

250+
// warn if the user is passing invalid params so they can debug it better when they get removed
251+
if (__DEV__) {
252+
const invalidParams: string[] = Object.keys(
253+
location.params || {}
254+
).filter(paramName => !matcher!.keys.find(k => k.name === paramName))
255+
256+
if (invalidParams.length) {
257+
warn(
258+
`Discarded invalid param(s) "${invalidParams.join(
259+
'", "'
260+
)}" when navigating. See https://github.com/vuejs/router/blob/main/packages/router/CHANGELOG.md#414-2022-08-22 for more details.`
261+
)
262+
}
263+
}
264+
250265
name = matcher.record.name
251266
params = assign(
252267
// paramsFromLocation is a new object

0 commit comments

Comments
 (0)