From: edison Date: Thu, 30 Nov 2023 10:03:23 +0000 (+0800) Subject: fix(compiler-sfc): support resolving components from props (#8785) X-Git-Tag: v3.3.10~24 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7cbcee3d831241a8bd3588ae92d3f27e3641e25f;p=thirdparty%2Fvuejs%2Fcore.git fix(compiler-sfc): support resolving components from props (#8785) --- diff --git a/packages/compiler-core/__tests__/transforms/transformElement.spec.ts b/packages/compiler-core/__tests__/transforms/transformElement.spec.ts index 97559369d8..c83bdeebef 100644 --- a/packages/compiler-core/__tests__/transforms/transformElement.spec.ts +++ b/packages/compiler-core/__tests__/transforms/transformElement.spec.ts @@ -152,6 +152,28 @@ describe('compiler: element transform', () => { expect(node.tag).toBe(`Foo.Example`) }) + test('resolve namespaced component from props bindings (inline)', () => { + const { root, node } = parseWithElementTransform(``, { + inline: true, + bindingMetadata: { + Foo: BindingTypes.PROPS + } + }) + expect(root.helpers).not.toContain(RESOLVE_COMPONENT) + expect(node.tag).toBe(`_unref(__props["Foo"]).Example`) + }) + + test('resolve namespaced component from props bindings (non-inline)', () => { + const { root, node } = parseWithElementTransform(``, { + inline: false, + bindingMetadata: { + Foo: BindingTypes.PROPS + } + }) + expect(root.helpers).not.toContain(RESOLVE_COMPONENT) + expect(node.tag).toBe('_unref($props["Foo"]).Example') + }) + test('do not resolve component from non-script-setup bindings', () => { const bindingMetadata = { Example: BindingTypes.SETUP_MAYBE_REF diff --git a/packages/compiler-core/src/transforms/transformElement.ts b/packages/compiler-core/src/transforms/transformElement.ts index fd61f01105..2b5f23a617 100644 --- a/packages/compiler-core/src/transforms/transformElement.ts +++ b/packages/compiler-core/src/transforms/transformElement.ts @@ -385,6 +385,13 @@ function resolveSetupReference(name: string, context: TransformContext) { `${context.helperString(UNREF)}(${fromMaybeRef})` : `$setup[${JSON.stringify(fromMaybeRef)}]` } + + const fromProps = checkType(BindingTypes.PROPS) + if (fromProps) { + return `${context.helperString(UNREF)}(${ + context.inline ? '__props' : '$props' + }[${JSON.stringify(fromProps)}])` + } } export type PropsExpression = ObjectExpression | CallExpression | ExpressionNode