@@ -145,12 +145,16 @@ function* lexer(str: string): Generator<LexToken, LexToken> {
145
145
}
146
146
147
147
if ( pos ) {
148
- throw new TypeError ( `Unterminated quote at ${ pos } : ${ DEBUG_URL } ` ) ;
148
+ throw new TypeError (
149
+ `'${ str } ': Unterminated quote at ${ pos } : ${ DEBUG_URL } ` ,
150
+ ) ;
149
151
}
150
152
}
151
153
152
154
if ( ! value ) {
153
- throw new TypeError ( `Missing parameter name at ${ i } : ${ DEBUG_URL } ` ) ;
155
+ throw new TypeError (
156
+ `'${ str } ': Missing parameter name at ${ i } : ${ DEBUG_URL } ` ,
157
+ ) ;
154
158
}
155
159
156
160
return value ;
@@ -198,12 +202,12 @@ class Iter {
198
202
return token . value ;
199
203
}
200
204
201
- consume ( type : TokenType ) : string {
205
+ consume ( type : TokenType , pathString : string ) : string {
202
206
const value = this . tryConsume ( type ) ;
203
207
if ( value !== undefined ) return value ;
204
208
const { type : nextType , index } = this . peek ( ) ;
205
209
throw new TypeError (
206
- `Unexpected ${ nextType } at ${ index } , expected ${ type } : ${ DEBUG_URL } ` ,
210
+ `' ${ pathString } ': Unexpected ${ nextType } at ${ index } , expected ${ type } : ${ DEBUG_URL } ` ,
207
211
) ;
208
212
}
209
213
@@ -312,7 +316,7 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
312
316
continue ;
313
317
}
314
318
315
- it . consume ( endType ) ;
319
+ it . consume ( endType , str ) ;
316
320
return tokens ;
317
321
}
318
322
}
@@ -331,14 +335,16 @@ export function compile<P extends ParamData = ParamData>(
331
335
const { encode = encodeURIComponent , delimiter = DEFAULT_DELIMITER } =
332
336
options ;
333
337
const data = path instanceof TokenData ? path : parse ( path , options ) ;
334
- const fn = tokensToFunction ( data . tokens , delimiter , encode ) ;
338
+ const fn = tokensToFunction ( data . tokens , delimiter , encode , path ) ;
335
339
336
- return function path ( params : P = { } as P ) {
337
- const [ path , ...missing ] = fn ( params ) ;
340
+ return function pathFn ( params : P = { } as P ) {
341
+ const [ returnPath , ...missing ] = fn ( params ) ;
338
342
if ( missing . length ) {
339
- throw new TypeError ( `Missing parameters: ${ missing . join ( ", " ) } ` ) ;
343
+ throw new TypeError (
344
+ `'${ stringify ( path ) } ': Missing parameters: ${ missing . join ( ", " ) } ` ,
345
+ ) ;
340
346
}
341
- return path ;
347
+ return returnPath ;
342
348
} ;
343
349
}
344
350
@@ -349,9 +355,10 @@ function tokensToFunction(
349
355
tokens : Token [ ] ,
350
356
delimiter : string ,
351
357
encode : Encode | false ,
358
+ path : Path ,
352
359
) {
353
360
const encoders = tokens . map ( ( token ) =>
354
- tokenToFunction ( token , delimiter , encode ) ,
361
+ tokenToFunction ( token , delimiter , encode , path ) ,
355
362
) ;
356
363
357
364
return ( data : ParamData ) => {
@@ -374,11 +381,12 @@ function tokenToFunction(
374
381
token : Token ,
375
382
delimiter : string ,
376
383
encode : Encode | false ,
384
+ path : Path ,
377
385
) : ( data : ParamData ) => string [ ] {
378
386
if ( token . type === "text" ) return ( ) => [ token . value ] ;
379
387
380
388
if ( token . type === "group" ) {
381
- const fn = tokensToFunction ( token . tokens , delimiter , encode ) ;
389
+ const fn = tokensToFunction ( token . tokens , delimiter , encode , path ) ;
382
390
383
391
return ( data ) => {
384
392
const [ value , ...missing ] = fn ( data ) ;
@@ -395,15 +403,17 @@ function tokenToFunction(
395
403
if ( value == null ) return [ "" , token . name ] ;
396
404
397
405
if ( ! Array . isArray ( value ) || value . length === 0 ) {
398
- throw new TypeError ( `Expected "${ token . name } " to be a non-empty array` ) ;
406
+ throw new TypeError (
407
+ `'${ stringify ( path ) } ': Expected "${ token . name } " to be a non-empty array` ,
408
+ ) ;
399
409
}
400
410
401
411
return [
402
412
value
403
413
. map ( ( value , index ) => {
404
414
if ( typeof value !== "string" ) {
405
415
throw new TypeError (
406
- `Expected "${ token . name } /${ index } " to be a string` ,
416
+ `' ${ stringify ( path ) } ': Expected "${ token . name } /${ index } " to be a string` ,
407
417
) ;
408
418
}
409
419
@@ -419,7 +429,9 @@ function tokenToFunction(
419
429
if ( value == null ) return [ "" , token . name ] ;
420
430
421
431
if ( typeof value !== "string" ) {
422
- throw new TypeError ( `Expected "${ token . name } " to be a string` ) ;
432
+ throw new TypeError (
433
+ `'${ stringify ( path ) } ': Expected "${ token . name } " to be a string` ,
434
+ ) ;
423
435
}
424
436
425
437
return [ encodeValue ( value ) ] ;
@@ -611,7 +623,10 @@ function negate(delimiter: string, backtrack: string) {
611
623
/**
612
624
* Stringify token data into a path string.
613
625
*/
614
- export function stringify ( data : TokenData ) {
626
+ export function stringify ( data : Path ) {
627
+ if ( typeof data === "string" ) {
628
+ return data ;
629
+ }
615
630
return data . tokens
616
631
. map ( function stringifyToken ( token , index , tokens ) : string {
617
632
if ( token . type === "text" ) return escapeText ( token . value ) ;
0 commit comments