UNPKG

3.14 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3 typeof define === 'function' && define.amd ? define(factory) :
4 (global.hoistNonReactStatics = factory());
5}(this, (function () { 'use strict';
6
7/**
8 * Copyright 2015, Yahoo! Inc.
9 * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
10 */
11var ReactIs = require('react-is');
12var REACT_STATICS = {
13 childContextTypes: true,
14 contextType: true,
15 contextTypes: true,
16 defaultProps: true,
17 displayName: true,
18 getDefaultProps: true,
19 getDerivedStateFromError: true,
20 getDerivedStateFromProps: true,
21 mixins: true,
22 propTypes: true,
23 type: true
24};
25
26var KNOWN_STATICS = {
27 name: true,
28 length: true,
29 prototype: true,
30 caller: true,
31 callee: true,
32 arguments: true,
33 arity: true
34};
35
36var FORWARD_REF_STATICS = {
37 '$$typeof': true,
38 render: true,
39 defaultProps: true,
40 displayName: true,
41 propTypes: true
42};
43
44var MEMO_STATICS = {
45 '$$typeof': true,
46 compare: true,
47 defaultProps: true,
48 displayName: true,
49 propTypes: true,
50 type: true
51};
52
53var TYPE_STATICS = {};
54TYPE_STATICS[ReactIs.ForwardRef] = FORWARD_REF_STATICS;
55
56function getStatics(component) {
57 if (ReactIs.isMemo(component)) {
58 return MEMO_STATICS;
59 }
60 return TYPE_STATICS[component['$$typeof']] || REACT_STATICS;
61}
62
63var defineProperty = Object.defineProperty;
64var getOwnPropertyNames = Object.getOwnPropertyNames;
65var getOwnPropertySymbols = Object.getOwnPropertySymbols;
66var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
67var getPrototypeOf = Object.getPrototypeOf;
68var objectPrototype = Object.prototype;
69
70function hoistNonReactStatics(targetComponent, sourceComponent, blacklist) {
71 if (typeof sourceComponent !== 'string') {
72 // don't hoist over string (html) components
73
74 if (objectPrototype) {
75 var inheritedComponent = getPrototypeOf(sourceComponent);
76 if (inheritedComponent && inheritedComponent !== objectPrototype) {
77 hoistNonReactStatics(targetComponent, inheritedComponent, blacklist);
78 }
79 }
80
81 var keys = getOwnPropertyNames(sourceComponent);
82
83 if (getOwnPropertySymbols) {
84 keys = keys.concat(getOwnPropertySymbols(sourceComponent));
85 }
86
87 var targetStatics = getStatics(targetComponent);
88 var sourceStatics = getStatics(sourceComponent);
89
90 for (var i = 0; i < keys.length; ++i) {
91 var key = keys[i];
92 if (!KNOWN_STATICS[key] && !(blacklist && blacklist[key]) && !(sourceStatics && sourceStatics[key]) && !(targetStatics && targetStatics[key])) {
93 var descriptor = getOwnPropertyDescriptor(sourceComponent, key);
94 try {
95 // Avoid failures from read-only properties
96 defineProperty(targetComponent, key, descriptor);
97 } catch (e) {}
98 }
99 }
100
101 return targetComponent;
102 }
103
104 return targetComponent;
105}
106
107return hoistNonReactStatics;
108
109})));