From fdbaf83a5fa359343697ec26f4f4a54aac73b241 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 26 Aug 2022 11:53:45 +0200 Subject: [PATCH] feat(warn): warn against removed params See #1527 --- .../router/__tests__/matcher/resolve.spec.ts | 9 ++++--- packages/router/__tests__/warnings.spec.ts | 24 ++++++++++++++++++- packages/router/src/matcher/index.ts | 15 ++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/packages/router/__tests__/matcher/resolve.spec.ts b/packages/router/__tests__/matcher/resolve.spec.ts index c3df1f6a..2fc768ed 100644 --- a/packages/router/__tests__/matcher/resolve.spec.ts +++ b/packages/router/__tests__/matcher/resolve.spec.ts @@ -16,6 +16,8 @@ const component: RouteComponent = defineComponent({}) const components = { default: component } describe('RouterMatcher.resolve', () => { + mockWarn() + function assertRecordMatch( record: RouteRecordRaw | RouteRecordRaw[], location: MatcherLocationRaw, @@ -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', () => { @@ -818,7 +822,6 @@ describe('RouterMatcher.resolve', () => { }) describe('LocationAsRelative', () => { - mockWarn() it('warns if a path isn not absolute', () => { const record = { path: '/parent', diff --git a/packages/router/__tests__/warnings.spec.ts b/packages/router/__tests__/warnings.spec.ts index 0d2d9927..0ed5c599 100644 --- a/packages/router/__tests__/warnings.spec.ts +++ b/packages/router/__tests__/warnings.spec.ts @@ -1,5 +1,5 @@ import { mockWarn } from 'jest-mock-warn' -import { createMemoryHistory, createRouter } from '../src' +import { createMemoryHistory, createRouter, createRouterMatcher } from '../src' import { defineAsyncComponent, defineComponent, @@ -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() + }) }) diff --git a/packages/router/src/matcher/index.ts b/packages/router/src/matcher/index.ts index f2bf941b..c2881d44 100644 --- a/packages/router/src/matcher/index.ts +++ b/packages/router/src/matcher/index.ts @@ -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 -- 2.47.2