Skip to content

Commit 8f7d4d2

Browse files
committed
feat: avoid invalid dts with invalid param
Close #706
1 parent bc5a060 commit 8f7d4d2

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/core/tree.spec.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,4 +987,64 @@ describe('Tree', () => {
987987
}) // query param
988988
})
989989
})
990+
991+
describe('Empty parameter names', () => {
992+
it('assigns default name "pathMatch" to empty parameter names', () => {
993+
const tree = new PrefixTree(RESOLVED_OPTIONS)
994+
let node = tree.insertParsedPath('/:()bar', 'test.vue')
995+
996+
expect(
997+
'Invalid parameter in path "/:()bar": parameter name cannot be empty'
998+
).toHaveBeenWarned()
999+
// The empty param gets assigned the default name "pathMatch"
1000+
expect(node.value.isParam()).toBe(true)
1001+
if (node.value.isParam()) {
1002+
expect(node.value.pathParams).toHaveLength(1)
1003+
expect(node.value.pathParams[0]).toMatchObject({
1004+
paramName: 'pathMatch',
1005+
})
1006+
}
1007+
1008+
// Empty param at the start - gets default name
1009+
node = tree.insertParsedPath('/:()', 'test1.vue')
1010+
expect(
1011+
'Invalid parameter in path "/:()": parameter name cannot be empty'
1012+
).toHaveBeenWarned()
1013+
expect(node.value.isParam()).toBe(true)
1014+
if (node.value.isParam()) {
1015+
expect(node.value.pathParams).toHaveLength(1)
1016+
expect(node.value.pathParams[0]).toMatchObject({
1017+
paramName: 'pathMatch',
1018+
})
1019+
}
1020+
1021+
// Empty param with prefix - gets default name
1022+
node = tree.insertParsedPath('/foo/:()', 'test2.vue')
1023+
expect(
1024+
'Invalid parameter in path "/foo/:()": parameter name cannot be empty'
1025+
).toHaveBeenWarned()
1026+
expect(node.value.isParam()).toBe(true)
1027+
if (node.value.isParam()) {
1028+
expect(node.value.pathParams).toHaveLength(1)
1029+
expect(node.value.pathParams[0]).toMatchObject({
1030+
paramName: 'pathMatch',
1031+
})
1032+
}
1033+
1034+
// Mixed: valid param, empty param, valid param - empty gets default name
1035+
node = tree.insertParsedPath('/:a/:()/:b', 'test3.vue')
1036+
expect(
1037+
'Invalid parameter in path "/:a/:()/:b": parameter name cannot be empty'
1038+
).toHaveBeenWarned()
1039+
expect(node.value.isParam()).toBe(true)
1040+
if (node.value.isParam()) {
1041+
expect(node.value.pathParams).toHaveLength(3)
1042+
expect(node.value.pathParams[0]).toMatchObject({ paramName: 'a' })
1043+
expect(node.value.pathParams[1]).toMatchObject({
1044+
paramName: 'pathMatch',
1045+
})
1046+
expect(node.value.pathParams[2]).toMatchObject({ paramName: 'b' })
1047+
}
1048+
})
1049+
})
9901050
})

src/core/treeNodeValue.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,13 @@ function parseRawPathSegment(
735735
state === ParseRawPathSegmentState.regexp ||
736736
state === ParseRawPathSegmentState.modifier
737737
) {
738+
// Check if the parameter name is empty and assign a default name
739+
if (!currentTreeRouteParam.paramName) {
740+
warn(
741+
`Invalid parameter in path "${segment}": parameter name cannot be empty. Using default name "pathMatch" for ':()'.`
742+
)
743+
currentTreeRouteParam.paramName = 'pathMatch'
744+
}
738745
// we consume the current param
739746
subSegments.push(currentTreeRouteParam)
740747
params.push(currentTreeRouteParam)

0 commit comments

Comments
 (0)