Skip to content

Commit 6bdcf9a

Browse files
authored
fix(matcher): refine param separator matching (#2898)
1 parent 36eee12 commit 6bdcf9a

2 files changed

Lines changed: 130 additions & 0 deletions

File tree

‎packages/router/__tests__/matcher/pathParser.spec.ts‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,20 @@ describe('Path parser', () => {
663663
])
664664
})
665665

666+
it('param followed by a separator and a param', () => {
667+
matchRegExp('^/([^/](?:(?!-)[^/])*)-([^/]+?)$', tokenizePath('/:a-:b'))
668+
matchRegExp(
669+
'^/([^/](?:(?!\\.json)[^/])*)\\.json([^/]+?)$',
670+
tokenizePath('/:a.json:b')
671+
)
672+
matchRegExp(
673+
'^/([^/](?:(?!\\|)[^/])*)\\|([^/]+?)$',
674+
tokenizePath('/:a|:b')
675+
)
676+
matchRegExp('^/([^/]+?)-(\\d+)$', tokenizePath('/:a-:b(\\d+)'))
677+
matchRegExp('^/([^/]+?)\\.json$', tokenizePath('/:a.json'))
678+
})
679+
666680
it('static and param+', () => {
667681
matchRegExp('^/ab/((?:\\d+)(?:/(?:\\d+))*)$', [
668682
[
@@ -934,6 +948,101 @@ describe('Path parser', () => {
934948
})
935949
})
936950

951+
it('param multiple with ambiguous separators', () => {
952+
matchParams('/:a-:b', '/x-y-y', { a: 'x', b: 'y-y' })
953+
matchParams('/:a-:b', '/x--y', { a: 'x', b: '-y' })
954+
matchParams('/:a-:b-:c', '/x--y-z', { a: 'x', b: '-y', c: 'z' })
955+
matchParams('/:a-:b-:c', '/-x-y-z', { a: '-x', b: 'y', c: 'z' })
956+
matchParams('/:a-:b-:c', '/x-y', null)
957+
matchParams('/:a-:b-:c', '/x-y/z', null)
958+
matchParams('/:a-:b-:c.json', '/a-b-c-d.json', {
959+
a: 'a',
960+
b: 'b',
961+
c: 'c-d',
962+
})
963+
matchParams('/:a-:b-:c.json', '/a-b-c.json.json', {
964+
a: 'a',
965+
b: 'b',
966+
c: 'c.json',
967+
})
968+
})
969+
970+
it('param multiple with multi-char separators', () => {
971+
matchParams('/:a--:b--:c', '/x---y--z', { a: 'x', b: '-y', c: 'z' })
972+
matchParams('/:a--:b--:c', '/x----y--z', { a: 'x', b: '--y', c: 'z' })
973+
matchParams('/:a()ab:b', '/xabab', { a: 'x', b: 'ab' })
974+
matchParams('/:a()ab:b', '/aabb', { a: 'a', b: 'b' })
975+
matchParams('/:a()aa:b', '/xaaa', { a: 'x', b: 'a' })
976+
})
977+
978+
it('param multiple with regex special separators', () => {
979+
matchParams('/:a.:b.:c', '/1.2.3.4', { a: '1', b: '2', c: '3.4' })
980+
matchParams('/:a\\+:b', '/x+y+z', { a: 'x', b: 'y+z' })
981+
matchParams('/:a$:b^:c', '/1$2$3^4^5', { a: '1', b: '2$3', c: '4^5' })
982+
matchParams('/:a[:b]:c', '/1[2[3]4]5', { a: '1', b: '2[3', c: '4]5' })
983+
})
984+
985+
it('param multiple with letter separators', () => {
986+
matchParams('/:a()X:b', '/1x2X3', { a: '1', b: '2X3' })
987+
matchParams(
988+
'/:a()X:b',
989+
'/1x2X3',
990+
{ a: '1x2', b: '3' },
991+
{ sensitive: true }
992+
)
993+
})
994+
995+
it('param multiple with optional params', () => {
996+
matchParams('/:a?-:b-:c', '/-x-y', { a: '', b: 'x', c: 'y' })
997+
matchParams('/:a?-:b-:c', '/w-x-y', { a: 'w', b: 'x', c: 'y' })
998+
matchParams('/:a-:b?-:c', '/x--y', { a: 'x', b: '', c: 'y' })
999+
})
1000+
1001+
it('param multiple without separator', () => {
1002+
matchParams('/:a:b:c', '/xyzb', { a: 'x', b: 'y', c: 'zb' })
1003+
})
1004+
1005+
it('param with custom re followed by a separator and a param', () => {
1006+
matchParams('/:a(\\d+)-:b', '/x-y', null)
1007+
matchParams('/:a(\\d+)-:b-:c', '/1-2-3', { a: '1', b: '2', c: '3' })
1008+
})
1009+
1010+
it('param repeatable followed by a separator and a param', () => {
1011+
matchParams('/:a+-:b', '/x-y/z-w', { a: ['x-y', 'z'], b: 'w' })
1012+
matchParams('/:a+-:b', '/x/y-z', { a: ['x', 'y'], b: 'z' })
1013+
})
1014+
1015+
it('param followed by a separator and a param with custom re', () => {
1016+
matchParams('/:a-:b(\\d+)', '/x-y-1', { a: 'x-y', b: '1' })
1017+
matchParams('/:a-:b(\\d+)', '/x-1-1', { a: 'x-1', b: '1' })
1018+
})
1019+
1020+
it('param followed by a static with | and a param', () => {
1021+
matchParams('/:a-|:b-|:c', '/x-|y-|z', { a: 'x', b: 'y', c: 'z' })
1022+
matchParams('/:a|:b', '/x|y|z', { a: 'x', b: 'y|z' })
1023+
matchParams('/:a-|:b', '/x-y', null)
1024+
})
1025+
1026+
it('param multiple with a backreference in a custom re', () => {
1027+
matchParams('/:a-:b-:c(\\1)', '/x-y-z-x-y', {
1028+
a: 'x-y',
1029+
b: 'z',
1030+
c: 'x-y',
1031+
})
1032+
matchParams('/:a-:b/:c(\\1)', '/x-y-z/x-y', {
1033+
a: 'x-y',
1034+
b: 'z',
1035+
c: 'x-y',
1036+
})
1037+
})
1038+
1039+
it('param multiple with a long non matching path', () => {
1040+
const parser = tokensToParser(tokenizePath('/:a-:b-:c.json'))
1041+
const start = performance.now()
1042+
expect(parser.parse('/' + '-'.repeat(5000) + 'X')).toBe(null)
1043+
expect(performance.now() - start).toBeLessThan(1000)
1044+
})
1045+
9371046
it('param optional', () => {
9381047
matchParams('/:a?', '/one', { a: 'one' })
9391048
matchParams('/:a*', '/one', { a: ['one'] })

‎packages/router/src/matcher/pathParserRanker.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ export function tokensToParser(
138138
let pattern = options.start ? '^' : ''
139139
// extracted keys
140140
const keys: PathParserParamKey[] = []
141+
// a backreference in a custom regexp can depend on where a previous param ends
142+
const hasBackreference = segments.some(segment =>
143+
segment.some(
144+
token =>
145+
token.type === TokenType.Param && /\\[1-9k]/.test(token.regexp || '')
146+
)
147+
)
141148

142149
for (const segment of segments) {
143150
// the root segment needs special treatment
@@ -186,6 +193,20 @@ export function tokensToParser(
186193
? `((?:${re})(?:/(?:${re}))*)`
187194
: `(${re})`
188195

196+
// `a` in /:a-:b ends at the first `-` after its first char
197+
const next = segment[tokenIndex + 1]
198+
const afterNext = segment[tokenIndex + 2]
199+
if (
200+
!hasBackreference &&
201+
!regexp &&
202+
!repeatable &&
203+
afterNext &&
204+
afterNext.type === TokenType.Param &&
205+
!afterNext.regexp &&
206+
next.type === TokenType.Static
207+
)
208+
subPattern = `([^/](?:(?!${next.value.replace(REGEX_CHARS_RE, '\\$&')})[^/])*)`
209+
189210
// prepend the slash if we are starting a new segment
190211
if (!tokenIndex)
191212
subPattern =

0 commit comments

Comments
 (0)