Skip to content

Commit e79a6df

Browse files
authored
fix(vapor/defineProps): register type bindings before compile template + props destructure work with vapor (#12545)
1 parent 6c0e8a8 commit e79a6df

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ return () => {}
117117

118118
exports[`sfc reactive props destructure > default values w/ type declaration & key is string 1`] = `
119119
"import { defineComponent as _defineComponent } from 'vue'
120+
import { toDisplayString as _toDisplayString } from "vue"
121+
120122
121123
export default /*@__PURE__*/_defineComponent({
122124
props: {
@@ -129,7 +131,9 @@ export default /*@__PURE__*/_defineComponent({
129131
130132
131133
132-
return () => {}
134+
return (_ctx: any,_cache: any) => {
135+
return _toDisplayString(__props.foo)
136+
}
133137
}
134138
135139
})"

packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ describe('sfc reactive props destructure', () => {
155155
"onUpdate:modelValue": (val: number) => void // double-quoted string containing symbols
156156
}>()
157157
</script>
158+
<template>{{ foo }}</template>
158159
`)
159160
expect(bindings).toStrictEqual({
160161
__propsAliases: {
@@ -173,6 +174,7 @@ describe('sfc reactive props destructure', () => {
173174
"foo:bar": { type: String, required: true, default: 'foo-bar' },
174175
"onUpdate:modelValue": { type: Function, required: true }
175176
},`)
177+
expect(content).toMatch(`__props.foo`)
176178
assertCode(content)
177179
})
178180

packages/compiler-sfc/src/script/defineProps.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ export function processDefineProps(
7979
)
8080
}
8181
ctx.propsTypeDecl = node.typeParameters.params[0]
82+
// register bindings
83+
const { props } = resolveTypeElements(ctx, ctx.propsTypeDecl)
84+
if (props) {
85+
for (const key in props) {
86+
if (!(key in ctx.bindingMetadata)) {
87+
ctx.bindingMetadata[key] = BindingTypes.PROPS
88+
}
89+
}
90+
}
8291
}
8392

8493
// handle props destructure
@@ -190,10 +199,6 @@ export function extractRuntimeProps(
190199

191200
for (const prop of props) {
192201
propStrings.push(genRuntimePropFromType(ctx, prop, hasStaticDefaults))
193-
// register bindings
194-
if ('bindingMetadata' in ctx && !(prop.key in ctx.bindingMetadata)) {
195-
ctx.bindingMetadata[prop.key] = BindingTypes.PROPS
196-
}
197202
}
198203

199204
let propsDecls = `{

packages/compiler-vapor/src/generators/expression.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isGloballyAllowed } from '@vue/shared'
1+
import { genPropsAccessExp, isGloballyAllowed } from '@vue/shared'
22
import {
33
BindingTypes,
44
NewlineType,
@@ -164,6 +164,12 @@ function genIdentifier(
164164
? `${helper('isRef')}(${raw}) ? (${raw}.value = ${assignment}) : null`
165165
: unref()
166166
break
167+
case BindingTypes.PROPS:
168+
raw = genPropsAccessExp(raw)
169+
break
170+
case BindingTypes.PROPS_ALIASED:
171+
raw = genPropsAccessExp(bindingMetadata.__propsAliases![raw])
172+
break
167173
default:
168174
raw = withAssignment(raw)
169175
}

0 commit comments

Comments
 (0)