]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
wip: props default this compat
authorEvan You <yyx990803@gmail.com>
Tue, 6 Apr 2021 15:08:21 +0000 (11:08 -0400)
committerEvan You <yyx990803@gmail.com>
Wed, 7 Apr 2021 20:19:24 +0000 (16:19 -0400)
packages/runtime-core/src/compat/deprecations.ts
packages/runtime-core/src/compat/props.ts [new file with mode: 0644]
packages/runtime-core/src/componentProps.ts

index b002cd0c63d1dccf43e9b73f9958da86914e0f11..44fd443854842cef69eafbaba9089d27417ce2cc 100644 (file)
@@ -22,7 +22,9 @@ export const enum DeprecationTypes {
   OPTIONS_DATA_FN,
   OPTIONS_DATA_MERGE,
   OPTIONS_BEFORE_DESTROY,
-  OPTIONS_DESTROYED
+  OPTIONS_DESTROYED,
+
+  PROPS_DEFAULT_THIS
 }
 
 type DeprecationData = {
@@ -137,7 +139,7 @@ const deprecations: Record<DeprecationTypes, DeprecationData> = {
 
   [DeprecationTypes.OPTIONS_DATA_MERGE]: {
     message: (key: string) =>
-      `Detected conflicting key "${key}" when merging "data" option values. ` +
+      `Detected conflicting key "${key}" when merging data option values. ` +
       `In Vue 3, data keys are merged shallowly and will override one another.`,
     link: `https://v3.vuejs.org/guide/migration/data-option.html#mixin-merge-behavior-change`
   },
@@ -148,6 +150,13 @@ const deprecations: Record<DeprecationTypes, DeprecationData> = {
 
   [DeprecationTypes.OPTIONS_DESTROYED]: {
     message: `\`destroyed\` has been renamed to \`unmounted\`.`
+  },
+
+  [DeprecationTypes.PROPS_DEFAULT_THIS]: {
+    message: (key: string) =>
+      `props default value function no longer has access to "this". ` +
+      `(found in prop "${key}")`,
+    link: `https://v3.vuejs.org/guide/migration/props-default-this.html`
   }
 }
 
diff --git a/packages/runtime-core/src/compat/props.ts b/packages/runtime-core/src/compat/props.ts
new file mode 100644 (file)
index 0000000..1582cb0
--- /dev/null
@@ -0,0 +1,12 @@
+import { DeprecationTypes, warnDeprecation } from './deprecations'
+
+export function createPropsDefaultThis(propKey: string) {
+  return new Proxy(
+    {},
+    {
+      get() {
+        warnDeprecation(DeprecationTypes.PROPS_DEFAULT_THIS, propKey)
+      }
+    }
+  )
+}
index 0238bf80e2809a2e9e892ca1ef25b1c7328d78bf..2dccb5182e5f3f251c043a10de898df606ed878c 100644 (file)
@@ -33,6 +33,7 @@ import {
 import { isEmitListener } from './componentEmits'
 import { InternalObjectKey } from './vnode'
 import { AppContext } from './apiCreateApp'
+import { createPropsDefaultThis } from './compat/props'
 
 export type ComponentPropsOptions<P = Data> =
   | ComponentObjectPropsOptions<P>
@@ -342,7 +343,10 @@ function resolvePropValue(
           value = propsDefaults[key]
         } else {
           setCurrentInstance(instance)
-          value = propsDefaults[key] = defaultValue(props)
+          value = propsDefaults[key] =
+            __COMPAT__ && __DEV__
+              ? defaultValue.call(createPropsDefaultThis(key), props)
+              : defaultValue(props)
           setCurrentInstance(null)
         }
       } else {