@@ -13,7 +13,7 @@ import {
13
13
} from '../encoding'
14
14
import { parseURL , stringifyURL } from '../location'
15
15
import type {
16
- MatcherLocationAsName ,
16
+ MatcherLocationAsNamed ,
17
17
MatcherLocationAsRelative ,
18
18
MatcherParamsFormatted ,
19
19
} from './matcher-location'
@@ -31,7 +31,7 @@ export interface RouteResolver {
31
31
32
32
/**
33
33
* 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 `.
35
35
*/
36
36
resolve (
37
37
relativeLocation : string ,
@@ -41,7 +41,7 @@ export interface RouteResolver {
41
41
/**
42
42
* Resolves a location by its name. Any required params or query must be passed in the `options` argument.
43
43
*/
44
- resolve ( location : MatcherLocationAsName ) : NEW_LocationResolved
44
+ resolve ( location : MatcherLocationAsNamed ) : NEW_LocationResolved
45
45
46
46
/**
47
47
* Resolves a location by its path. Any required query must be passed.
@@ -67,7 +67,7 @@ export interface RouteResolver {
67
67
type MatcherResolveArgs =
68
68
| [ absoluteLocation : `/${string } `]
69
69
| [ relativeLocation : string , currentLocation : NEW_LocationResolved ]
70
- | [ location : MatcherLocationAsName ]
70
+ | [ location : MatcherLocationAsNamed ]
71
71
| [
72
72
relativeLocation : MatcherLocationAsRelative ,
73
73
currentLocation : NEW_LocationResolved
@@ -108,7 +108,11 @@ export type MatcherPathParams = Record<string, MatcherPathParamsValue>
108
108
export type MatcherQueryParamsValue = string | null | Array < string | null >
109
109
export type MatcherQueryParams = Record < string , MatcherQueryParamsValue >
110
110
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 > (
112
116
fn : ( v : string | number | null | undefined ) => R ,
113
117
params : MatcherPathParams | LocationQuery | undefined
114
118
) : Record < string , R | R [ ] > {
@@ -195,7 +199,7 @@ function transformObject<T>(
195
199
}
196
200
197
201
export const NO_MATCH_LOCATION = {
198
- name : Symbol ( 'no-match' ) ,
202
+ name : __DEV__ ? Symbol ( 'no-match' ) : Symbol ( ) ,
199
203
params : { } ,
200
204
matched : [ ] ,
201
205
} satisfies Omit < NEW_LocationResolved , 'path' | 'hash' | 'query' | 'fullPath' >
@@ -215,8 +219,9 @@ export function createCompiledMatcher(): RouteResolver {
215
219
216
220
function resolve ( ...args : MatcherResolveArgs ) : NEW_LocationResolved {
217
221
const [ location , currentLocation ] = args
222
+
223
+ // string location, e.g. '/foo', '../bar', 'baz', '?page=1'
218
224
if ( typeof location === 'string' ) {
219
- // string location, e.g. '/foo', '../bar', 'baz'
220
225
const url = parseURL ( parseQuery , location , currentLocation ?. path )
221
226
222
227
let matcher : MatcherPattern | undefined
@@ -257,14 +262,30 @@ export function createCompiledMatcher(): RouteResolver {
257
262
}
258
263
} else {
259
264
// 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
260
280
const name = location . name ?? currentLocation ! . name
261
281
const matcher = matchers . get ( name )
262
282
if ( ! matcher ) {
263
283
throw new Error ( `Matcher "${ String ( location . name ) } " not found` )
264
284
}
265
285
266
286
// 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
268
289
const mixedUnencodedParams = matcher . matchParams ( params )
269
290
270
291
if ( ! mixedUnencodedParams ) {
0 commit comments