]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(vapor/defineProps): register type bindings before compile template + props destru...
authoredison <daiwei521@126.com>
Mon, 16 Dec 2024 06:18:27 +0000 (14:18 +0800)
committerGitHub <noreply@github.com>
Mon, 16 Dec 2024 06:18:27 +0000 (14:18 +0800)
packages/compiler-sfc/__tests__/compileScript/__snapshots__/definePropsDestructure.spec.ts.snap
packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts
packages/compiler-sfc/src/script/defineProps.ts
packages/compiler-vapor/src/generators/expression.ts

index 1044b0e167cbdd12e57de493d59cde1c2f71539d..d8ce62ecd2a9cf988397ab31ed1033dbea6def64 100644 (file)
@@ -117,6 +117,8 @@ return () => {}
 
 exports[`sfc reactive props destructure > default values w/ type declaration & key is string 1`] = `
 "import { defineComponent as _defineComponent } from 'vue'
+import { toDisplayString as _toDisplayString } from "vue"
+
 
 export default /*@__PURE__*/_defineComponent({
   props: {
@@ -129,7 +131,9 @@ export default /*@__PURE__*/_defineComponent({
 
       
       
-return () => {}
+return (_ctx: any,_cache: any) => {
+  return _toDisplayString(__props.foo)
+}
 }
 
 })"
index 50602eb59bc0fa607d36e3507ff3eca7551f0322..e58aed1d3d244e2287cce7fb920734f42b253cd9 100644 (file)
@@ -155,6 +155,7 @@ describe('sfc reactive props destructure', () => {
         "onUpdate:modelValue": (val: number) => void  // double-quoted string containing symbols
       }>()
       </script>
+      <template>{{ foo }}</template>
     `)
     expect(bindings).toStrictEqual({
       __propsAliases: {
@@ -173,6 +174,7 @@ describe('sfc reactive props destructure', () => {
     "foo:bar": { type: String, required: true, default: 'foo-bar' },
     "onUpdate:modelValue": { type: Function, required: true }
   },`)
+    expect(content).toMatch(`__props.foo`)
     assertCode(content)
   })
 
index 9a4880a1a543f20f601b44c672df817d024aabc7..ac5226168e40bc3aa5a5f2f475b1c05db6d93a5f 100644 (file)
@@ -79,6 +79,15 @@ export function processDefineProps(
       )
     }
     ctx.propsTypeDecl = node.typeParameters.params[0]
+    // register bindings
+    const { props } = resolveTypeElements(ctx, ctx.propsTypeDecl)
+    if (props) {
+      for (const key in props) {
+        if (!(key in ctx.bindingMetadata)) {
+          ctx.bindingMetadata[key] = BindingTypes.PROPS
+        }
+      }
+    }
   }
 
   // handle props destructure
@@ -190,10 +199,6 @@ export function extractRuntimeProps(
 
   for (const prop of props) {
     propStrings.push(genRuntimePropFromType(ctx, prop, hasStaticDefaults))
-    // register bindings
-    if ('bindingMetadata' in ctx && !(prop.key in ctx.bindingMetadata)) {
-      ctx.bindingMetadata[prop.key] = BindingTypes.PROPS
-    }
   }
 
   let propsDecls = `{
index 69274aab56d6bcca8e74937ee3f21fba54baa404..990659b84e405c9631e29832defc8abb0e21fa1e 100644 (file)
@@ -1,4 +1,4 @@
-import { isGloballyAllowed } from '@vue/shared'
+import { genPropsAccessExp, isGloballyAllowed } from '@vue/shared'
 import {
   BindingTypes,
   NewlineType,
@@ -164,6 +164,12 @@ function genIdentifier(
               ? `${helper('isRef')}(${raw}) ? (${raw}.value = ${assignment}) : null`
               : unref()
         break
+      case BindingTypes.PROPS:
+        raw = genPropsAccessExp(raw)
+        break
+      case BindingTypes.PROPS_ALIASED:
+        raw = genPropsAccessExp(bindingMetadata.__propsAliases![raw])
+        break
       default:
         raw = withAssignment(raw)
     }