@@ -10,6 +10,23 @@ function isReactFragment(openingElement) {
1010 )
1111}
1212
13+ function applyAttribute ( { openingElement, t, name, options } ) {
14+ if ( ! openingElement || isReactFragment ( openingElement ) ) return
15+
16+ const isAttributeAlreadySet = openingElement . node . attributes . find (
17+ node => node . name . name === options . attribute
18+ )
19+
20+ if ( isAttributeAlreadySet ) return
21+
22+ openingElement . node . attributes . push (
23+ t . jSXAttribute (
24+ t . jSXIdentifier ( options . attribute ) ,
25+ t . stringLiteral ( options . format ( name ) )
26+ )
27+ )
28+ }
29+
1330function functionBodyPushAttributes ( t , path , options , componentName ) {
1431 let openingElement = null
1532 const functionBody = path . get ( 'body' ) . get ( 'body' )
@@ -31,15 +48,7 @@ function functionBodyPushAttributes(t, path, options, componentName) {
3148 openingElement = arg . get ( 'openingElement' )
3249 }
3350
34- if ( ! openingElement ) return
35- if ( isReactFragment ( openingElement ) ) return
36-
37- openingElement . node . attributes . push (
38- t . jSXAttribute (
39- t . jSXIdentifier ( options . attribute ) ,
40- t . stringLiteral ( options . format ( componentName ) )
41- )
42- )
51+ applyAttribute ( { openingElement, t, name : componentName , options } )
4352}
4453
4554export default function ( { types : t } ) {
@@ -84,14 +93,13 @@ export default function({ types: t }) {
8493 if ( ! arg . isJSXElement ( ) ) return
8594
8695 const openingElement = arg . get ( 'openingElement' )
87- if ( isReactFragment ( openingElement ) ) return
88-
89- openingElement . node . attributes . push (
90- t . jSXAttribute (
91- t . jSXIdentifier ( options . attribute ) ,
92- t . stringLiteral ( options . format ( name . node && name . node . name ) )
93- )
94- )
96+
97+ applyAttribute ( {
98+ openingElement,
99+ t,
100+ name : name . node && name . node . name ,
101+ options
102+ } )
95103 }
96104 } )
97105 }
0 commit comments