UNPKG

2.14 kBJavaScriptView Raw
1/**
2 * Copyright 2015, Yahoo! Inc.
3 * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
4 */
5'use strict';
6
7var REACT_STATICS = {
8 childContextTypes: true,
9 contextTypes: true,
10 defaultProps: true,
11 displayName: true,
12 getDefaultProps: true,
13 mixins: true,
14 propTypes: true,
15 type: true
16};
17
18var KNOWN_STATICS = {
19 name: true,
20 length: true,
21 prototype: true,
22 caller: true,
23 callee: true,
24 arguments: true,
25 arity: true
26};
27
28var getOwnPropertySymbols = Object.getOwnPropertySymbols;
29var hasOwnProperty = Object.prototype.hasOwnProperty;
30var propIsEnumerable = Object.prototype.propertyIsEnumerable;
31var getPrototypeOf = Object.getPrototypeOf;
32var objectPrototype = getPrototypeOf && getPrototypeOf(Object);
33var getOwnPropertyNames = Object.getOwnPropertyNames;
34
35module.exports = function hoistNonReactStatics(targetComponent, sourceComponent, blacklist) {
36 if (typeof sourceComponent !== 'string') { // don't hoist over string (html) components
37
38 if (objectPrototype) {
39 var inheritedComponent = getPrototypeOf(sourceComponent);
40 if (inheritedComponent && inheritedComponent !== objectPrototype) {
41 hoistNonReactStatics(targetComponent, inheritedComponent, blacklist);
42 }
43 }
44
45 var keys = getOwnPropertyNames(sourceComponent);
46
47 if (getOwnPropertySymbols) {
48 keys = keys.concat(getOwnPropertySymbols(sourceComponent));
49 }
50
51 for (var i = 0; i < keys.length; ++i) {
52 var key = keys[i];
53 if (!REACT_STATICS[key] && !KNOWN_STATICS[key] && (!blacklist || !blacklist[key])) {
54 // Only hoist enumerables and non-enumerable functions
55 if(propIsEnumerable.call(sourceComponent, key) || typeof sourceComponent[key] === 'function') {
56 try { // Avoid failures from read-only properties
57 targetComponent[key] = sourceComponent[key];
58 } catch (e) {}
59 }
60 }
61 }
62
63 return targetComponent;
64 }
65
66 return targetComponent;
67};