Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -129,7 +131,9 @@ export default /*@__PURE__*/_defineComponent({



return () => {}
return (_ctx: any,_cache: any) => {
return _toDisplayString(__props.foo)
}
}

})"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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)
})

Expand Down
13 changes: 9 additions & 4 deletions packages/compiler-sfc/src/script/defineProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ export function processDefineProps(
)
}
ctx.propsTypeDecl = node.typeParameters.params[0]
// register bindings
const { props } = resolveTypeElements(ctx, ctx.propsTypeDecl)
Copy link
Member Author

@edison1105 edison1105 Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this changes make sure that foo will be overwritten as __props.foo instead of _ctx.foo
see Playground

if (props) {
for (const key in props) {
if (!(key in ctx.bindingMetadata)) {
ctx.bindingMetadata[key] = BindingTypes.PROPS
}
}
}
}

// handle props destructure
Expand Down Expand Up @@ -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 = `{
Expand Down
8 changes: 7 additions & 1 deletion packages/compiler-vapor/src/generators/expression.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isGloballyAllowed } from '@vue/shared'
import { genPropsAccessExp, isGloballyAllowed } from '@vue/shared'
import {
BindingTypes,
NewlineType,
Expand Down Expand Up @@ -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)
}
Expand Down
Loading