]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): defineProps return binding or rest binding should be considered...
authorEvan You <yyx990803@gmail.com>
Tue, 10 May 2022 09:34:15 +0000 (17:34 +0800)
committerEvan You <yyx990803@gmail.com>
Tue, 10 May 2022 09:34:15 +0000 (17:34 +0800)
packages/compiler-core/src/options.ts
packages/compiler-core/src/transforms/transformElement.ts
packages/compiler-core/src/transforms/transformExpression.ts
packages/compiler-sfc/__tests__/compileScript.spec.ts
packages/compiler-sfc/__tests__/compileScriptPropsTransform.spec.ts
packages/compiler-sfc/src/compileScript.ts

index a0658b4d75ddc781bbcc4463b20cef8e792fbac3..1221772f70661a034ef585b3f0a0b73fa31914b7 100644 (file)
@@ -97,6 +97,10 @@ export const enum BindingTypes {
    * template expressions.
    */
   SETUP_CONST = 'setup-const',
+  /**
+   * a const binding that does not need `unref()`, but may be mutated.
+   */
+  SETUP_REACTIVE_CONST = 'setup-reactive-const',
   /**
    * a const binding that may be a ref.
    */
index 7143963d1f5d0b1ec73b6da8f947ce062e171b11..3e2f74c0d0105a3ed96b2f5d28d969b4721463be 100644 (file)
@@ -352,7 +352,9 @@ function resolveSetupReference(name: string, context: TransformContext) {
     }
   }
 
-  const fromConst = checkType(BindingTypes.SETUP_CONST)
+  const fromConst =
+    checkType(BindingTypes.SETUP_CONST) ||
+    checkType(BindingTypes.SETUP_REACTIVE_CONST)
   if (fromConst) {
     return context.inline
       ? // in inline mode, const setup bindings (e.g. imports) can be used as-is
index 7a5e699efb4f36ba085fece5b302732392b4ca18..a3a48be61835e0bf178fc85aeb2c03edb5635e76 100644 (file)
@@ -122,7 +122,11 @@ export function processExpression(
       const isDestructureAssignment =
         parent && isInDestructureAssignment(parent, parentStack)
 
-      if (type === BindingTypes.SETUP_CONST || localVars[raw]) {
+      if (
+        type === BindingTypes.SETUP_CONST ||
+        type === BindingTypes.SETUP_REACTIVE_CONST ||
+        localVars[raw]
+      ) {
         return raw
       } else if (type === BindingTypes.SETUP_REF) {
         return `${raw}.value`
index f87a326c82f8aa6d8ce7b897c77478d16fd18e11..d7b10f5d735109f9318a3c4e2e11ff17f347f4e8 100644 (file)
@@ -68,7 +68,7 @@ const bar = 1
     expect(bindings).toStrictEqual({
       foo: BindingTypes.PROPS,
       bar: BindingTypes.SETUP_CONST,
-      props: BindingTypes.SETUP_CONST
+      props: BindingTypes.SETUP_REACTIVE_CONST
     })
 
     // should remove defineOptions import and call
index e7fbb31c344eff19e78f41179c184e23197dd5d9..0dafc0200dd77ee21102b1a57b5efd1267a28a31 100644 (file)
@@ -141,7 +141,7 @@ describe('sfc props transform', () => {
       foo: BindingTypes.PROPS,
       bar: BindingTypes.PROPS,
       baz: BindingTypes.PROPS,
-      rest: BindingTypes.SETUP_CONST
+      rest: BindingTypes.SETUP_REACTIVE_CONST
     })
   })
 
index 7c16bcc8f372862058e0b51dc0b4a1b78b500e77..5ac05b31e4e26e9727302ca1c818a184870ca554 100644 (file)
@@ -1207,7 +1207,8 @@ export function compileScript(
   // props aliases
   if (propsDestructureDecl) {
     if (propsDestructureRestId) {
-      bindingMetadata[propsDestructureRestId] = BindingTypes.SETUP_CONST
+      bindingMetadata[propsDestructureRestId] =
+        BindingTypes.SETUP_REACTIVE_CONST
     }
     for (const key in propsDestructuredBindings) {
       const { local } = propsDestructuredBindings[key]
@@ -1525,14 +1526,18 @@ function walkDeclaration(
         const userReactiveBinding = userImportAlias['reactive'] || 'reactive'
         if (isCallOf(init, userReactiveBinding)) {
           // treat reactive() calls as let since it's meant to be mutable
-          bindingType = BindingTypes.SETUP_LET
+          bindingType = isConst
+            ? BindingTypes.SETUP_REACTIVE_CONST
+            : BindingTypes.SETUP_LET
         } else if (
           // if a declaration is a const literal, we can mark it so that
           // the generated render fn code doesn't need to unref() it
           isDefineCall ||
           (isConst && canNeverBeRef(init!, userReactiveBinding))
         ) {
-          bindingType = BindingTypes.SETUP_CONST
+          bindingType = isCallOf(init, DEFINE_PROPS)
+            ? BindingTypes.SETUP_REACTIVE_CONST
+            : BindingTypes.SETUP_CONST
         } else if (isConst) {
           if (isCallOf(init, userImportAlias['ref'] || 'ref')) {
             bindingType = BindingTypes.SETUP_REF