Skip to content

Commit 4101441

Browse files
committed
fix(compiler-sfc): defineProps return binding or rest binding should be considered reactive
1 parent 03b03ee commit 4101441

File tree

6 files changed

+22
-7
lines changed

6 files changed

+22
-7
lines changed

packages/compiler-core/src/options.ts

+4
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ export const enum BindingTypes {
9797
* template expressions.
9898
*/
9999
SETUP_CONST = 'setup-const',
100+
/**
101+
* a const binding that does not need `unref()`, but may be mutated.
102+
*/
103+
SETUP_REACTIVE_CONST = 'setup-reactive-const',
100104
/**
101105
* a const binding that may be a ref.
102106
*/

packages/compiler-core/src/transforms/transformElement.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,9 @@ function resolveSetupReference(name: string, context: TransformContext) {
352352
}
353353
}
354354

355-
const fromConst = checkType(BindingTypes.SETUP_CONST)
355+
const fromConst =
356+
checkType(BindingTypes.SETUP_CONST) ||
357+
checkType(BindingTypes.SETUP_REACTIVE_CONST)
356358
if (fromConst) {
357359
return context.inline
358360
? // in inline mode, const setup bindings (e.g. imports) can be used as-is

packages/compiler-core/src/transforms/transformExpression.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@ export function processExpression(
122122
const isDestructureAssignment =
123123
parent && isInDestructureAssignment(parent, parentStack)
124124

125-
if (type === BindingTypes.SETUP_CONST || localVars[raw]) {
125+
if (
126+
type === BindingTypes.SETUP_CONST ||
127+
type === BindingTypes.SETUP_REACTIVE_CONST ||
128+
localVars[raw]
129+
) {
126130
return raw
127131
} else if (type === BindingTypes.SETUP_REF) {
128132
return `${raw}.value`

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const bar = 1
6868
expect(bindings).toStrictEqual({
6969
foo: BindingTypes.PROPS,
7070
bar: BindingTypes.SETUP_CONST,
71-
props: BindingTypes.SETUP_CONST
71+
props: BindingTypes.SETUP_REACTIVE_CONST
7272
})
7373

7474
// should remove defineOptions import and call

packages/compiler-sfc/__tests__/compileScriptPropsTransform.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ describe('sfc props transform', () => {
141141
foo: BindingTypes.PROPS,
142142
bar: BindingTypes.PROPS,
143143
baz: BindingTypes.PROPS,
144-
rest: BindingTypes.SETUP_CONST
144+
rest: BindingTypes.SETUP_REACTIVE_CONST
145145
})
146146
})
147147

packages/compiler-sfc/src/compileScript.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,8 @@ export function compileScript(
12071207
// props aliases
12081208
if (propsDestructureDecl) {
12091209
if (propsDestructureRestId) {
1210-
bindingMetadata[propsDestructureRestId] = BindingTypes.SETUP_CONST
1210+
bindingMetadata[propsDestructureRestId] =
1211+
BindingTypes.SETUP_REACTIVE_CONST
12111212
}
12121213
for (const key in propsDestructuredBindings) {
12131214
const { local } = propsDestructuredBindings[key]
@@ -1525,14 +1526,18 @@ function walkDeclaration(
15251526
const userReactiveBinding = userImportAlias['reactive'] || 'reactive'
15261527
if (isCallOf(init, userReactiveBinding)) {
15271528
// treat reactive() calls as let since it's meant to be mutable
1528-
bindingType = BindingTypes.SETUP_LET
1529+
bindingType = isConst
1530+
? BindingTypes.SETUP_REACTIVE_CONST
1531+
: BindingTypes.SETUP_LET
15291532
} else if (
15301533
// if a declaration is a const literal, we can mark it so that
15311534
// the generated render fn code doesn't need to unref() it
15321535
isDefineCall ||
15331536
(isConst && canNeverBeRef(init!, userReactiveBinding))
15341537
) {
1535-
bindingType = BindingTypes.SETUP_CONST
1538+
bindingType = isCallOf(init, DEFINE_PROPS)
1539+
? BindingTypes.SETUP_REACTIVE_CONST
1540+
: BindingTypes.SETUP_CONST
15361541
} else if (isConst) {
15371542
if (isCallOf(init, userImportAlias['ref'] || 'ref')) {
15381543
bindingType = BindingTypes.SETUP_REF

0 commit comments

Comments
 (0)