Skip to content

Commit 7cbcee3

Browse files
authored
fix(compiler-sfc): support resolving components from props (#8785)
1 parent 9845f1d commit 7cbcee3

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

packages/compiler-core/__tests__/transforms/transformElement.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,28 @@ describe('compiler: element transform', () => {
152152
expect(node.tag).toBe(`Foo.Example`)
153153
})
154154

155+
test('resolve namespaced component from props bindings (inline)', () => {
156+
const { root, node } = parseWithElementTransform(`<Foo.Example/>`, {
157+
inline: true,
158+
bindingMetadata: {
159+
Foo: BindingTypes.PROPS
160+
}
161+
})
162+
expect(root.helpers).not.toContain(RESOLVE_COMPONENT)
163+
expect(node.tag).toBe(`_unref(__props["Foo"]).Example`)
164+
})
165+
166+
test('resolve namespaced component from props bindings (non-inline)', () => {
167+
const { root, node } = parseWithElementTransform(`<Foo.Example/>`, {
168+
inline: false,
169+
bindingMetadata: {
170+
Foo: BindingTypes.PROPS
171+
}
172+
})
173+
expect(root.helpers).not.toContain(RESOLVE_COMPONENT)
174+
expect(node.tag).toBe('_unref($props["Foo"]).Example')
175+
})
176+
155177
test('do not resolve component from non-script-setup bindings', () => {
156178
const bindingMetadata = {
157179
Example: BindingTypes.SETUP_MAYBE_REF

packages/compiler-core/src/transforms/transformElement.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,13 @@ function resolveSetupReference(name: string, context: TransformContext) {
385385
`${context.helperString(UNREF)}(${fromMaybeRef})`
386386
: `$setup[${JSON.stringify(fromMaybeRef)}]`
387387
}
388+
389+
const fromProps = checkType(BindingTypes.PROPS)
390+
if (fromProps) {
391+
return `${context.helperString(UNREF)}(${
392+
context.inline ? '__props' : '$props'
393+
}[${JSON.stringify(fromProps)}])`
394+
}
388395
}
389396

390397
export type PropsExpression = ObjectExpression | CallExpression | ExpressionNode

0 commit comments

Comments
 (0)