All files / runtime-core/src/compat componentFunctional.ts

89.65% Statements 26/29
70% Branches 7/10
83.33% Functions 5/6
89.65% Lines 26/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 6584x         84x   84x 84x   84x         84x   1x 1x       84x 2x       2x   2x 2x   2x             1x           1x 1x 1x 1x         2x   2x 2x 2x   2x   2x 2x    
import {
  ComponentOptions,
  FunctionalComponent,
  getCurrentInstance
} from '../component'
import { resolveInjections } from '../componentOptions'
import { InternalSlots } from '../componentSlots'
import { getCompatListeners } from './instanceListeners'
import { compatH } from './renderFn'
 
const normalizedFunctionalComponentMap = new Map<
  ComponentOptions,
  FunctionalComponent
>()
 
export const legacySlotProxyHandlers: ProxyHandler<InternalSlots> = {
  get(target, key: string) {
    const slot = target[key]
    return slot && slot()
  }
}
 
export function convertLegacyFunctionalComponent(comp: ComponentOptions) {
  Iif (normalizedFunctionalComponentMap.has(comp)) {
    return normalizedFunctionalComponentMap.get(comp)!
  }
 
  const legacyFn = comp.render as any
 
  const Func: FunctionalComponent = (props, ctx) => {
    const instance = getCurrentInstance()!
 
    const legacyCtx = {
      props,
      children: instance.vnode.children || [],
      data: instance.vnode.props || {},
      scopedSlots: ctx.slots,
      parent: instance.parent && instance.parent.proxy,
      slots() {
        return new Proxy(ctx.slots, legacySlotProxyHandlers)
      },
      get listeners() {
        return getCompatListeners(instance)
      },
      get injections() {
        if (comp.inject) {
          const injections = {}
          resolveInjections(comp.inject, injections)
          return injections
        }
        return {}
      }
    }
    return legacyFn(compatH, legacyCtx)
  }
  Func.props = comp.props
  Func.displayName = comp.name
  Func.compatConfig = comp.compatConfig
  // v2 functional components do not inherit attrs
  Func.inheritAttrs = false
 
  normalizedFunctionalComponentMap.set(comp, Func)
  return Func
}