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

This commit is contained in:
edison 2024-12-16 14:18:27 +08:00 committed by GitHub
parent 6c0e8a8f24
commit e79a6df093
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 23 additions and 6 deletions

View File

@ -117,6 +117,8 @@ return () => {}
exports[`sfc reactive props destructure > default values w/ type declaration & key is string 1`] = ` exports[`sfc reactive props destructure > default values w/ type declaration & key is string 1`] = `
"import { defineComponent as _defineComponent } from 'vue' "import { defineComponent as _defineComponent } from 'vue'
import { toDisplayString as _toDisplayString } from "vue"
export default /*@__PURE__*/_defineComponent({ export default /*@__PURE__*/_defineComponent({
props: { props: {
@ -129,7 +131,9 @@ export default /*@__PURE__*/_defineComponent({
return () => {} return (_ctx: any,_cache: any) => {
return _toDisplayString(__props.foo)
}
} }
})" })"

View File

@ -155,6 +155,7 @@ describe('sfc reactive props destructure', () => {
"onUpdate:modelValue": (val: number) => void // double-quoted string containing symbols "onUpdate:modelValue": (val: number) => void // double-quoted string containing symbols
}>() }>()
</script> </script>
<template>{{ foo }}</template>
`) `)
expect(bindings).toStrictEqual({ expect(bindings).toStrictEqual({
__propsAliases: { __propsAliases: {
@ -173,6 +174,7 @@ describe('sfc reactive props destructure', () => {
"foo:bar": { type: String, required: true, default: 'foo-bar' }, "foo:bar": { type: String, required: true, default: 'foo-bar' },
"onUpdate:modelValue": { type: Function, required: true } "onUpdate:modelValue": { type: Function, required: true }
},`) },`)
expect(content).toMatch(`__props.foo`)
assertCode(content) assertCode(content)
}) })

View File

@ -79,6 +79,15 @@ export function processDefineProps(
) )
} }
ctx.propsTypeDecl = node.typeParameters.params[0] 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 // handle props destructure
@ -190,10 +199,6 @@ export function extractRuntimeProps(
for (const prop of props) { for (const prop of props) {
propStrings.push(genRuntimePropFromType(ctx, prop, hasStaticDefaults)) 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 = `{ let propsDecls = `{

View File

@ -1,4 +1,4 @@
import { isGloballyAllowed } from '@vue/shared' import { genPropsAccessExp, isGloballyAllowed } from '@vue/shared'
import { import {
BindingTypes, BindingTypes,
NewlineType, NewlineType,
@ -164,6 +164,12 @@ function genIdentifier(
? `${helper('isRef')}(${raw}) ? (${raw}.value = ${assignment}) : null` ? `${helper('isRef')}(${raw}) ? (${raw}.value = ${assignment}) : null`
: unref() : unref()
break break
case BindingTypes.PROPS:
raw = genPropsAccessExp(raw)
break
case BindingTypes.PROPS_ALIASED:
raw = genPropsAccessExp(bindingMetadata.__propsAliases![raw])
break
default: default:
raw = withAssignment(raw) raw = withAssignment(raw)
} }