]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-core): support v-bind shorthand syntax for dynamic slot name (#10218)
authorzhoulixiang <18366276315@163.com>
Tue, 6 Feb 2024 09:54:06 +0000 (17:54 +0800)
committerGitHub <noreply@github.com>
Tue, 6 Feb 2024 09:54:06 +0000 (17:54 +0800)
close #10213

packages/compiler-core/__tests__/transforms/transformSlotOutlet.spec.ts
packages/compiler-core/src/transforms/transformSlotOutlet.ts

index f8809ab6a7bea83ba1b02f132383fd0826a16d00..6420bdbbdac7f13295eb4021acc15f9eeb986691 100644 (file)
@@ -389,4 +389,20 @@ describe('compiler: transform <slot> outlets', () => {
       },
     })
   })
+
+  test('dynamically named slot outlet with v-bind shorthand', () => {
+    const ast = parseWithSlots(`<slot :name />`)
+    expect((ast.children[0] as ElementNode).codegenNode).toMatchObject({
+      type: NodeTypes.JS_CALL_EXPRESSION,
+      callee: RENDER_SLOT,
+      arguments: [
+        `$slots`,
+        {
+          type: NodeTypes.SIMPLE_EXPRESSION,
+          content: `name`,
+          isStatic: false,
+        },
+      ],
+    })
+  })
 })
index 310b6a94ea00f6deb23cfb5d4d28a0a816d62597..ea635e997b1d66e354fffc7c8aa60eed902487ce 100644 (file)
@@ -6,12 +6,14 @@ import {
   type SlotOutletNode,
   createCallExpression,
   createFunctionExpression,
+  createSimpleExpression,
 } from '../ast'
 import { isSlotOutlet, isStaticArgOf, isStaticExp } from '../utils'
 import { type PropsExpression, buildProps } from './transformElement'
 import { ErrorCodes, createCompilerError } from '../errors'
 import { RENDER_SLOT } from '../runtimeHelpers'
 import { camelize } from '@vue/shared'
+import { processExpression } from './transformExpression'
 
 export const transformSlotOutlet: NodeTransform = (node, context) => {
   if (isSlotOutlet(node)) {
@@ -76,7 +78,15 @@ export function processSlotOutlet(
       }
     } else {
       if (p.name === 'bind' && isStaticArgOf(p.arg, 'name')) {
-        if (p.exp) slotName = p.exp
+        if (p.exp) {
+          slotName = p.exp
+        } else if (p.arg && p.arg.type === NodeTypes.SIMPLE_EXPRESSION) {
+          const name = camelize(p.arg.content)
+          slotName = p.exp = createSimpleExpression(name, false, p.arg.loc)
+          if (!__BROWSER__) {
+            slotName = p.exp = processExpression(p.exp, context)
+          }
+        }
       } else {
         if (p.name === 'bind' && p.arg && isStaticExp(p.arg)) {
           p.arg.content = camelize(p.arg.content)