2021-04-07 23:22:56 +08:00
|
|
|
import { extend } from '@vue/shared'
|
2021-04-10 06:52:14 +08:00
|
|
|
import { ComponentInternalInstance, ComponentOptions } from '../component'
|
2021-04-08 03:38:04 +08:00
|
|
|
import { DeprecationTypes, warnDeprecation } from './deprecations'
|
2021-04-07 23:22:56 +08:00
|
|
|
|
|
|
|
export type CompatConfig = Partial<
|
2021-04-09 05:11:05 +08:00
|
|
|
Record<DeprecationTypes, boolean | 'suppress-warning'>
|
|
|
|
> & {
|
|
|
|
MODE?: 2 | 3
|
2021-04-07 23:22:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const globalCompatConfig: CompatConfig = {}
|
|
|
|
|
|
|
|
export function configureCompat(config: CompatConfig) {
|
|
|
|
extend(globalCompatConfig, config)
|
|
|
|
}
|
|
|
|
|
2021-04-10 06:52:14 +08:00
|
|
|
export function getCompatConfigForKey(
|
|
|
|
key: DeprecationTypes | 'MODE',
|
|
|
|
instance: ComponentInternalInstance | null
|
|
|
|
) {
|
2021-04-09 05:11:05 +08:00
|
|
|
const instanceConfig =
|
|
|
|
instance && (instance.type as ComponentOptions).compatConfig
|
|
|
|
if (instanceConfig && key in instanceConfig) {
|
|
|
|
return instanceConfig[key]
|
|
|
|
}
|
2021-04-07 23:22:56 +08:00
|
|
|
return globalCompatConfig[key]
|
|
|
|
}
|
2021-04-08 00:24:45 +08:00
|
|
|
|
2021-04-10 06:52:14 +08:00
|
|
|
export function isCompatEnabled(
|
|
|
|
key: DeprecationTypes,
|
|
|
|
instance: ComponentInternalInstance | null
|
|
|
|
): boolean {
|
2021-04-11 23:15:40 +08:00
|
|
|
// skip compat for built-in components
|
|
|
|
if (instance && instance.type.__isBuiltIn) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-04-10 06:52:14 +08:00
|
|
|
const mode = getCompatConfigForKey('MODE', instance) || 2
|
|
|
|
const val = getCompatConfigForKey(key, instance)
|
2021-04-09 05:11:05 +08:00
|
|
|
if (mode === 2) {
|
|
|
|
return val !== false
|
|
|
|
} else {
|
|
|
|
return val === true || val === 'suppress-warning'
|
|
|
|
}
|
2021-04-08 03:38:04 +08:00
|
|
|
}
|
|
|
|
|
2021-04-10 11:51:50 +08:00
|
|
|
/**
|
|
|
|
* Use this for features that are completely removed in non-compat build.
|
|
|
|
*/
|
2021-04-10 06:52:14 +08:00
|
|
|
export function assertCompatEnabled(
|
|
|
|
key: DeprecationTypes,
|
|
|
|
instance: ComponentInternalInstance | null,
|
|
|
|
...args: any[]
|
|
|
|
) {
|
|
|
|
if (!isCompatEnabled(key, instance)) {
|
2021-04-08 03:38:04 +08:00
|
|
|
throw new Error(`${key} compat has been disabled.`)
|
|
|
|
} else if (__DEV__) {
|
2021-04-10 06:52:14 +08:00
|
|
|
warnDeprecation(key, instance, ...args)
|
2021-04-08 03:38:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 11:51:50 +08:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
*/
|
2021-04-10 06:52:14 +08:00
|
|
|
export function softAssertCompatEnabled(
|
|
|
|
key: DeprecationTypes,
|
|
|
|
instance: ComponentInternalInstance | null,
|
|
|
|
...args: any[]
|
|
|
|
) {
|
2021-04-08 03:38:04 +08:00
|
|
|
if (__DEV__) {
|
2021-04-10 06:52:14 +08:00
|
|
|
warnDeprecation(key, instance, ...args)
|
2021-04-08 03:38:04 +08:00
|
|
|
}
|
2021-04-10 06:52:14 +08:00
|
|
|
return isCompatEnabled(key, instance)
|
2021-04-08 00:24:45 +08:00
|
|
|
}
|
2021-04-09 04:33:53 +08:00
|
|
|
|
2021-04-10 11:51:50 +08:00
|
|
|
/**
|
|
|
|
* 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
|
2021-04-09 04:33:53 +08:00
|
|
|
if (__TEST__) {
|
|
|
|
configureCompat({
|
2021-04-10 11:51:50 +08:00
|
|
|
MODE: 3
|
2021-04-09 04:33:53 +08:00
|
|
|
})
|
|
|
|
}
|