From: Eduardo San Martin Morote Date: Tue, 7 Apr 2020 10:40:13 +0000 (+0200) Subject: feat: allow symbols as route record name X-Git-Tag: v4.0.0-alpha.5~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f42ab3fecfaecddcef0ccf8bb0f7f44ca24d6160;p=thirdparty%2Fvuejs%2Frouter.git feat: allow symbols as route record name --- diff --git a/src/matcher/index.ts b/src/matcher/index.ts index 6fcb4ca4..d9da0168 100644 --- a/src/matcher/index.ts +++ b/src/matcher/index.ts @@ -1,4 +1,10 @@ -import { RouteRecordRaw, MatcherLocationRaw, MatcherLocation } from '../types' +import { + RouteRecordRaw, + MatcherLocationRaw, + MatcherLocation, + isRouteName, + RouteRecordName, +} from '../types' import { createRouterError, ErrorTypes, MatcherError } from '../errors' import { createRouteRecordMatcher, RouteRecordMatcher } from './path-matcher' import { RouteRecordRedirect, RouteRecordNormalized } from './types' @@ -14,12 +20,10 @@ interface RouterMatcher { addRoute: (record: RouteRecordRaw, parent?: RouteRecordMatcher) => () => void removeRoute: { (matcher: RouteRecordMatcher): void - (name: Required['name']): void + (name: RouteRecordName): void } getRoutes: () => RouteRecordMatcher[] - getRecordMatcher: ( - name: Required['name'] - ) => RouteRecordMatcher | undefined + getRecordMatcher: (name: RouteRecordName) => RouteRecordMatcher | undefined resolve: ( location: MatcherLocationRaw, currentLocation: MatcherLocation @@ -32,9 +36,9 @@ export function createRouterMatcher( ): RouterMatcher { // normalized ordered array of matchers const matchers: RouteRecordMatcher[] = [] - const matcherMap = new Map() + const matcherMap = new Map() - function getRecordMatcher(name: string) { + function getRecordMatcher(name: RouteRecordName) { return matcherMap.get(name) } @@ -130,8 +134,8 @@ export function createRouterMatcher( : noop } - function removeRoute(matcherRef: string | RouteRecordMatcher) { - if (typeof matcherRef === 'string') { + function removeRoute(matcherRef: RouteRecordName | RouteRecordMatcher) { + if (isRouteName(matcherRef)) { const matcher = matcherMap.get(matcherRef) if (matcher) { matcherMap.delete(matcherRef) diff --git a/src/router.ts b/src/router.ts index e06595ec..be9b9a14 100644 --- a/src/router.ts +++ b/src/router.ts @@ -10,6 +10,8 @@ import { MatcherLocation, RouteLocationNormalizedLoaded, RouteLocation, + RouteRecordName, + isRouteName, } from './types' import { RouterHistory, HistoryState } from './history/common' import { @@ -83,9 +85,10 @@ export interface Router { history: RouterHistory currentRoute: Ref - addRoute(parentName: string, route: RouteRecordRaw): () => void + addRoute(parentName: RouteRecordName, route: RouteRecordRaw): () => void addRoute(route: RouteRecordRaw): () => void - removeRoute(name: string): void + removeRoute(name: RouteRecordName): void + // TODO: hasRoute() getRoutes(): RouteRecord[] resolve(to: RouteLocationRaw): RouteLocation @@ -132,12 +135,12 @@ export function createRouter({ const decodeParams = applyToParams.bind(null, decode) function addRoute( - parentOrRoute: string | RouteRecordRaw, + parentOrRoute: RouteRecordName | RouteRecordRaw, route?: RouteRecordRaw ) { let parent: Parameters[1] | undefined let record: RouteRecordRaw - if (typeof parentOrRoute === 'string') { + if (isRouteName(parentOrRoute)) { parent = matcher.getRecordMatcher(parentOrRoute) record = route! } else { @@ -147,13 +150,13 @@ export function createRouter({ return matcher.addRoute(record, parent) } - function removeRoute(name: string) { + function removeRoute(name: RouteRecordName) { let recordMatcher = matcher.getRecordMatcher(name) if (recordMatcher) { matcher.removeRoute(recordMatcher) } else if (__DEV__) { // TODO: adapt if we allow Symbol as a name - warn(`Cannot remove non-existent route "${name}"`) + warn(`Cannot remove non-existent route "${String(name)}"`) } } diff --git a/src/types/index.ts b/src/types/index.ts index 043b63ac..e1ce660c 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -43,7 +43,7 @@ export interface LocationAsPath { } export interface LocationAsName { - name: string + name: RouteRecordName params?: RouteParamsRaw } @@ -90,7 +90,7 @@ export interface _RouteLocationBase { fullPath: string query: LocationQuery hash: string - name: string | null | undefined + name: RouteRecordName | null | undefined params: RouteParams // TODO: make it an array? redirectedFrom: RouteLocation | undefined @@ -171,6 +171,8 @@ export interface RouteComponentInterface { export type RouteComponent = ComponentOptions & RouteComponentInterface export type RawRouteComponent = RouteComponent | Lazy +export type RouteRecordName = string | symbol + // TODO: could this be moved to matcher? /** * Common properties among all kind of {@link RouteRecordRaw} @@ -190,7 +192,7 @@ export interface _RouteRecordBase { /** * Name for the route record. */ - name?: string + name?: RouteRecordName /** * Allow passing down params as props to the component rendered by `router-view`. */ diff --git a/src/types/type-guards.ts b/src/types/type-guards.ts index a00d4dbf..782ef08c 100644 --- a/src/types/type-guards.ts +++ b/src/types/type-guards.ts @@ -1,5 +1,9 @@ -import { RouteLocationRaw } from './index' +import { RouteLocationRaw, RouteRecordName } from './index' export function isRouteLocation(route: any): route is RouteLocationRaw { return typeof route === 'string' || (route && typeof route === 'object') } + +export function isRouteName(name: any): name is RouteRecordName { + return typeof name === 'string' || typeof name === 'symbol' +}