vue3-core/packages/runtime-core/src/compat/compatConfig.ts

74 lines
1.8 KiB
TypeScript
Raw Normal View History

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'
import { DeprecationTypes, warnDeprecation } from './deprecations'
2021-04-07 23:22:56 +08:00
export type CompatConfig = Partial<
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
) {
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 {
const mode = getCompatConfigForKey('MODE', instance) || 2
const val = getCompatConfigForKey(key, instance)
if (mode === 2) {
return val !== false
} else {
return val === true || val === 'suppress-warning'
}
}
2021-04-10 06:52:14 +08:00
export function assertCompatEnabled(
key: DeprecationTypes,
instance: ComponentInternalInstance | null,
...args: any[]
) {
if (!isCompatEnabled(key, instance)) {
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-10 06:52:14 +08:00
export function softAssertCompatEnabled(
key: DeprecationTypes,
instance: ComponentInternalInstance | null,
...args: any[]
) {
if (__DEV__) {
2021-04-10 06:52:14 +08:00
warnDeprecation(key, instance, ...args)
}
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
// disable features that conflict with v3 behavior
if (__TEST__) {
configureCompat({
COMPONENT_ASYNC: false,
COMPONENT_FUNCTIONAL: false,
WATCH_ARRAY: false,
INSTANCE_ATTRS_CLASS_STYLE: false
2021-04-09 04:33:53 +08:00
})
}