Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/codegen/generateDTS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ ${normalizeLines(routeNamedMap)}

/**
* Route file to route info map by unplugin-vue-router.
* Used by the volar plugin to automatically type useRoute()
* Used by the \`sfc-typed-router\` Volar plugin to automatically type \`useRoute()\`.
*
* Each key is a file path relative to the project root with 2 properties:
* - routes: union of route names of the possible routes when in this page (passed to useRoute<...>())
Expand All @@ -61,7 +61,7 @@ ${normalizeLines(routeFileInfoMap)}

/**
* Get a union of possible route names in a certain route component file.
* Used by the volar plugin to automatically type useRoute()
* Used by the \`sfc-typed-router\` Volar plugin to automatically type \`useRoute()\`.
*
* @internal
*/
Expand Down
61 changes: 61 additions & 0 deletions src/codegen/generateRouteFileInfoMap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,65 @@ describe('generateRouteFileInfoMap', () => {
}"
`)
})

it('does not contain routes without components', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@posva This test already passed before my change in generateRouteFileInfoMap.ts

const tree = new PrefixTree(DEFAULT_OPTIONS)
tree.insert('optional/[[id]]', 'optional/[[id]].vue')
tree.insert(
'optional-repeatable/[[id]]+',
'optional-repeatable/[[id]]+.vue'
)
tree.insert('repeatable/[id]+', 'repeatable/[id]+.vue')

expect(formatExports(generateRouteFileInfoMap(tree, { root: '' })))
.toMatchInlineSnapshot(`
"export interface _RouteFileInfoMap {
'optional/[[id]].vue': {
routes: '/optional/[[id]]'
views: never
}
'optional-repeatable/[[id]]+.vue': {
routes: '/optional-repeatable/[[id]]+'
views: never
}
'repeatable/[id]+.vue': {
routes: '/repeatable/[id]+'
views: never
}
}"
`)
})

it('does not contain nested routes without components', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
tree.insert('parent', 'parent.vue')
tree.insert('parent/optional/[[id]]', 'parent/optional/[[id]].vue')
tree.insert(
'parent/optional-repeatable/[[id]]+',
'parent/optional-repeatable/[[id]]+.vue'
)
tree.insert('parent/repeatable/[id]+', 'parent/repeatable/[id]+.vue')

expect(formatExports(generateRouteFileInfoMap(tree, { root: '' })))
.toMatchInlineSnapshot(`
"export interface _RouteFileInfoMap {
'parent.vue': {
routes: '/parent' | '/parent/optional/[[id]]' | '/parent/optional-repeatable/[[id]]+' | '/parent/repeatable/[id]+'
views: 'default'
}
'parent/optional/[[id]].vue': {
routes: '/parent/optional/[[id]]'
views: never
}
'parent/optional-repeatable/[[id]]+.vue': {
routes: '/parent/optional-repeatable/[[id]]+'
views: never
}
'parent/repeatable/[id]+.vue': {
routes: '/parent/repeatable/[id]+'
views: never
}
}"
`)
})
})
23 changes: 16 additions & 7 deletions src/codegen/generateRouteFileInfoMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,28 @@ function generateRouteFileInfoLines(
routeNames: string[]
childrenNamedViews: string[] | null
}> {
const children = node.children.size > 0 ? node.getChildrenDeepSorted() : null
const deepChildren =
node.children.size > 0 ? node.getChildrenDeepSorted() : null

const childrenNamedViews = children
const deepChildrenNamedViews = deepChildren
? Array.from(
new Set(
children.flatMap((child) => Array.from(child.value.components.keys()))
deepChildren.flatMap((child) =>
Array.from(child.value.components.keys())
)
)
)
: null

const routeNames = [node, ...node.getChildrenDeepSorted()]
// an unnamed route cannot be accessed in types
.filter((node): node is TreeNode & { name: string } => !!node.name)
const routeNames: Array<Extract<TreeNode['name'], string>> = [
node,
...(deepChildren ?? []),
]
// unnamed routes and routes that don't correspond to certain components cannot be accessed in types
.filter(
(node): node is TreeNode & { name: string } =>
node.value.components.size > 0 && !!node.name
)
.map((node) => node.name)

// Most of the time we only have one view, but with named views we can have multiple.
Expand All @@ -86,7 +95,7 @@ function generateRouteFileInfoLines(
: Array.from(node.value.components.values()).map((file) => ({
key: relative(rootDir, file).replaceAll('\\', '/'),
routeNames,
childrenNamedViews,
childrenNamedViews: deepChildrenNamedViews,
}))

const childrenRouteInfo = node
Expand Down
Loading