Skip to content

Commit

Permalink
fix: handle empty regexp in raw routes
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed May 2, 2023
1 parent 2002aa9 commit 9bea452
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 15 deletions.
104 changes: 90 additions & 14 deletions src/core/extendRoutes.spec.ts
Expand Up @@ -54,6 +54,46 @@ describe('EditableTreeNode', () => {
expect(node.children.at(0)?.fullPath).toBe('/foo/bar/nested')
})

it('adds params', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
const editable = new EditableTreeNode(tree)

editable.insert(':id', 'file.vue')
expect(tree.children.size).toBe(1)
const child = tree.children.get(':id')!
expect(child.fullPath).toBe('/:id')
expect(child.path).toBe('/:id')
expect(child.params).toEqual([
{
paramName: 'id',
modifier: '',
optional: false,
repeatable: false,
isSplat: false,
},
])
})

it('adds params with modifiers', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
const editable = new EditableTreeNode(tree)

editable.insert(':id+', 'file.vue')
expect(tree.children.size).toBe(1)
const child = tree.children.get(':id+')!
expect(child.fullPath).toBe('/:id+')
expect(child.path).toBe('/:id+')
expect(child.params).toEqual([
{
paramName: 'id',
modifier: '+',
optional: false,
repeatable: true,
isSplat: false,
},
])
})

it('can have multiple params', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
const editable = new EditableTreeNode(tree)
Expand Down Expand Up @@ -115,16 +155,15 @@ describe('EditableTreeNode', () => {
])
})

it('adds params', () => {
it('adds params with custom regex', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
const editable = new EditableTreeNode(tree)

editable.insert(':id', 'file.vue')
expect(tree.children.size).toBe(1)
const child = tree.children.get(':id')!
expect(child.fullPath).toBe('/:id')
expect(child.path).toBe('/:id')
expect(child.params).toEqual([
editable.insert(':id(\\d+)', 'file.vue')
const node = tree.children.get(':id(\\d+)')!
expect(node.fullPath).toBe('/:id(\\d+)')
expect(node.path).toBe('/:id(\\d+)')
expect(node.params).toEqual([
{
paramName: 'id',
modifier: '',
Expand All @@ -135,16 +174,53 @@ describe('EditableTreeNode', () => {
])
})

it('add params with modifiers', () => {
it('adds a param with empty regex', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
const editable = new EditableTreeNode(tree)

editable.insert(':id+', 'file.vue')
expect(tree.children.size).toBe(1)
const child = tree.children.get(':id+')!
expect(child.fullPath).toBe('/:id+')
expect(child.path).toBe('/:id+')
expect(child.params).toEqual([
editable.insert(':id()', 'file.vue')
const node = tree.children.get(':id()')!
expect(node.fullPath).toBe('/:id()')
expect(node.path).toBe('/:id()')
expect(node.params).toEqual([
{
paramName: 'id',
modifier: '',
optional: false,
repeatable: false,
isSplat: false,
},
])
})

it('adds a param with a modifier and custom regex', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
const editable = new EditableTreeNode(tree)

editable.insert(':id(\\d+)+', 'file.vue')
const node = tree.children.get(':id(\\d+)+')!
expect(node.fullPath).toBe('/:id(\\d+)+')
expect(node.path).toBe('/:id(\\d+)+')
expect(node.params).toEqual([
{
paramName: 'id',
modifier: '+',
optional: false,
repeatable: true,
isSplat: false,
},
])
})

it('adds a param with a modifier and empty regex', () => {
const tree = createPrefixTree(DEFAULT_OPTIONS)
const editable = new EditableTreeNode(tree)

editable.insert(':id()+', 'file.vue')
const node = tree.children.get(':id()+')!
expect(node.fullPath).toBe('/:id()+')
expect(node.path).toBe('/:id()+')
expect(node.params).toEqual([
{
paramName: 'id',
modifier: '+',
Expand Down
6 changes: 5 additions & 1 deletion src/core/treeNodeValue.ts
Expand Up @@ -489,7 +489,11 @@ function parseRawPathSegment(
throw new Error(`Invalid segment: "${segment}"`)
}

if (buffer) {
if (
buffer ||
// an empty finished regexp enters this state but must also be consumed
state === ParseRawPathSegmentState.modifier
) {
consumeBuffer()
}

Expand Down

0 comments on commit 9bea452

Please sign in to comment.