Projekt

Obecné

Profil

Stáhnout (2.68 KB) Statistiky
| Větev: | Revize:
1
'use strict';
2

    
3
var reactIs = require('react-is');
4

    
5
/**
6
 * Copyright 2015, Yahoo! Inc.
7
 * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
8
 */
9
var REACT_STATICS = {
10
  childContextTypes: true,
11
  contextType: true,
12
  contextTypes: true,
13
  defaultProps: true,
14
  displayName: true,
15
  getDefaultProps: true,
16
  getDerivedStateFromError: true,
17
  getDerivedStateFromProps: true,
18
  mixins: true,
19
  propTypes: true,
20
  type: true
21
};
22
var KNOWN_STATICS = {
23
  name: true,
24
  length: true,
25
  prototype: true,
26
  caller: true,
27
  callee: true,
28
  arguments: true,
29
  arity: true
30
};
31
var FORWARD_REF_STATICS = {
32
  '$$typeof': true,
33
  render: true,
34
  defaultProps: true,
35
  displayName: true,
36
  propTypes: true
37
};
38
var MEMO_STATICS = {
39
  '$$typeof': true,
40
  compare: true,
41
  defaultProps: true,
42
  displayName: true,
43
  propTypes: true,
44
  type: true
45
};
46
var TYPE_STATICS = {};
47
TYPE_STATICS[reactIs.ForwardRef] = FORWARD_REF_STATICS;
48
TYPE_STATICS[reactIs.Memo] = MEMO_STATICS;
49

    
50
function getStatics(component) {
51
  // React v16.11 and below
52
  if (reactIs.isMemo(component)) {
53
    return MEMO_STATICS;
54
  } // React v16.12 and above
55

    
56

    
57
  return TYPE_STATICS[component['$$typeof']] || REACT_STATICS;
58
}
59

    
60
var defineProperty = Object.defineProperty;
61
var getOwnPropertyNames = Object.getOwnPropertyNames;
62
var getOwnPropertySymbols = Object.getOwnPropertySymbols;
63
var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
64
var getPrototypeOf = Object.getPrototypeOf;
65
var objectPrototype = Object.prototype;
66
function hoistNonReactStatics(targetComponent, sourceComponent, blacklist) {
67
  if (typeof sourceComponent !== 'string') {
68
    // don't hoist over string (html) components
69
    if (objectPrototype) {
70
      var inheritedComponent = getPrototypeOf(sourceComponent);
71

    
72
      if (inheritedComponent && inheritedComponent !== objectPrototype) {
73
        hoistNonReactStatics(targetComponent, inheritedComponent, blacklist);
74
      }
75
    }
76

    
77
    var keys = getOwnPropertyNames(sourceComponent);
78

    
79
    if (getOwnPropertySymbols) {
80
      keys = keys.concat(getOwnPropertySymbols(sourceComponent));
81
    }
82

    
83
    var targetStatics = getStatics(targetComponent);
84
    var sourceStatics = getStatics(sourceComponent);
85

    
86
    for (var i = 0; i < keys.length; ++i) {
87
      var key = keys[i];
88

    
89
      if (!KNOWN_STATICS[key] && !(blacklist && blacklist[key]) && !(sourceStatics && sourceStatics[key]) && !(targetStatics && targetStatics[key])) {
90
        var descriptor = getOwnPropertyDescriptor(sourceComponent, key);
91

    
92
        try {
93
          // Avoid failures from read-only properties
94
          defineProperty(targetComponent, key, descriptor);
95
        } catch (e) {}
96
      }
97
    }
98
  }
99

    
100
  return targetComponent;
101
}
102

    
103
module.exports = hoistNonReactStatics;
(1-1/3)