From: Eduardo San Martin Morote Date: Thu, 13 Jun 2024 08:50:34 +0000 (+0200) Subject: refactor: use RouteRecordNameGeneric X-Git-Tag: v4.4.0-alpha.3~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2510170ffa620d90869ebf1aa8b5daf241fe1a78;p=thirdparty%2Fvuejs%2Frouter.git refactor: use RouteRecordNameGeneric It seems to still have value as it work in app code... --- diff --git a/packages/router/src/index.ts b/packages/router/src/index.ts index c40e2d2c..2a62ad15 100644 --- a/packages/router/src/index.ts +++ b/packages/router/src/index.ts @@ -114,6 +114,7 @@ export type { // route records RouteRecordInfo, + RouteRecordNameGeneric, RouteRecordName, _RouteRecordProps, RouteRecordRedirectOption, diff --git a/packages/router/src/matcher/index.ts b/packages/router/src/matcher/index.ts index c4ad8f55..fc88daca 100644 --- a/packages/router/src/matcher/index.ts +++ b/packages/router/src/matcher/index.ts @@ -18,7 +18,7 @@ import { comparePathParserScore } from './pathParserRanker' import { warn } from '../warning' import { assign, noop } from '../utils' -import type { RouteRecordName, _RouteRecordProps } from '../typed-routes' +import type { RouteRecordNameGeneric, _RouteRecordProps } from '../typed-routes' /** * Internal RouterMatcher @@ -29,11 +29,11 @@ export interface RouterMatcher { addRoute: (record: RouteRecordRaw, parent?: RouteRecordMatcher) => () => void removeRoute(matcher: RouteRecordMatcher): void - removeRoute(name: NonNullable): void + removeRoute(name: NonNullable): void getRoutes: () => RouteRecordMatcher[] getRecordMatcher: ( - name: NonNullable + name: NonNullable ) => RouteRecordMatcher | undefined /** @@ -61,13 +61,16 @@ export function createRouterMatcher( ): RouterMatcher { // normalized ordered array of matchers const matchers: RouteRecordMatcher[] = [] - const matcherMap = new Map() + const matcherMap = new Map< + NonNullable, + RouteRecordMatcher + >() globalOptions = mergeOptions( { strict: false, end: true, sensitive: false } as PathParserOptions, globalOptions ) - function getRecordMatcher(name: RouteRecordName) { + function getRecordMatcher(name: NonNullable) { return matcherMap.get(name) } @@ -195,7 +198,7 @@ export function createRouterMatcher( } function removeRoute( - matcherRef: NonNullable | RouteRecordMatcher + matcherRef: NonNullable | RouteRecordMatcher ) { if (isRouteName(matcherRef)) { const matcher = matcherMap.get(matcherRef) diff --git a/packages/router/src/router.ts b/packages/router/src/router.ts index db12647f..20df891a 100644 --- a/packages/router/src/router.ts +++ b/packages/router/src/router.ts @@ -18,7 +18,7 @@ import type { RouteLocationAsRelative, RouteLocationAsPath, RouteLocationAsString, - RouteRecordName, + RouteRecordNameGeneric, } from './typed-routes' import { RouterHistory, HistoryState, NavigationType } from './history/common' import { @@ -212,8 +212,8 @@ export interface Router { * @param route - Route Record to add */ addRoute( - // NOTE: RouteRecordName could be `keyof RouteMap` but the point of dynamic routes is not knowing the routes at build - parentName: NonNullable, + // NOTE: it could be `keyof RouteMap` but the point of dynamic routes is not knowing the routes at build + parentName: NonNullable, route: RouteRecordRaw ): () => void /** @@ -227,13 +227,13 @@ export interface Router { * * @param name - Name of the route to remove */ - removeRoute(name: NonNullable): void + removeRoute(name: NonNullable): void /** * Checks if a route with a given name exists * * @param name - Name of the route to check */ - hasRoute(name: NonNullable): boolean + hasRoute(name: NonNullable): boolean /** * Get a full list of all the {@link RouteRecord | route records}. */ @@ -411,7 +411,7 @@ export function createRouter(options: RouterOptions): Router { applyToParams.bind(null, decode) function addRoute( - parentOrRoute: NonNullable | RouteRecordRaw, + parentOrRoute: NonNullable | RouteRecordRaw, route?: RouteRecordRaw ) { let parent: Parameters<(typeof matcher)['addRoute']>[1] | undefined @@ -434,7 +434,7 @@ export function createRouter(options: RouterOptions): Router { return matcher.addRoute(record, parent) } - function removeRoute(name: NonNullable) { + function removeRoute(name: NonNullable) { const recordMatcher = matcher.getRecordMatcher(name) if (recordMatcher) { matcher.removeRoute(recordMatcher) @@ -447,7 +447,7 @@ export function createRouter(options: RouterOptions): Router { return matcher.getRoutes().map(routeMatcher => routeMatcher.record) } - function hasRoute(name: NonNullable): boolean { + function hasRoute(name: NonNullable): boolean { return !!matcher.getRecordMatcher(name) } diff --git a/packages/router/src/typed-routes/route-location.ts b/packages/router/src/typed-routes/route-location.ts index e68f3e2b..3be52576 100644 --- a/packages/router/src/typed-routes/route-location.ts +++ b/packages/router/src/typed-routes/route-location.ts @@ -11,7 +11,7 @@ import type { _LiteralUnion } from '../types/utils' import type { RouteMap, RouteMapGeneric } from './route-map' import type { Router } from '../router' import type { RouteRecord, RouteRecordNormalized } from '../matcher/types' -import type { RouteRecordName } from './route-records' +import type { RouteRecordNameGeneric } from './route-records' /** * Generic version of {@link RouteLocation}. It is used when no {@link RouteMap} is provided. @@ -49,7 +49,7 @@ export type RouteLocationTypedList< * Generic version of {@link RouteLocationNormalized} that is used when no {@link RouteMap} is provided. */ export interface RouteLocationNormalizedGeneric extends _RouteLocationBase { - name: RouteRecordName + name: RouteRecordNameGeneric params: RouteParamsGeneric /** * Array of {@link RouteRecordNormalized} @@ -122,7 +122,7 @@ export type RouteLocationNormalizedLoadedTypedList< export interface RouteLocationAsRelativeGeneric extends RouteQueryAndHash, RouteLocationOptions { - name?: RouteRecordName + name?: RouteRecordNameGeneric params?: RouteParamsRawGeneric /** * A relative path to the current location. This property should be removed diff --git a/packages/router/src/typed-routes/route-records.ts b/packages/router/src/typed-routes/route-records.ts index 14509864..d7d9d202 100644 --- a/packages/router/src/typed-routes/route-records.ts +++ b/packages/router/src/typed-routes/route-records.ts @@ -3,7 +3,7 @@ import type { RouteLocationNormalized, RouteLocationRaw, } from './route-location' -import type { RouteMap } from './route-map' +import type { RouteMap, RouteMapGeneric } from './route-map' /** * @internal @@ -12,12 +12,19 @@ export type RouteRecordRedirectOption = | RouteLocationRaw | ((to: RouteLocation) => RouteLocationRaw) +/** + * Generic version of {@link RouteRecordName}. + */ +export type RouteRecordNameGeneric = string | symbol | undefined + /** * Possible values for a route record **after normalization** * - * NOTE: since `RouteRecordName` is a type, it evaluates too early and it's always be a generic version. If you need a typed version of all of the names of routes, use {@link RouteMap | `keyof RouteMap`} + * NOTE: since `RouteRecordName` is a type, it evaluates too early and it's often the generic version {@link RouteRecordNameGeneric}. If you need a typed version of all of the names of routes, use {@link RouteMap | `keyof RouteMap`} */ -export type RouteRecordName = string | symbol | undefined +export type RouteRecordName = RouteMapGeneric extends RouteMap + ? RouteRecordNameGeneric + : keyof RouteMap /** * @internal diff --git a/packages/router/src/types/index.ts b/packages/router/src/types/index.ts index b9383b16..e3069740 100644 --- a/packages/router/src/types/index.ts +++ b/packages/router/src/types/index.ts @@ -8,7 +8,7 @@ import type { RouteLocation, RouteRecordRedirectOption, _RouteRecordProps, - RouteRecordName, + RouteRecordNameGeneric, } from '../typed-routes' import type { _Awaitable } from './utils' @@ -67,7 +67,7 @@ export interface MatcherLocationAsPath { * @internal */ export interface MatcherLocationAsName { - name: RouteRecordName + name: RouteRecordNameGeneric // to allow checking location.path == null /** * Ignored path property since we are dealing with a relative location. Only `undefined` is allowed. @@ -92,7 +92,7 @@ export interface MatcherLocationAsRelative { * @internal */ export interface LocationAsRelativeRaw { - name?: RouteRecordName + name?: RouteRecordNameGeneric // to allow checking location.path == null /** * Ignored path property since we are dealing with a relative location. Only `undefined` is allowed. @@ -215,7 +215,7 @@ export interface _RouteRecordBase extends PathParserOptions { /** * Name for the route record. Must be unique. */ - name?: RouteRecordName + name?: RouteRecordNameGeneric /** * Before Enter guard specific to this record. Note `beforeEnter` has no @@ -376,7 +376,7 @@ export interface MatcherLocation { /** * Name of the matched record */ - name: RouteRecordName | null | undefined + name: RouteRecordNameGeneric | null | undefined /** * Percentage encoded pathname section of the URL. diff --git a/packages/router/src/types/typeGuards.ts b/packages/router/src/types/typeGuards.ts index 7eac281d..ba30bd9b 100644 --- a/packages/router/src/types/typeGuards.ts +++ b/packages/router/src/types/typeGuards.ts @@ -1,9 +1,9 @@ -import type { RouteLocationRaw, RouteRecordName } from '../typed-routes' +import type { RouteLocationRaw, RouteRecordNameGeneric } from '../typed-routes' export function isRouteLocation(route: any): route is RouteLocationRaw { return typeof route === 'string' || (route && typeof route === 'object') } -export function isRouteName(name: any): name is RouteRecordName { +export function isRouteName(name: any): name is RouteRecordNameGeneric { return typeof name === 'string' || typeof name === 'symbol' }