@@ -6,69 +6,66 @@ import isObjectAssigning from 'utils/isObjectAssigning'
66import appendObject from 'utils/magicString/appendObject'
77import insertToObject from 'utils/magicString/insertToObject'
88import overwriteWithObject from 'utils/magicString/overwriteWithObject'
9- import isReactNode from 'utils/magicString/ react/isReactNode'
9+ import isReactNode from 'utils/react/isReactNode'
1010
1111type Params = {
1212 node : Record < string , any >
13- parent : Record < string , any >
1413 code : MagicString
1514 componentName : string
1615}
1716
18- export default function injectReactFunctionComponent ( {
19- node,
20- parent,
21- code,
22- componentName,
23- } : Params ) {
24- if ( isReactNode ( node ) && parent . type !== 'CallExpression' ) {
25- const tagProps = node . arguments [ 1 ]
17+ export default function injectReactFunctionComponent ( { node, code, componentName } : Params ) {
18+ if ( ! isReactNode ( node ) ) return false
2619
27- if ( tagProps ?. start || ! isEmpty ( tagProps . properties ) ) {
28- const hasProps = tagProps . value !== null // NOTE: if a component has props, the value will always be undefined
20+ const tagProps = node . arguments [ 1 ]
2921
30- if ( hasProps ) {
31- // e.g `<svg>...</svg>`
32- if ( isObjectAssigning ( tagProps ) ) {
33- const firstArgs = head ( tagProps . arguments as Record < string , any > [ ] )
22+ if ( ! tagProps ?. start && isEmpty ( tagProps . properties ) ) return false
3423
35- firstArgs &&
36- insertToObject ( {
37- code,
38- node : firstArgs ,
39- attrs : { [ DATA_QA ] : componentName } ,
40- } )
41- } else {
42- // e.g `<div />`
43- if ( isEmpty ( tagProps . properties ) ) {
44- insertToObject ( {
45- code,
46- node : tagProps ,
47- attrs : { [ DATA_QA ] : componentName } ,
48- } )
49- } else {
50- const props = head ( tagProps . properties as Record < string , any > [ ] )
24+ // NOTE: if a component has props, the value will always be undefined
25+ const hasProps = tagProps . value !== null
5126
52- props &&
53- appendObject ( {
54- code,
55- startPosition : props . start ,
56- attrs : { [ DATA_QA ] : componentName } ,
57- } )
58- }
59- }
60- } else {
61- overwriteWithObject ( {
62- code,
63- startPosition : tagProps . start ,
64- endPosition : tagProps . end ,
65- attrs : { [ DATA_QA ] : componentName } ,
66- } )
67- }
27+ if ( ! hasProps ) {
28+ overwriteWithObject ( {
29+ code,
30+ startPosition : tagProps . start ,
31+ endPosition : tagProps . end ,
32+ attrs : { [ DATA_QA ] : componentName } ,
33+ } )
34+ return true
35+ }
36+
37+ // e.g `<svg>...</svg>`
38+ if ( isObjectAssigning ( tagProps ) ) {
39+ const firstArgs = head ( tagProps . arguments as Record < string , any > [ ] )
6840
69- return true
70- }
41+ if ( ! firstArgs ) return false
42+
43+ insertToObject ( {
44+ code,
45+ node : firstArgs ,
46+ attrs : { [ DATA_QA ] : componentName } ,
47+ } )
48+ return true
7149 }
7250
73- return false
51+ // e.g `<div />`
52+ if ( isEmpty ( tagProps . properties ) ) {
53+ insertToObject ( {
54+ code,
55+ node : tagProps ,
56+ attrs : { [ DATA_QA ] : componentName } ,
57+ } )
58+ return true
59+ }
60+
61+ const props = head ( tagProps . properties as Record < string , any > [ ] )
62+
63+ if ( ! props ) return false
64+
65+ appendObject ( {
66+ code,
67+ startPosition : props . start ,
68+ attrs : { [ DATA_QA ] : componentName } ,
69+ } )
70+ return true
7471}
0 commit comments