UNPKG

2.18 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 getOwnPropertySymbols = Object.getOwnPropertySymbols;
19var hasOwnProperty = Object.prototype.hasOwnProperty;
20var propIsEnumerable = Object.prototype.propertyIsEnumerable;
21var getPrototypeOf = Object.getPrototypeOf;
22var objectPrototype = getPrototypeOf && getPrototypeOf(Object);
23
24module.exports = function hoistNonReactStatics(targetComponent, sourceComponent, blacklist) {
25 if (typeof sourceComponent !== 'string') { // don't hoist over string (html) components
26
27 if (objectPrototype) {
28 var inheritedComponent = getPrototypeOf(sourceComponent);
29 if (inheritedComponent && inheritedComponent !== objectPrototype) {
30 hoistNonReactStatics(targetComponent, inheritedComponent, blacklist);
31 }
32 }
33
34 for (var key in sourceComponent) {
35 if (!REACT_STATICS[key] && (!blacklist || !blacklist[key])) {
36 if (hasOwnProperty.call(sourceComponent, key)) {
37 try { // Avoid failures from read-only properties
38 targetComponent[key] = sourceComponent[key];
39 } catch (e) {}
40 }
41 }
42 }
43
44 if (getOwnPropertySymbols) {
45 var symbols = getOwnPropertySymbols(sourceComponent);
46 for (var i = 0; i < symbols.length; i++) {
47 if (!REACT_STATICS[symbols[i]] && (!blacklist || !blacklist[symbols[i]])) {
48 if (propIsEnumerable.call(sourceComponent, symbols[i])) {
49 try { // Avoid failures from read-only properties
50 targetComponent[symbols[i]] = sourceComponent[symbols[i]];
51 } catch(e) {}
52 }
53 }
54 }
55 }
56
57 return targetComponent;
58 }
59
60 return targetComponent;
61};