@@ -13,7 +13,7 @@ import {
1313} from '../encoding'
1414import { parseURL , stringifyURL } from '../location'
1515import type {
16- MatcherLocationAsName ,
16+ MatcherLocationAsNamed ,
1717 MatcherLocationAsRelative ,
1818 MatcherParamsFormatted ,
1919} from './matcher-location'
@@ -31,7 +31,7 @@ export interface RouteResolver {
3131
3232 /**
3333 * Resolves a string location relative to another location. A relative location can be `./same-folder`,
34- * `../parent-folder`, or even `same-folder `.
34+ * `../parent-folder`, `same-folder`, or even `?page=2 `.
3535 */
3636 resolve (
3737 relativeLocation : string ,
@@ -41,7 +41,7 @@ export interface RouteResolver {
4141 /**
4242 * Resolves a location by its name. Any required params or query must be passed in the `options` argument.
4343 */
44- resolve ( location : MatcherLocationAsName ) : NEW_LocationResolved
44+ resolve ( location : MatcherLocationAsNamed ) : NEW_LocationResolved
4545
4646 /**
4747 * Resolves a location by its path. Any required query must be passed.
@@ -67,7 +67,7 @@ export interface RouteResolver {
6767type MatcherResolveArgs =
6868 | [ absoluteLocation : `/${string } `]
6969 | [ relativeLocation : string , currentLocation : NEW_LocationResolved ]
70- | [ location : MatcherLocationAsName ]
70+ | [ location : MatcherLocationAsNamed ]
7171 | [
7272 relativeLocation : MatcherLocationAsRelative ,
7373 currentLocation : NEW_LocationResolved
@@ -108,7 +108,11 @@ export type MatcherPathParams = Record<string, MatcherPathParamsValue>
108108export type MatcherQueryParamsValue = string | null | Array < string | null >
109109export type MatcherQueryParams = Record < string , MatcherQueryParamsValue >
110110
111- export function applyToParams < R > (
111+ /**
112+ * Apply a function to all properties in an object. It's used to encode/decode params and queries.
113+ * @internal
114+ */
115+ export function applyFnToObject < R > (
112116 fn : ( v : string | number | null | undefined ) => R ,
113117 params : MatcherPathParams | LocationQuery | undefined
114118) : Record < string , R | R [ ] > {
@@ -195,7 +199,7 @@ function transformObject<T>(
195199}
196200
197201export const NO_MATCH_LOCATION = {
198- name : Symbol ( 'no-match' ) ,
202+ name : __DEV__ ? Symbol ( 'no-match' ) : Symbol ( ) ,
199203 params : { } ,
200204 matched : [ ] ,
201205} satisfies Omit < NEW_LocationResolved , 'path' | 'hash' | 'query' | 'fullPath' >
@@ -215,8 +219,9 @@ export function createCompiledMatcher(): RouteResolver {
215219
216220 function resolve ( ...args : MatcherResolveArgs ) : NEW_LocationResolved {
217221 const [ location , currentLocation ] = args
222+
223+ // string location, e.g. '/foo', '../bar', 'baz', '?page=1'
218224 if ( typeof location === 'string' ) {
219- // string location, e.g. '/foo', '../bar', 'baz'
220225 const url = parseURL ( parseQuery , location , currentLocation ?. path )
221226
222227 let matcher : MatcherPattern | undefined
@@ -257,14 +262,30 @@ export function createCompiledMatcher(): RouteResolver {
257262 }
258263 } else {
259264 // relative location or by name
265+ if ( __DEV__ && location . name == null && currentLocation == null ) {
266+ console . warn (
267+ `Cannot resolve an unnamed relative location without a current location. This will throw in production.` ,
268+ location
269+ )
270+ return {
271+ ...NO_MATCH_LOCATION ,
272+ fullPath : '/' ,
273+ path : '/' ,
274+ query : { } ,
275+ hash : '' ,
276+ }
277+ }
278+
279+ // either one of them must be defined and is catched by the dev only warn above
260280 const name = location . name ?? currentLocation ! . name
261281 const matcher = matchers . get ( name )
262282 if ( ! matcher ) {
263283 throw new Error ( `Matcher "${ String ( location . name ) } " not found` )
264284 }
265285
266286 // unencoded params in a formatted form that the user came up with
267- const params = location . params ?? currentLocation ! . params
287+ const params : MatcherParamsFormatted =
288+ location . params ?? currentLocation ! . params
268289 const mixedUnencodedParams = matcher . matchParams ( params )
269290
270291 if ( ! mixedUnencodedParams ) {
0 commit comments