UNPKG

2.89 kBJavaScriptView Raw
1'use strict';
2
3/**
4 * Copyright 2015, Yahoo! Inc.
5 * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
6 */
7var ReactIs = require('react-is');
8var REACT_STATICS = {
9 childContextTypes: true,
10 contextType: true,
11 contextTypes: true,
12 defaultProps: true,
13 displayName: true,
14 getDefaultProps: true,
15 getDerivedStateFromError: true,
16 getDerivedStateFromProps: true,
17 mixins: true,
18 propTypes: true,
19 type: true
20};
21
22var 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
32var FORWARD_REF_STATICS = {
33 '$$typeof': true,
34 render: true,
35 defaultProps: true,
36 displayName: true,
37 propTypes: true
38};
39
40var MEMO_STATICS = {
41 '$$typeof': true,
42 compare: true,
43 defaultProps: true,
44 displayName: true,
45 propTypes: true,
46 type: true
47};
48
49var TYPE_STATICS = {};
50TYPE_STATICS[ReactIs.ForwardRef] = FORWARD_REF_STATICS;
51
52function getStatics(component) {
53 if (ReactIs.isMemo(component)) {
54 return MEMO_STATICS;
55 }
56 return TYPE_STATICS[component['$$typeof']] || REACT_STATICS;
57}
58
59var defineProperty = Object.defineProperty;
60var getOwnPropertyNames = Object.getOwnPropertyNames;
61var getOwnPropertySymbols = Object.getOwnPropertySymbols;
62var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
63var getPrototypeOf = Object.getPrototypeOf;
64var objectPrototype = Object.prototype;
65
66function hoistNonReactStatics(targetComponent, sourceComponent, blacklist) {
67 if (typeof sourceComponent !== 'string') {
68 // don't hoist over string (html) components
69
70 if (objectPrototype) {
71 var inheritedComponent = getPrototypeOf(sourceComponent);
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 if (!KNOWN_STATICS[key] && !(blacklist && blacklist[key]) && !(sourceStatics && sourceStatics[key]) && !(targetStatics && targetStatics[key])) {
89 var descriptor = getOwnPropertyDescriptor(sourceComponent, key);
90 try {
91 // Avoid failures from read-only properties
92 defineProperty(targetComponent, key, descriptor);
93 } catch (e) {}
94 }
95 }
96
97 return targetComponent;
98 }
99
100 return targetComponent;
101}
102
103module.exports = hoistNonReactStatics;