From d619a770a8adf6dff832f1794ba62d8b44c13215 Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 6 Apr 2021 11:08:21 -0400 Subject: [PATCH] wip: props default this compat --- packages/runtime-core/src/compat/deprecations.ts | 13 +++++++++++-- packages/runtime-core/src/compat/props.ts | 12 ++++++++++++ packages/runtime-core/src/componentProps.ts | 6 +++++- 3 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 packages/runtime-core/src/compat/props.ts diff --git a/packages/runtime-core/src/compat/deprecations.ts b/packages/runtime-core/src/compat/deprecations.ts index b002cd0c6..44fd44385 100644 --- a/packages/runtime-core/src/compat/deprecations.ts +++ b/packages/runtime-core/src/compat/deprecations.ts @@ -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.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.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 index 000000000..1582cb08f --- /dev/null +++ b/packages/runtime-core/src/compat/props.ts @@ -0,0 +1,12 @@ +import { DeprecationTypes, warnDeprecation } from './deprecations' + +export function createPropsDefaultThis(propKey: string) { + return new Proxy( + {}, + { + get() { + warnDeprecation(DeprecationTypes.PROPS_DEFAULT_THIS, propKey) + } + } + ) +} diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index 0238bf80e..2dccb5182 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -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

= | ComponentObjectPropsOptions

@@ -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 {