61 lines
1.5 KiB
TypeScript
61 lines
1.5 KiB
TypeScript
import { isArray } from '@vue/shared'
|
|
import { ComponentInternalInstance } from '../component'
|
|
import { ObjectDirective, DirectiveHook } from '../directives'
|
|
import { softAssertCompatEnabled, DeprecationTypes } from './compatConfig'
|
|
|
|
export interface LegacyDirective {
|
|
bind?: DirectiveHook
|
|
inserted?: DirectiveHook
|
|
update?: DirectiveHook
|
|
componentUpdated?: DirectiveHook
|
|
unbind?: DirectiveHook
|
|
}
|
|
|
|
const legacyDirectiveHookMap: Partial<
|
|
Record<
|
|
keyof ObjectDirective,
|
|
keyof LegacyDirective | (keyof LegacyDirective)[]
|
|
>
|
|
> = {
|
|
beforeMount: 'bind',
|
|
mounted: 'inserted',
|
|
updated: ['update', 'componentUpdated'],
|
|
unmounted: 'unbind'
|
|
}
|
|
|
|
export function mapCompatDirectiveHook(
|
|
name: keyof ObjectDirective,
|
|
dir: ObjectDirective & LegacyDirective,
|
|
instance: ComponentInternalInstance | null
|
|
): DirectiveHook | DirectiveHook[] | undefined {
|
|
const mappedName = legacyDirectiveHookMap[name]
|
|
if (mappedName) {
|
|
if (isArray(mappedName)) {
|
|
const hook: DirectiveHook[] = []
|
|
mappedName.forEach(name => {
|
|
const mappedHook = dir[name]
|
|
if (mappedHook) {
|
|
softAssertCompatEnabled(
|
|
DeprecationTypes.CUSTOM_DIR,
|
|
instance,
|
|
mappedName,
|
|
name
|
|
)
|
|
hook.push(mappedHook)
|
|
}
|
|
})
|
|
return hook.length ? hook : undefined
|
|
} else {
|
|
if (dir[mappedName]) {
|
|
softAssertCompatEnabled(
|
|
DeprecationTypes.CUSTOM_DIR,
|
|
instance,
|
|
mappedName,
|
|
name
|
|
)
|
|
}
|
|
return dir[mappedName]
|
|
}
|
|
}
|
|
}
|