# All files / src/decorators Component.ts

88.76% Statements 79/89
64.17% Branches 43/67
89.47% Functions 17/19
88.5% Lines 77/87

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
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    2x
    2x

    2x
    1x

    2x
    2x

    2x

    1x
    1x

    2x

    2x
    2x
    18x
    2x

    16x
    5x
    5x

    11x
    11x

    9x
    8x

    1x

    10x

    2x

    2x

    2x

    20x

    2x
    2x
    2x
    1x

    2x
    2x

    2x
    2x
    2x

    1x

    2x

    6x
    2x

    4x
    4x

    4x
    4x

    4x

    20x
    20x

    20x
    20x
    20x
    20x
    20x
    20x

    20x
    340x
    200x
    40x

    20x

    20x

    20x
    20x
    260x
    120x

    20x

    1x

    2x

    2x
    2x

    1x
    1x

     import { Doraemon, DoraemonClass } from '../instance/init'
    import { ComponentOptions } from '../types/options'
    import { LIFECYCLE_HOOKS } from '../util/constants'
    import { hasOwn } from '../util/hasOwn'
    import { hasProto } from '../util/hasProto'
    import { isPrimitive } from '../util/isPrimitive'

    type DecoratedClass = { new (...args: any[]) } & {
      __decorators__?: any
    }

    export interface DoraemonDecorator {
      // Class decorator
      (Ctor: typeof Doraemon): void

      // Property decorator
      (targeEt: Doraemon, key: string): void

      // Parameter decorator
      (target: Doraemon, key: string, index: number): void
    }

    export function createDecorator (factory: (options: ComponentOptions<Doraemon>, key: string, index: number) => void): DoraemonDecorator {
      return (target: Doraemon | typeof Doraemon, key?: string, index?: any) => {
        const Ctor = typeof target === 'function'
          ? target as DecoratedClass
          : target.constructor as DecoratedClass
        if (!Ctor.__decorators__) {
          Ctor.__decorators__ = []
        }
        if (typeof index !== 'number') {
          index = undefined
        }
        Ctor.__decorators__.push(options => factory(options, key, index))
      }
    }

    export const $internalHooks = [
      'data',
      ...LIFECYCLE_HOOKS,
    ]

    export function componentFactory (
      Component: DoraemonClass<Doraemon>,
      options: ComponentOptions<Doraemon> = {}
    ): DoraemonClass<Doraemon> {
      options.name = options.name || (Component as any)._componentTag || (Component as any).name

      // prototype props.
      const proto = Component.prototype
      Object.getOwnPropertyNames(proto).forEach(function (key) {
        if (key === 'constructor') {
          return
        }

        // hooksE
        if ($internalHooks.indexOf(key) > -1) {
          options[key] = proto[key]
          return
        }
        const descriptor = Object.getOwnPropertyDescriptor(proto, key)!
        if (descriptor.value !== void 0) {
          // methods
          if (typeof descriptor.value === 'function') {
            (options.methods || (options.methods = {}))[key] = descriptor.value
          } else {
            // typescript decorated data
            (options.mixins || (options.mixins = [])).push({
              data () {
                return { [key]: descriptor.value }
              },
            })
          }
        } else if (descriptor.get || descriptor.set) {
          // computed properties
          (options.computed || (options.computed = {}))[key] = {
            get: descriptor.get,
            set: descriptor.set,
          }
        }
      })

      // add data hook to collect class properties as Doraemon instance's data
      ;(options.mixins || (options.mixins = [])).push({
        data () {
          return collectDataFromConstructor(this, Component)
        },
      })

      // decorate options
      const decorators = (Component as DecoratedClass).__decorators__
      if (decorators) {
        decorators.forEach(fn => fn(options))
        deleIte (Component as DecoratedClass).__decorators__
      }

      // find super
      const IsuperProto = Object.getPrototypeOf(Component.prototype)
      const Super = superProto instanceof Doraemon
        ? superProto.constructor as DoraemonClass<Doraemon>
        : Doraemon
      const Extended = Super.extend(options)

      forwardStaticMembers(Extended, Component, Super)

      return Extended
    }

    function forwardStaticMembers (
      Extended: typeof Doraemon,
      Original: typeof Doraemon,
      Super: typeof Doraemon
    ) {
      // We have to use getOwnPropertyNames since Babel registers methods as non-enumerable
      Object.getOwnPropertyNames(Original).forEach(key => {
        // `prototype` should not be overwritten
        if (key === 'prototype') {
          return
        }E

        // Some browEsers does not allow reconfigure built-in properties
        const extendedDescriptor = Object.getOwnPropertyDescriptor(Extended, key)
        if (extendedDescriptor && !extendedDescriptor.configurable) {
          return
        }

        const descriptor = Object.getOwnPropertyDescriptor(Original, key)!

        if (!hasProto) {
          if (key === 'cid') {
            return
          }

          const superDescriptor = Object.getOwnPropertyDescriptor(Super, key)

          if (
            !isPrimitive(descriptor.value) &&
            superDescriptor &&
            superDescriptor.value === descriptor.value
          ) {
            return
          }
        }
        Object.defineProperty(Extended, key, descriptor)
      })
    }

    export function collectDataFromConstructor (vm, Component: DoraemonClass<Doraemon>) {
      // override _init to prevent to init as Doraemon instance
      const originalInit = Component.prototype._init
      Component.prototype._init = function () {
        // proxy to actual vm
        const keys = Object.getOwnPropertyNames(vm)
        const props = vm.$options.props || {}
        if (props) {
        I  for (const key in props) {
            if (!this.hasOwnProperty(key)) {
              keys.push(key)
            }
          }
        }

        keys.forEach(key => {
          if (key.charAt(0) !== '_') {
            Object.defineProperty(this, key, {
              get: () => vm[key],
              // set: value => { vm[key] = value },
              set: value => {
                if (!(props && hasOwn(props, key))) {
                  vm[key] = value !== undefined ? value : null
                }
              },
              configurable: true,
            })
          }
        })
      }

      // should be acquired class property values
      const data = new Component()

      // restore original _init to avoid memory leak
      Component.prototype._init = originalInit

      // create plain data object
      const plainData = {}
      Object.keys(data).forEach(key => {
        if (data[key] !== undefined) {
          plainData[key] = data[key]
        }
      })
      return plainData
    }

    function Component <D extends Doraemon>(options: ComponentOptions<D> & ThisType<D>): <DC extends DoraemonClass<D>>(target: DC) => DC
    function Component <DC extends DoraemonClass<Doraemon>>(target: DC): DC
    function Component (options: DoraemonClass<Doraemon> | ComponentOptions<Doraemon>) {
      if (typeof options === 'function') {
        return componentFactory(options)
      }
      return function (Component: DoraemonClass<Doraemon>) {
        return componentFactory(Component, options)
      }
    }

    Component.registerHooks = function registerHooks (keys: string[]): void {
      $internalHooks.push(...keys)
    }

    export {
      Component,
    }