source: node_modules/react-redux/src/utils/hoistStatics.ts@ a762898

Last change on this file since a762898 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 3.6 KB
Line 
1// Copied directly from:
2// https://github.com/mridgway/hoist-non-react-statics/blob/main/src/index.js
3// https://unpkg.com/browse/@types/hoist-non-react-statics@3.3.6/index.d.ts
4
5/**
6 * Copyright 2015, Yahoo! Inc.
7 * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
8 */
9import type { ForwardRefExoticComponent, MemoExoticComponent } from 'react'
10import { ForwardRef, Memo, isMemo } from '../utils/react-is'
11
12const 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} as const
25
26const KNOWN_STATICS = {
27 name: true,
28 length: true,
29 prototype: true,
30 caller: true,
31 callee: true,
32 arguments: true,
33 arity: true,
34} as const
35
36const FORWARD_REF_STATICS = {
37 $$typeof: true,
38 render: true,
39 defaultProps: true,
40 displayName: true,
41 propTypes: true,
42} as const
43
44const MEMO_STATICS = {
45 $$typeof: true,
46 compare: true,
47 defaultProps: true,
48 displayName: true,
49 propTypes: true,
50 type: true,
51} as const
52
53const TYPE_STATICS = {
54 [ForwardRef]: FORWARD_REF_STATICS,
55 [Memo]: MEMO_STATICS,
56} as const
57
58function getStatics(component: any) {
59 // React v16.11 and below
60 if (isMemo(component)) {
61 return MEMO_STATICS
62 }
63
64 // React v16.12 and above
65 return TYPE_STATICS[component['$$typeof']] || REACT_STATICS
66}
67
68export type NonReactStatics<
69 Source,
70 C extends {
71 [key: string]: true
72 } = {},
73> = {
74 [key in Exclude<
75 keyof Source,
76 Source extends MemoExoticComponent<any>
77 ? keyof typeof MEMO_STATICS | keyof C
78 : Source extends ForwardRefExoticComponent<any>
79 ? keyof typeof FORWARD_REF_STATICS | keyof C
80 : keyof typeof REACT_STATICS | keyof typeof KNOWN_STATICS | keyof C
81 >]: Source[key]
82}
83
84const defineProperty = Object.defineProperty
85const getOwnPropertyNames = Object.getOwnPropertyNames
86const getOwnPropertySymbols = Object.getOwnPropertySymbols
87const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor
88const getPrototypeOf = Object.getPrototypeOf
89const objectPrototype = Object.prototype
90
91export default function hoistNonReactStatics<
92 Target,
93 Source,
94 CustomStatic extends {
95 [key: string]: true
96 } = {},
97>(
98 targetComponent: Target,
99 sourceComponent: Source,
100): Target & NonReactStatics<Source, CustomStatic> {
101 if (typeof sourceComponent !== 'string') {
102 // don't hoist over string (html) components
103
104 if (objectPrototype) {
105 const inheritedComponent = getPrototypeOf(sourceComponent)
106 if (inheritedComponent && inheritedComponent !== objectPrototype) {
107 hoistNonReactStatics(targetComponent, inheritedComponent)
108 }
109 }
110
111 let keys: (string | symbol)[] = getOwnPropertyNames(sourceComponent)
112
113 if (getOwnPropertySymbols) {
114 keys = keys.concat(getOwnPropertySymbols(sourceComponent))
115 }
116
117 const targetStatics = getStatics(targetComponent)
118 const sourceStatics = getStatics(sourceComponent)
119
120 for (let i = 0; i < keys.length; ++i) {
121 const key = keys[i]
122 if (
123 !KNOWN_STATICS[key as keyof typeof KNOWN_STATICS] &&
124 !(sourceStatics && sourceStatics[key as keyof typeof sourceStatics]) &&
125 !(targetStatics && targetStatics[key as keyof typeof targetStatics])
126 ) {
127 const descriptor = getOwnPropertyDescriptor(sourceComponent, key)
128 try {
129 // Avoid failures from read-only properties
130 defineProperty(targetComponent, key, descriptor!)
131 } catch (e) {
132 // ignore
133 }
134 }
135 }
136 }
137
138 return targetComponent as any
139}
Note: See TracBrowser for help on using the repository browser.