From: Eduardo San Martin Morote Date: Fri, 1 May 2020 17:00:43 +0000 (+0200) Subject: feat(warn): detect missing param in nested absolute paths X-Git-Tag: v4.0.0-alpha.10~18 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f5b59493a4e27bf07bd5a0d2e109bc6750f6f1a9;p=thirdparty%2Fvuejs%2Frouter.git feat(warn): detect missing param in nested absolute paths --- 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 }