All files / src/instance proxy.ts

100% Statements 23/23
87.5% Branches 7/8
100% Functions 4/4
100% Lines 23/23

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  1x 1x 1x 1x 1x 1x             54x 28x   54x 7x       54x   1x   20x 20x 20x 20x 90x 90x 20x   70x     70x 50x      
import { hasOwn } from '../util/hasOwn'
import { isReserved } from '../util/isReserved'
import { noop } from '../util/noop'
 
const sharedPropertyDefinition = {
  enumerable: true,
  configurable: true,
  get: noop,
  set: noop,
}
 
export function proxy (target, sourceKey, key) {
  sharedPropertyDefinition.get = function proxyGetter () {
    return this._renderProxy[sourceKey][key]
  }
  sharedPropertyDefinition.set = function proxySetter (val) {
    this._renderProxy.setData({
      [key]: val,
    })
  }
  Object.defineProperty(target, key, sharedPropertyDefinition)
}
 
export function initProxy (vm) {
  const props = vm.$options.props
  const keys = Object.keys(vm._renderProxy.data)
  let i = keys.length
  while (i--) {
    const key = keys[i]
    if (props && hasOwn(props, key)) {
      continue
    } else if (!isReserved(key)) {
      // 在 extend 时,静态 props & computed 已经在组件的原型上代理
      // 所以只需E要在实例上代理其他的 props
      if (!(key in vm)) {
        proxy(vm, 'data', key)
      }
    }
  }
}