1
- import type { Element as ReactElement } from 'react' ;
1
+ import type { ReactElement , ReactNode } from 'react' ;
2
2
import React , { Fragment } from 'react' ;
3
3
import {
4
4
ForwardRef ,
@@ -23,7 +23,9 @@ import type { TreeNode } from './../tree';
23
23
24
24
const supportFragment = Boolean ( Fragment ) ;
25
25
26
- const getFunctionTypeName = ( functionType ) : string => {
26
+ const getFunctionTypeName = (
27
+ functionType : ( ...args : Array < any > ) => any
28
+ ) : string => {
27
29
if ( ! functionType . name || functionType . name === '_default' ) {
28
30
return 'No Display Name' ;
29
31
}
@@ -49,26 +51,32 @@ const getWrappedComponentDisplayName = (Component: any): string => {
49
51
50
52
// heavily inspired by:
51
53
// https://github.com/facebook/react/blob/3746eaf985dd92f8aa5f5658941d07b6b855e9d9/packages/react-devtools-shared/src/backend/renderer.js#L399-L496
52
- const getReactElementDisplayName = ( element : ReactElement < any > ) : string => {
54
+ const getReactElementDisplayName = ( element : ReactElement ) : string => {
53
55
switch ( true ) {
54
56
case typeof element . type === 'string' :
57
+ // @ts -expect-error: flow to TS
55
58
return element . type ;
56
59
57
60
case typeof element . type === 'function' :
61
+ // @ts -expect-error: flow to TS
58
62
if ( element . type . displayName ) {
63
+ // @ts -expect-error: flow to TS
59
64
return element . type . displayName ;
60
65
}
61
66
67
+ // @ts -expect-error: flow to TS
62
68
return getFunctionTypeName ( element . type ) ;
63
69
64
70
case isForwardRef ( element ) :
65
71
case isMemo ( element ) :
66
72
return getWrappedComponentDisplayName ( element . type ) ;
67
73
68
74
case isContextConsumer ( element ) :
75
+ // @ts -expect-error: flow to TS
69
76
return `${ element . type . _context . displayName || 'Context' } .Consumer` ;
70
77
71
78
case isContextProvider ( element ) :
79
+ // @ts -expect-error: flow to TS
72
80
return `${ element . type . _context . displayName || 'Context' } .Provider` ;
73
81
74
82
case isLazy ( element ) :
@@ -88,19 +96,20 @@ const getReactElementDisplayName = (element: ReactElement<any>): string => {
88
96
}
89
97
} ;
90
98
91
- const noChildren = ( propsValue , propName ) => propName !== 'children' ;
99
+ const noChildren = ( propsValue : unknown , propName : string ) =>
100
+ propName !== 'children' ;
92
101
93
- const onlyMeaningfulChildren = ( children ) : boolean =>
102
+ const onlyMeaningfulChildren = ( children : ReactNode ) : boolean =>
94
103
children !== true &&
95
104
children !== false &&
96
105
children !== null &&
97
106
children !== '' ;
98
107
99
108
const filterProps = (
100
- originalProps : { } ,
101
- cb : ( arg0 : any , arg1 : string ) => boolean
102
- ) => {
103
- const filteredProps = { } ;
109
+ originalProps : Record < string , any > ,
110
+ cb : ( propsValue : any , propsName : string ) => boolean
111
+ ) : Record < string , any > => {
112
+ const filteredProps : Record < string , any > = { } ;
104
113
Object . keys ( originalProps )
105
114
. filter ( ( key ) => cb ( originalProps [ key ] , key ) )
106
115
. forEach ( ( key ) => ( filteredProps [ key ] = originalProps [ key ] ) ) ;
@@ -126,7 +135,9 @@ const parseReactElement = (
126
135
const displayName = displayNameFn ( element ) ;
127
136
const props = filterProps ( element . props , noChildren ) ;
128
137
138
+ // @ts -expect-error: flow to TS
129
139
if ( element . ref !== null ) {
140
+ // @ts -expect-error: flow to TS
130
141
props . ref = element . ref ;
131
142
}
132
143
@@ -137,21 +148,17 @@ const parseReactElement = (
137
148
props . key = key ;
138
149
}
139
150
151
+ // @ts -expect-error: flow to TS
140
152
const defaultProps = filterProps ( element . type . defaultProps || { } , noChildren ) ;
141
- const childrens = React . Children . toArray ( element . props . children )
153
+ const children = React . Children . toArray ( element . props . children )
142
154
. filter ( onlyMeaningfulChildren )
143
- . map ( ( child ) => parseReactElement ( child , options ) ) ;
155
+ . map ( ( child : ReactElement < any > ) => parseReactElement ( child , options ) ) ;
144
156
145
157
if ( supportFragment && element . type === Fragment ) {
146
- return createReactFragmentTreeNode ( key , childrens ) ;
158
+ return createReactFragmentTreeNode ( key , children ) ;
147
159
}
148
160
149
- return createReactElementTreeNode (
150
- displayName ,
151
- props ,
152
- defaultProps ,
153
- childrens
154
- ) ;
161
+ return createReactElementTreeNode ( displayName , props , defaultProps , children ) ;
155
162
} ;
156
163
157
164
export default parseReactElement ;
0 commit comments