From: Eduardo San Martin Morote Date: Sun, 1 Mar 2020 18:31:17 +0000 (+0100) Subject: feat: add aliasOf to normalized records X-Git-Tag: v4.0.0-alpha.2~24 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d9f31748802c39572254691108b0667cfd40e911;p=thirdparty%2Fvuejs%2Frouter.git feat: add aliasOf to normalized records --- diff --git a/__tests__/matcher/resolve.spec.ts b/__tests__/matcher/resolve.spec.ts index 088a2724..01ca72e2 100644 --- a/__tests__/matcher/resolve.spec.ts +++ b/__tests__/matcher/resolve.spec.ts @@ -48,7 +48,10 @@ describe('Router Matcher', () => { resolved.matched = record.map(normalizeRouteRecord) // allow passing an expect.any(Array) else if (Array.isArray(resolved.matched)) - resolved.matched = resolved.matched.map(normalizeRouteRecord as any) + resolved.matched = resolved.matched.map(m => ({ + ...normalizeRouteRecord(m as any), + aliasOf: m.aliasOf, + })) } // allows not passing params @@ -60,7 +63,10 @@ describe('Router Matcher', () => { const startCopy = { ...start, - matched: start.matched.map(normalizeRouteRecord), + matched: start.matched.map(m => ({ + ...normalizeRouteRecord(m), + aliasOf: m.aliasOf, + })), } // make matched non enumerable @@ -111,6 +117,38 @@ describe('Router Matcher', () => { path: '/home', name: 'Home', components, + aliasOf: expect.objectContaining({ name: 'Home', path: '/' }), + meta: { foo: true }, + }, + ], + } + ) + }) + + it.todo('multiple aliases') + it.todo('resolve named child with parent with alias') + + it('resolves the original record by name', () => { + assertRecordMatch( + { + path: '/', + alias: '/home', + name: 'Home', + components, + meta: { foo: true }, + }, + { name: 'Home' }, + { + name: 'Home', + path: '/', + params: {}, + meta: { foo: true }, + matched: [ + { + path: '/', + name: 'Home', + components, + aliasOf: undefined, meta: { foo: true }, }, ], @@ -133,7 +171,12 @@ describe('Router Matcher', () => { name: 'nested', params: {}, matched: [ - { path: '/p', children, components }, + { + path: '/p', + children, + components, + aliasOf: expect.objectContaining({ path: '/parent' }), + }, { path: '/p/one', name: 'nested', components }, ], } diff --git a/__tests__/utils.ts b/__tests__/utils.ts index 16299e7b..1b88eeb5 100644 --- a/__tests__/utils.ts +++ b/__tests__/utils.ts @@ -22,6 +22,7 @@ export interface RouteRecordViewLoose 'path' | 'name' | 'components' | 'children' | 'meta' | 'beforeEnter' > { leaveGuards?: any + aliasOf: RouteRecordViewLoose | undefined } // @ts-ignore we are intentionally overriding the type diff --git a/playground/App.vue b/playground/App.vue index 78823d42..5c6c567d 100644 --- a/playground/App.vue +++ b/playground/App.vue @@ -77,6 +77,17 @@ >/nested/nested/nested +
  • + /anidado +
  • +
  • + /anidado/nested +
  • +
  • + /anidado/nested/nested +
  • /long-0
  • diff --git a/playground/router.ts b/playground/router.ts index 760a2cc7..68d1a5fe 100644 --- a/playground/router.ts +++ b/playground/router.ts @@ -49,6 +49,7 @@ export const router = createRouter({ { path: '/:data(.*)', component: NotFound, name: 'NotFound' }, { path: '/nested', + alias: '/anidado', component: Nested, name: 'Nested', children: [ diff --git a/src/matcher/index.ts b/src/matcher/index.ts index 0269bc77..7ccf9d9a 100644 --- a/src/matcher/index.ts +++ b/src/matcher/index.ts @@ -53,7 +53,6 @@ export function createRouterMatcher( const options: PathParserOptions = { ...globalOptions, ...record.options } // generate an array of records to correctly handle aliases const normalizedRecords: RouteRecordNormalized[] = [mainNormalizedRecord] - // TODO: remember aliases in records to allow active in router-link if ('alias' in record) { const aliases = typeof record.alias === 'string' ? [record.alias] : record.alias! @@ -61,6 +60,7 @@ export function createRouterMatcher( normalizedRecords.push({ ...mainNormalizedRecord, path: alias, + aliasOf: mainNormalizedRecord, }) } } @@ -131,7 +131,9 @@ export function createRouterMatcher( // console.log('END i is', { i }) // while (i < matchers.length && matcher.score <= matchers[i].score) i++ matchers.splice(i, 0, matcher) - if (matcher.record.name) matcherMap.set(matcher.record.name, matcher) + // only add the original record to the name map + if (matcher.record.name && !matcher.record.aliasOf) + matcherMap.set(matcher.record.name, matcher) } /** @@ -187,6 +189,8 @@ export function createRouterMatcher( let parentMatcher: RouteRecordMatcher | void = matcher while (parentMatcher) { // reversed order so parents are at the beginning + // const { record } = parentMatcher + // TODO: check resolving child routes by path when parent has an alias matched.unshift(parentMatcher.record) parentMatcher = parentMatcher.parent } @@ -237,6 +241,7 @@ export function normalizeRouteRecord( beforeEnter, meta: record.meta || {}, leaveGuards: [], + aliasOf: undefined, } } diff --git a/src/matcher/types.ts b/src/matcher/types.ts index 5de1ca03..58e26039 100644 --- a/src/matcher/types.ts +++ b/src/matcher/types.ts @@ -9,4 +9,5 @@ export interface RouteRecordNormalized { meta: Exclude beforeEnter: RouteRecordMultipleViews['beforeEnter'] leaveGuards: NavigationGuard[] + aliasOf: RouteRecordNormalized | undefined }