wip: refactor compat check utils

This commit is contained in:
Evan You 2021-04-09 23:51:50 -04:00
parent 7a25cbb7a7
commit a2f441dc0e
8 changed files with 53 additions and 22 deletions

View File

@ -34,7 +34,7 @@ import {
import { queuePostRenderEffect } from './renderer' import { queuePostRenderEffect } from './renderer'
import { warn } from './warning' import { warn } from './warning'
import { DeprecationTypes } from './compat/deprecations' import { DeprecationTypes } from './compat/deprecations'
import { isCompatEnabled, softAssertCompatEnabled } from './compat/compatConfig' import { checkCompatEnabled, isCompatEnabled } from './compat/compatConfig'
export type WatchEffect = (onInvalidate: InvalidateCbRegistrator) => void export type WatchEffect = (onInvalidate: InvalidateCbRegistrator) => void
@ -226,7 +226,7 @@ function doWatch(
const val = baseGetter() const val = baseGetter()
if ( if (
isArray(val) && isArray(val) &&
softAssertCompatEnabled(DeprecationTypes.WATCH_ARRAY, instance) checkCompatEnabled(DeprecationTypes.WATCH_ARRAY, instance)
) { ) {
traverse(val) traverse(val)
} }

View File

@ -1,7 +1,10 @@
import Vue from '@vue/compat' import Vue from '@vue/compat'
describe('compat: global API', () => { describe('compat: global API', () => {
test('should work', async () => { beforeEach(() => Vue.configureCompat({ MODE: 2 }))
afterEach(() => Vue.configureCompat({ MODE: 3 }))
test('should work', () => {
const el = document.createElement('div') const el = document.createElement('div')
el.innerHTML = `{{ msg }}` el.innerHTML = `{{ msg }}`
new Vue({ new Vue({

View File

@ -39,6 +39,9 @@ export function isCompatEnabled(
} }
} }
/**
* Use this for features that are completely removed in non-compat build.
*/
export function assertCompatEnabled( export function assertCompatEnabled(
key: DeprecationTypes, key: DeprecationTypes,
instance: ComponentInternalInstance | null, instance: ComponentInternalInstance | null,
@ -51,6 +54,10 @@ export function assertCompatEnabled(
} }
} }
/**
* Use this for features where legacy usage is still possible, but will likely
* lead to runtime error if compat is disabled. (warn in all cases)
*/
export function softAssertCompatEnabled( export function softAssertCompatEnabled(
key: DeprecationTypes, key: DeprecationTypes,
instance: ComponentInternalInstance | null, instance: ComponentInternalInstance | null,
@ -62,12 +69,26 @@ export function softAssertCompatEnabled(
return isCompatEnabled(key, instance) return isCompatEnabled(key, instance)
} }
// disable features that conflict with v3 behavior /**
* Use this for features with the same syntax but with mutually exclusive
* behavior in 2 vs 3. Only warn if compat is enabled.
* e.g. render function
*/
export function checkCompatEnabled(
key: DeprecationTypes,
instance: ComponentInternalInstance | null,
...args: any[]
) {
const enabled = isCompatEnabled(key, instance)
if (__DEV__ && enabled) {
warnDeprecation(key, instance, ...args)
}
return enabled
}
// run tests in v3 mode by default
if (__TEST__) { if (__TEST__) {
configureCompat({ configureCompat({
COMPONENT_ASYNC: false, MODE: 3
COMPONENT_FUNCTIONAL: false,
WATCH_ARRAY: false,
INSTANCE_ATTRS_CLASS_STYLE: false
}) })
} }

View File

@ -10,8 +10,8 @@ import {
import { resolveInjections } from '../componentOptions' import { resolveInjections } from '../componentOptions'
import { InternalSlots } from '../componentSlots' import { InternalSlots } from '../componentSlots'
import { isVNode } from '../vnode' import { isVNode } from '../vnode'
import { isCompatEnabled, softAssertCompatEnabled } from './compatConfig' import { checkCompatEnabled, softAssertCompatEnabled } from './compatConfig'
import { DeprecationTypes, warnDeprecation } from './deprecations' import { DeprecationTypes } from './deprecations'
import { getCompatListeners } from './instanceListeners' import { getCompatListeners } from './instanceListeners'
import { compatH } from './renderFn' import { compatH } from './renderFn'
@ -24,9 +24,8 @@ export function convertLegacyComponent(
// use softAssert here. // use softAssert here.
if ( if (
isFunction(comp) && isFunction(comp) &&
isCompatEnabled(DeprecationTypes.COMPONENT_ASYNC, instance) checkCompatEnabled(DeprecationTypes.COMPONENT_ASYNC, instance, comp)
) { ) {
__DEV__ && warnDeprecation(DeprecationTypes.COMPONENT_ASYNC, instance, comp)
return convertLegacyAsyncComponent(comp) return convertLegacyAsyncComponent(comp)
} }

View File

@ -1,8 +1,12 @@
import { extend, NOOP } from '@vue/shared' import { extend, NOOP } from '@vue/shared'
import { PublicPropertiesMap } from '../componentPublicInstance' import { PublicPropertiesMap } from '../componentPublicInstance'
import { getCompatChildren } from './instanceChildren' import { getCompatChildren } from './instanceChildren'
import { assertCompatEnabled, isCompatEnabled } from './compatConfig' import {
import { DeprecationTypes, warnDeprecation } from './deprecations' assertCompatEnabled,
checkCompatEnabled,
isCompatEnabled
} from './compatConfig'
import { DeprecationTypes } from './deprecations'
import { off, on, once } from './instanceEventEmitter' import { off, on, once } from './instanceEventEmitter'
import { getCompatListeners } from './instanceListeners' import { getCompatListeners } from './instanceListeners'
import { shallowReadonly } from '@vue/reactivity' import { shallowReadonly } from '@vue/reactivity'
@ -48,7 +52,7 @@ export function installCompatInstanceProperties(map: PublicPropertiesMap) {
if (isCompatEnabled(DeprecationTypes.RENDER_FUNCTION, i)) { if (isCompatEnabled(DeprecationTypes.RENDER_FUNCTION, i)) {
return new Proxy(i.slots, legacySlotProxyHandlers) return new Proxy(i.slots, legacySlotProxyHandlers)
} }
return i.slots return __DEV__ ? shallowReadonly(i.slots) : i.slots
}, },
$scopedSlots: i => { $scopedSlots: i => {
@ -59,7 +63,7 @@ export function installCompatInstanceProperties(map: PublicPropertiesMap) {
// overrides existing accessor // overrides existing accessor
$attrs: i => { $attrs: i => {
if (__DEV__ && i.type.inheritAttrs === false) { if (__DEV__ && i.type.inheritAttrs === false) {
warnDeprecation(DeprecationTypes.INSTANCE_ATTRS_CLASS_STYLE, i) checkCompatEnabled(DeprecationTypes.INSTANCE_ATTRS_CLASS_STYLE, i)
} }
return __DEV__ ? shallowReadonly(i.attrs) : i.attrs return __DEV__ ? shallowReadonly(i.attrs) : i.attrs
}, },

View File

@ -54,8 +54,8 @@ import { CompilerOptions } from '@vue/compiler-core'
import { markAttrsAccessed } from './componentRenderUtils' import { markAttrsAccessed } from './componentRenderUtils'
import { currentRenderingInstance } from './componentRenderContext' import { currentRenderingInstance } from './componentRenderContext'
import { startMeasure, endMeasure } from './profiling' import { startMeasure, endMeasure } from './profiling'
import { isCompatEnabled } from './compat/compatConfig' import { checkCompatEnabled } from './compat/compatConfig'
import { DeprecationTypes, warnDeprecation } from './compat/deprecations' import { DeprecationTypes } from './compat/deprecations'
import { compatH } from './compat/renderFn' import { compatH } from './compat/renderFn'
export type Data = Record<string, unknown> export type Data = Record<string, unknown>
@ -687,9 +687,8 @@ export function finishComponentSetup(
if ( if (
__COMPAT__ && __COMPAT__ &&
Component.render && Component.render &&
isCompatEnabled(DeprecationTypes.RENDER_FUNCTION, instance) checkCompatEnabled(DeprecationTypes.RENDER_FUNCTION, instance)
) { ) {
warnDeprecation(DeprecationTypes.RENDER_FUNCTION, instance)
const originalRender = Component.render const originalRender = Component.render
Component.render = function compatRender() { Component.render = function compatRender() {
return originalRender.call(this, compatH) return originalRender.call(this, compatH)

View File

@ -288,12 +288,17 @@ export { LegacyConfig } from './compat/globalConfig'
import { warnDeprecation } from './compat/deprecations' import { warnDeprecation } from './compat/deprecations'
import { createCompatVue } from './compat/global' import { createCompatVue } from './compat/global'
import { isCompatEnabled, softAssertCompatEnabled } from './compat/compatConfig' import {
isCompatEnabled,
checkCompatEnabled,
softAssertCompatEnabled
} from './compat/compatConfig'
const _compatUtils = { const _compatUtils = {
warnDeprecation, warnDeprecation,
createCompatVue, createCompatVue,
isCompatEnabled, isCompatEnabled,
checkCompatEnabled,
softAssertCompatEnabled softAssertCompatEnabled
} }

View File

@ -106,7 +106,7 @@ const TransitionGroupImpl = {
if ( if (
__COMPAT__ && __COMPAT__ &&
!rawProps.tag && !rawProps.tag &&
compatUtils.softAssertCompatEnabled( compatUtils.checkCompatEnabled(
DeprecationTypes.TRANSITION_GROUP_ROOT, DeprecationTypes.TRANSITION_GROUP_ROOT,
instance.parent instance.parent
) )