From: Eduardo San Martin Morote Date: Fri, 15 Jan 2021 09:13:45 +0000 (+0100) Subject: fix(warn): should not warn missing optional params in aliases X-Git-Tag: v4.0.4~51 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=92f8901f54775cb4b3d1f2415b6a2b3ff77eb440;p=thirdparty%2Fvuejs%2Frouter.git fix(warn): should not warn missing optional params in aliases --- diff --git a/__tests__/matcher/addingRemoving.spec.ts b/__tests__/matcher/addingRemoving.spec.ts index 6130754f..276fa9aa 100644 --- a/__tests__/matcher/addingRemoving.spec.ts +++ b/__tests__/matcher/addingRemoving.spec.ts @@ -391,10 +391,25 @@ describe('Matcher: adding and removing records', () => { describe('warnings', () => { mockWarn() - // TODO: add warnings for invalid records - it.skip('warns if alias is missing a required param', () => { + it('warns if alias is missing a required param', () => { createRouterMatcher([{ path: '/:id', alias: '/no-id', component }], {}) - expect('TODO').toHaveBeenWarned() + expect('same param named "id"').toHaveBeenWarned() + }) + + it('does not warn for optional param on alias', () => { + createRouterMatcher( + [{ path: '/:id', alias: '/:id-:suffix?', component }], + {} + ) + expect('same param named').not.toHaveBeenWarned() + }) + + it('does not warn for optional param on main record', () => { + createRouterMatcher( + [{ alias: '/:id', path: '/:id-:suffix?', component }], + {} + ) + expect('same param named').not.toHaveBeenWarned() }) }) }) diff --git a/src/matcher/index.ts b/src/matcher/index.ts index 2b2a0a56..039d2f87 100644 --- a/src/matcher/index.ts +++ b/src/matcher/index.ts @@ -425,15 +425,21 @@ function isSameParam(a: ParamKey, b: ParamKey): boolean { ) } +/** + * Check if a path and its alias have the same required params + * + * @param a - original record + * @param b - alias record + */ function checkSameParams(a: RouteRecordMatcher, b: RouteRecordMatcher) { for (let key of a.keys) { - if (!b.keys.find(isSameParam.bind(null, key))) + if (!key.optional && !b.keys.find(isSameParam.bind(null, key))) return warn( `Alias "${b.record.path}" and the original record: "${a.record.path}" should have the exact same param named "${key.name}"` ) } for (let key of b.keys) { - if (!a.keys.find(isSameParam.bind(null, key))) + if (!key.optional && !a.keys.find(isSameParam.bind(null, key))) return warn( `Alias "${b.record.path}" and the original record: "${a.record.path}" should have the exact same param named "${key.name}"` )