Skip to content

Commit d754601

Browse files
committed
feat: allow removing a route from types
1 parent 2a89837 commit d754601

File tree

9 files changed

+29
-15
lines changed

9 files changed

+29
-15
lines changed

playground/src/pages/articles.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ if (routeHasParam(route, 'id')) {
3333
route.name satisfies '/articles/[id]' | '/articles/[id]+'
3434
route.params.id satisfies string | [string, ...string[]]
3535
}
36+
37+
definePage({
38+
// remove the name to avoid the page appearing in types
39+
name: false,
40+
})
3641
</script>
3742

3843
<template>

playground/typed-router.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ declare module 'vue-router/auto-routes' {
2929
'/@[profileId]': RouteRecordInfo<'/@[profileId]', '/@:profileId', { profileId: ParamValue<true> }, { profileId: ParamValue<false> }>,
3030
'/about': RouteRecordInfo<'/about', '/about', Record<never, never>, Record<never, never>>,
3131
'/about.extra.nested': RouteRecordInfo<'/about.extra.nested', '/about/extra/nested', Record<never, never>, Record<never, never>>,
32-
'/articles': RouteRecordInfo<'/articles', '/articles', Record<never, never>, Record<never, never>, '/articles/' | '/articles/[id]' | '/articles/[id]+'>,
3332
'/articles/': RouteRecordInfo<'/articles/', '/articles', Record<never, never>, Record<never, never>>,
3433
'/articles/[id]': RouteRecordInfo<'/articles/[id]', '/articles/:id', { id: ParamValue<true> }, { id: ParamValue<false> }>,
3534
'/articles/[id]+': RouteRecordInfo<'/articles/[id]+', '/articles/:id+', { id: ParamValueOneOrMore<true> }, { id: ParamValueOneOrMore<false> }>,
@@ -145,7 +144,7 @@ declare module 'vue-router/auto-routes' {
145144
views: never
146145
}
147146
'src/pages/articles.vue': {
148-
routes: '/articles' | '/articles/' | '/articles/[id]' | '/articles/[id]+'
147+
routes: '/articles/' | '/articles/[id]' | '/articles/[id]+'
149148
views: 'default'
150149
}
151150
'src/pages/articles/index.vue': {

src/codegen/generateRouteFileInfoMap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function generateRouteFileInfoLines(
7676

7777
const routeNames = [node, ...node.getChildrenDeepSorted()]
7878
// an unnamed route cannot be accessed in types
79-
.filter((node) => node.name)
79+
.filter((node): node is TreeNode & { name: string } => !!node.name)
8080
.map((node) => node.name)
8181

8282
// Most of the time we only have one view, but with named views we can have multiple.

src/core/customBlock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export interface CustomRouteBlock
2323
'components' | 'component' | 'children' | 'beforeEnter' | 'name'
2424
>
2525
> {
26-
name?: string | undefined
26+
name?: string | undefined | false
2727
}
2828

2929
function parseCustomBlock(

src/core/definePage.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export function definePageTransform({
189189
export function extractDefinePageNameAndPath(
190190
sfcCode: string,
191191
id: string
192-
): { name?: string; path?: string } | null | undefined {
192+
): { name?: string | false; path?: string } | null | undefined {
193193
if (!sfcCode.includes(MACRO_DEFINE_PAGE)) return
194194

195195
const { ast, definePageNodes } = getCodeAst(sfcCode, id)
@@ -221,10 +221,14 @@ export function extractDefinePageNameAndPath(
221221
for (const prop of routeRecord.properties) {
222222
if (prop.type === 'ObjectProperty' && prop.key.type === 'Identifier') {
223223
if (prop.key.name === 'name') {
224-
if (prop.value.type !== 'StringLiteral') {
225-
warn(`route name must be a string literal. Found in "${id}".`)
224+
if (
225+
prop.value.type !== 'StringLiteral' &&
226+
(prop.value.type !== 'BooleanLiteral' || prop.value.value !== false)
227+
) {
228+
warn(`route name must be a string literal or false. Found in "${id}".`)
226229
} else {
227-
routeInfo.name = prop.value.value
230+
// TODO: why does TS not narrow down the type?
231+
routeInfo.name = (prop.value.value as string | false)
228232
}
229233
} else if (prop.key.name === 'path') {
230234
if (prop.value.type !== 'StringLiteral') {

src/core/extendRoutes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export class EditableTreeNode {
8585
* nothing.
8686
* @see {@link isPassThrough}
8787
*/
88-
get name(): string {
88+
get name(): string | false {
8989
return this.node.name
9090
}
9191

src/core/tree.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,9 @@ export class TreeNode {
239239
const overrideName = this.value.overrides.name
240240
// allows passing a null or empty name so the route is not named
241241
// and isn't listed in the route map
242-
return overrideName !== undefined
243-
? overrideName
244-
: this.options.getRouteName(this)
242+
return overrideName === undefined
243+
? this.options.getRouteName(this)
244+
: overrideName
245245
}
246246

247247
/**

src/core/treeNodeValue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const enum TreeNodeType {
1010

1111
export interface RouteRecordOverride
1212
extends Partial<Pick<RouteRecordRaw, 'meta' | 'props' | 'alias' | 'path'>> {
13-
name?: string | undefined
13+
name?: string | undefined | false
1414
}
1515

1616
export type SubSegment = string | TreeRouteParam

src/runtime.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,11 @@ export function _mergeRouteRecord(
5151
*/
5252
export interface DefinePage
5353
extends Partial<
54-
Omit<RouteRecordRaw, 'children' | 'components' | 'component'>
55-
> {}
54+
Omit<RouteRecordRaw, 'children' | 'components' | 'component' | 'name'>
55+
> {
56+
/**
57+
* A route name. If not provided, the name will be generated based on the file path.
58+
* Can be set to `false` to remove the name from types.
59+
*/
60+
name?: string | false
61+
}

0 commit comments

Comments
 (0)