Skip to content

Commit

Permalink
feat: add aliasOf to normalized records
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Mar 1, 2020
1 parent 234f531 commit d9f3174
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 5 deletions.
49 changes: 46 additions & 3 deletions __tests__/matcher/resolve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 },
},
],
Expand All @@ -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 },
],
}
Expand Down
1 change: 1 addition & 0 deletions __tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions playground/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@
>/nested/nested/nested</router-link
>
</li>
<li>
<router-link to="/anidado">/anidado</router-link>
</li>
<li>
<router-link to="/anidado/nested">/anidado/nested</router-link>
</li>
<li>
<router-link to="/anidado/nested/nested"
>/anidado/nested/nested</router-link
>
</li>
<li>
<router-link to="/long-0">/long-0</router-link>
</li>
Expand Down
1 change: 1 addition & 0 deletions playground/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const router = createRouter({
{ path: '/:data(.*)', component: NotFound, name: 'NotFound' },
{
path: '/nested',
alias: '/anidado',
component: Nested,
name: 'Nested',
children: [
Expand Down
9 changes: 7 additions & 2 deletions src/matcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ 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!
for (const alias of aliases) {
normalizedRecords.push({
...mainNormalizedRecord,
path: alias,
aliasOf: mainNormalizedRecord,
})
}
}
Expand Down Expand Up @@ -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)
}

/**
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -237,6 +241,7 @@ export function normalizeRouteRecord(
beforeEnter,
meta: record.meta || {},
leaveGuards: [],
aliasOf: undefined,
}
}

Expand Down
1 change: 1 addition & 0 deletions src/matcher/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export interface RouteRecordNormalized {
meta: Exclude<RouteRecordMultipleViews['meta'], void>
beforeEnter: RouteRecordMultipleViews['beforeEnter']
leaveGuards: NavigationGuard[]
aliasOf: RouteRecordNormalized | undefined
}

0 comments on commit d9f3174

Please sign in to comment.