@@ -42,15 +42,25 @@ function isMatchable(record: RouteRecordRaw): boolean {
42
42
)
43
43
}
44
44
45
+ function joinPaths ( a : string | undefined , b : string ) {
46
+ if ( a ?. endsWith ( '/' ) ) {
47
+ return a + b
48
+ }
49
+ return a + '/' + b
50
+ }
51
+
45
52
function compileRouteRecord (
46
53
record : RouteRecordRaw ,
47
54
parentRecord ?: RouteRecordRaw
48
55
) : NEW_MatcherRecordRaw {
49
56
// we adapt the path to ensure they are absolute
50
57
// TODO: aliases? they could be handled directly in the path matcher
58
+ if ( ! parentRecord && ! record . path . startsWith ( '/' ) ) {
59
+ throw new Error ( `Record without parent must have an absolute path` )
60
+ }
51
61
const path = record . path . startsWith ( '/' )
52
62
? record . path
53
- : ( parentRecord ?. path || '' ) + record . path
63
+ : joinPaths ( parentRecord ?. path , record . path )
54
64
record . path = path
55
65
const parser = tokensToParser (
56
66
tokenizePath ( record . path ) ,
@@ -62,10 +72,12 @@ function compileRouteRecord(
62
72
return {
63
73
group : ! isMatchable ( record ) ,
64
74
name : record . name ,
75
+ score : parser . score ,
65
76
66
77
path : {
67
78
match ( value ) {
68
79
const params = parser . parse ( value )
80
+ // console.log('🌟', parser.re, value, params)
69
81
if ( params ) {
70
82
return params
71
83
}
@@ -181,20 +193,21 @@ describe('RouterMatcher.resolve', () => {
181
193
: matcher . resolve (
182
194
// FIXME: is this a ts bug?
183
195
// @ts -expect-error
184
- typeof fromLocation === 'string'
185
- ? { path : fromLocation }
186
- : fromLocation
196
+ fromLocation
187
197
)
188
198
199
+ // console.log(matcher.getMatchers())
189
200
// console.log({ toLocation, resolved, expectedLocation, resolvedFrom })
190
201
191
202
const result = matcher . resolve (
192
203
// FIXME: should work now
193
204
// @ts -expect-error
194
- typeof toLocation === 'string' ? { path : toLocation } : toLocation ,
205
+ toLocation ,
195
206
resolvedFrom === START_LOCATION ? undefined : resolvedFrom
196
207
)
197
208
209
+ // console.log(result)
210
+
198
211
if (
199
212
expectedLocation . name === undefined ||
200
213
expectedLocation . name !== NO_MATCH_LOCATION . name
@@ -479,7 +492,7 @@ describe('RouterMatcher.resolve', () => {
479
492
// TODO: not sure where this warning should appear now
480
493
it . todo ( 'warns if a path isn not absolute' , ( ) => {
481
494
const matcher = createCompiledMatcher ( [
482
- { path : new MatcherPatternPathStatic ( '/' ) } ,
495
+ { path : new MatcherPatternPathStatic ( '/' ) , score : [ [ 80 ] ] } ,
483
496
] )
484
497
matcher . resolve ( { path : 'two' } , matcher . resolve ( { path : '/' } ) )
485
498
expect ( 'received "two"' ) . toHaveBeenWarned ( )
@@ -1169,34 +1182,42 @@ describe('RouterMatcher.resolve', () => {
1169
1182
} )
1170
1183
} )
1171
1184
1172
- describe . skip ( 'children' , ( ) => {
1173
- const ChildA = { path : 'a' , name : 'child-a' , components }
1174
- const ChildB = { path : 'b' , name : 'child-b' , components }
1175
- const ChildC = { path : 'c' , name : 'child-c' , components }
1176
- const ChildD = { path : '/absolute' , name : 'absolute' , components }
1177
- const ChildWithParam = { path : ':p' , name : 'child-params' , components }
1178
- const NestedChildWithParam = {
1185
+ describe ( 'children' , ( ) => {
1186
+ const ChildA : RouteRecordRaw = { path : 'a' , name : 'child-a' , components }
1187
+ const ChildB : RouteRecordRaw = { path : 'b' , name : 'child-b' , components }
1188
+ const ChildC : RouteRecordRaw = { path : 'c' , name : 'child-c' , components }
1189
+ const ChildD : RouteRecordRaw = {
1190
+ path : '/absolute' ,
1191
+ name : 'absolute' ,
1192
+ components,
1193
+ }
1194
+ const ChildWithParam : RouteRecordRaw = {
1195
+ path : ':p' ,
1196
+ name : 'child-params' ,
1197
+ components,
1198
+ }
1199
+ const NestedChildWithParam : RouteRecordRaw = {
1179
1200
...ChildWithParam ,
1180
1201
name : 'nested-child-params' ,
1181
1202
}
1182
- const NestedChildA = { ...ChildA , name : 'nested-child-a' }
1183
- const NestedChildB = { ...ChildB , name : 'nested-child-b' }
1184
- const NestedChildC = { ...ChildC , name : 'nested-child-c' }
1185
- const Nested = {
1203
+ const NestedChildA : RouteRecordRaw = { ...ChildA , name : 'nested-child-a' }
1204
+ const NestedChildB : RouteRecordRaw = { ...ChildB , name : 'nested-child-b' }
1205
+ const NestedChildC : RouteRecordRaw = { ...ChildC , name : 'nested-child-c' }
1206
+ const Nested : RouteRecordRaw = {
1186
1207
path : 'nested' ,
1187
1208
name : 'nested' ,
1188
1209
components,
1189
1210
children : [ NestedChildA , NestedChildB , NestedChildC ] ,
1190
1211
}
1191
- const NestedWithParam = {
1212
+ const NestedWithParam : RouteRecordRaw = {
1192
1213
path : 'nested/:n' ,
1193
1214
name : 'nested' ,
1194
1215
components,
1195
1216
children : [ NestedChildWithParam ] ,
1196
1217
}
1197
1218
1198
1219
it ( 'resolves children' , ( ) => {
1199
- const Foo = {
1220
+ const Foo : RouteRecordRaw = {
1200
1221
path : '/foo' ,
1201
1222
name : 'Foo' ,
1202
1223
components,
@@ -1216,8 +1237,8 @@ describe('RouterMatcher.resolve', () => {
1216
1237
} )
1217
1238
1218
1239
it ( 'resolves children with empty paths' , ( ) => {
1219
- const Nested = { path : '' , name : 'nested' , components }
1220
- const Foo = {
1240
+ const Nested : RouteRecordRaw = { path : '' , name : 'nested' , components }
1241
+ const Foo : RouteRecordRaw = {
1221
1242
path : '/foo' ,
1222
1243
name : 'Foo' ,
1223
1244
components,
0 commit comments