hoist-non-react-statics.cjs.js 2.7 KB

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