File tree 2 files changed +31
-1
lines changed
2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -171,6 +171,25 @@ const traversalMethods = {
171
171
} ) ;
172
172
return Collection . fromPaths ( paths , this , JSXElement ) ;
173
173
} ,
174
+
175
+ /**
176
+ * Returns all children that are of jsxElementType.
177
+ *
178
+ * @return {Collection<jsxElementType> }
179
+ */
180
+ childNodesOfType : function ( jsxChildElementType ) {
181
+ const paths = [ ] ;
182
+ this . forEach ( function ( path ) {
183
+ const children = path . get ( 'children' ) ;
184
+ const l = children . value . length ;
185
+ for ( let i = 0 ; i < l ; i ++ ) {
186
+ if ( jsxChildElementType . check ( children . value [ i ] ) ) {
187
+ paths . push ( children . get ( i ) ) ;
188
+ }
189
+ }
190
+ } ) ;
191
+ return Collection . fromPaths ( paths , this , jsxChildElementType ) ;
192
+ } ,
174
193
} ;
175
194
176
195
const mappingMethods = {
Original file line number Diff line number Diff line change @@ -37,6 +37,7 @@ describe('JSXCollection API', function() {
37
37
' <Baz.Bar />' ,
38
38
' </Child>' ,
39
39
' <Child id="2" foo="baz" baz/>' ,
40
+ ' {"foo"}' ,
40
41
'</FooBar>'
41
42
] . join ( '\n' ) , { parser : getParser ( ) } ) . program ] ;
42
43
} ) ;
@@ -65,7 +66,7 @@ describe('JSXCollection API', function() {
65
66
Collection . fromNodes ( nodes )
66
67
. findJSXElements ( 'FooBar' )
67
68
. childNodes ( ) ;
68
- expect ( children . length ) . toBe ( 5 ) ;
69
+ expect ( children . length ) . toBe ( 7 ) ;
69
70
expect ( children . getTypes ( ) ) . toContain ( 'Expression' ) ;
70
71
} ) ;
71
72
@@ -79,6 +80,16 @@ describe('JSXCollection API', function() {
79
80
expect ( children . getTypes ( ) ) . toContain ( 'JSXElement' ) ;
80
81
} ) ;
81
82
83
+ it ( 'returns the child element types of an JSXElement' , function ( ) {
84
+ const children =
85
+ Collection . fromNodes ( nodes )
86
+ . findJSXElements ( 'FooBar' )
87
+ . childNodesOfType ( types . JSXExpressionContainer ) ;
88
+
89
+ expect ( children . length ) . toBe ( 1 ) ;
90
+ expect ( children . getTypes ( ) ) . toContain ( 'JSXExpressionContainer' ) ;
91
+ } ) ;
92
+
82
93
it ( 'returns a properly typed collection even if empty' , function ( ) {
83
94
const children =
84
95
Collection . fromNodes ( [ ] )
You can’t perform that action at this time.
0 commit comments