Skip to content

Commit f6836fa

Browse files
committed
fix: preserve named-view props and detect quoted props keys
1 parent 2effe09 commit f6836fa

4 files changed

Lines changed: 53 additions & 4 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { mergeRouteRecord } from './runtime'
3+
4+
describe('mergeRouteRecord', () => {
5+
it('merges props for named views across route records', () => {
6+
const record = mergeRouteRecord(
7+
{
8+
path: '/',
9+
components: {},
10+
props: { default: true, aside: { title: 'initial' } },
11+
},
12+
{ props: { aside: { title: 'updated' } } },
13+
{ props: { footer: false } }
14+
)
15+
16+
expect(record.props).toEqual({
17+
default: true,
18+
aside: { title: 'updated' },
19+
footer: false,
20+
})
21+
})
22+
23+
it('keeps named-view props when a later record has no props', () => {
24+
const record = mergeRouteRecord(
25+
{ path: '/', components: {}, props: { aside: true } },
26+
{ props: undefined, meta: { title: 'page' } }
27+
)
28+
29+
expect(record.props).toEqual({ aside: true })
30+
expect(record.meta).toEqual({ title: 'page' })
31+
})
32+
})

‎packages/router/src/experimental/runtime.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ export function mergeRouteRecord(
114114
routeRecord.alias || []
115115
)
116116

117-
// TODO: other nested properties
118-
// const props = assign({}, acc.props, routeRecord.props)
117+
const props = assign({}, acc.props, routeRecord.props)
119118

120119
assign(acc, routeRecord)
121120
acc.meta = meta
122121
acc.alias = alias
122+
acc.props = props
123123
return acc
124124
}, main)
125125
}

‎packages/router/src/unplugin/core/definePage.spec.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,23 @@ definePage({ props: true })`)
284284
).toContain(`const props = defineProps(["userId","page"])`)
285285
})
286286

287+
it.each(["'props'", '"props"'])(
288+
'declares the params with a %s key',
289+
async key => {
290+
expect(
291+
await transformScript(`defineProps()
292+
definePage({ ${key}: true })`)
293+
).toContain(`defineProps(["userId","page"])`)
294+
}
295+
)
296+
297+
it('ignores a computed props key', async () => {
298+
expect(
299+
await transformScript(`defineProps()
300+
definePage({ ['props']: true })`)
301+
).toContain('defineProps()')
302+
})
303+
287304
it('adds the params to the type argument of `defineProps()`', async () => {
288305
expect(
289306
await transformScript(`const props = withDefaults(defineProps<Props & { extra?: string }>(), {})

‎packages/router/src/unplugin/core/definePage.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,8 @@ function hasPropsTrue(definePageNode: CallExpression): boolean {
296296
prop =>
297297
prop.type === 'ObjectProperty' &&
298298
!prop.computed &&
299-
prop.key.type === 'Identifier' &&
300-
prop.key.name === 'props' &&
299+
((prop.key.type === 'Identifier' && prop.key.name === 'props') ||
300+
(prop.key.type === 'StringLiteral' && prop.key.value === 'props')) &&
301301
prop.value.type === 'BooleanLiteral' &&
302302
prop.value.value
303303
)

0 commit comments

Comments
 (0)