Explorar o código

fix(vapor/defineProps): register type bindings before compile template + props destructure work with vapor (#12545)

edison hai 1 ano
pai
achega
e79a6df093

+ 5 - 1
packages/compiler-sfc/__tests__/compileScript/__snapshots__/definePropsDestructure.spec.ts.snap

@@ -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)
+}
 }
 
 })"

+ 2 - 0
packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts

@@ -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)
   })
 

+ 9 - 4
packages/compiler-sfc/src/script/defineProps.ts

@@ -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 = `{

+ 7 - 1
packages/compiler-vapor/src/generators/expression.ts

@@ -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)
     }