From f5b59493a4e27bf07bd5a0d2e109bc6750f6f1a9 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 1 May 2020 19:00:43 +0200 Subject: [PATCH] feat(warn): detect missing param in nested absolute paths --- __tests__/warnings.spec.ts | 22 ++++++++++++++++++++++ src/matcher/index.ts | 15 +++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/__tests__/warnings.spec.ts b/__tests__/warnings.spec.ts index a188219a..7b54705b 100644 --- a/__tests__/warnings.spec.ts +++ b/__tests__/warnings.spec.ts @@ -49,6 +49,28 @@ describe('warnings', () => { ).toHaveBeenWarned() }) + it('warns if a child with absolute path is missing a parent param', async () => { + createRouter({ + history: createMemoryHistory(), + routes: [ + { + path: '/:a', + component, + children: [ + { + path: ':b', + component, + children: [{ path: '/:a/b', component }], + }, + ], + }, + ], + }) + expect( + `Absolute path "/:a/b" should have the exact same param named "b" as its parent "/:a/:b".` + ).toHaveBeenWarned() + }) + it('warns if an alias has a param with the same name but different', async () => { createRouter({ history: createMemoryHistory(), diff --git a/src/matcher/index.ts b/src/matcher/index.ts index 02552b74..d4eeb1eb 100644 --- a/src/matcher/index.ts +++ b/src/matcher/index.ts @@ -102,6 +102,9 @@ export function createRouterMatcher( // create the object before hand so it can be passed to children matcher = createRouteRecordMatcher(normalizedRecord, parent, options) + if (__DEV__ && parent && path[0] === '/') + checkMissingParamsInAbsolutePath(matcher, parent) + // if we are an alias we must tell the original record that we exist // so we can be removed if (originalRecord) { @@ -390,4 +393,16 @@ function checkSameParams(a: RouteRecordMatcher, b: RouteRecordMatcher) { } } +function checkMissingParamsInAbsolutePath( + record: RouteRecordMatcher, + parent: RouteRecordMatcher +) { + for (let key of parent.keys) { + if (!record.keys.find(isSameParam.bind(null, key))) + return warn( + `Absolute path "${record.record.path}" should have the exact same param named "${key.name}" as its parent "${parent.record.path}".` + ) + } +} + export { PathParserOptions, _PathParserOptions } -- 2.47.3