@@ -46,17 +46,18 @@ const getJSXAttributeValue = (
46
46
const transformJSXSpreadAttribute = (
47
47
nodePath : NodePath ,
48
48
path : NodePath < t . JSXSpreadAttribute > ,
49
- mergeArgs : ( t . ObjectProperty | t . Expression ) [ ] ,
49
+ mergeProps : boolean ,
50
+ args : ( t . ObjectProperty | t . Expression | t . SpreadElement ) [ ] ,
50
51
) => {
51
52
const argument = path . get ( 'argument' ) as NodePath < t . ObjectExpression > ;
52
53
const { properties } = argument . node ;
53
54
if ( ! properties ) {
54
55
if ( argument . isIdentifier ( ) ) {
55
56
walksScope ( nodePath , ( argument . node as t . Identifier ) . name , SlotFlags . DYNAMIC ) ;
56
57
}
57
- mergeArgs . push ( argument . node ) ;
58
+ args . push ( mergeProps ? argument . node : t . spreadElement ( argument . node ) ) ;
58
59
} else {
59
- mergeArgs . push ( t . objectExpression ( properties ) ) ;
60
+ args . push ( t . objectExpression ( properties ) ) ;
60
61
}
61
62
} ;
62
63
@@ -71,7 +72,10 @@ const mergeAsArray = (existing: t.ObjectProperty, incoming: t.ObjectProperty) =>
71
72
}
72
73
} ;
73
74
74
- const dedupeProperties = ( properties : t . ObjectProperty [ ] = [ ] ) => {
75
+ const dedupeProperties = ( properties : t . ObjectProperty [ ] = [ ] , mergeProps ?: boolean ) => {
76
+ if ( ! mergeProps ) {
77
+ return properties ;
78
+ }
75
79
const knownProps = new Map < string , t . ObjectProperty > ( ) ;
76
80
const deduped : t . ObjectProperty [ ] = [ ] ;
77
81
properties . forEach ( ( prop ) => {
@@ -146,6 +150,7 @@ const buildProps = (path: NodePath<t.JSXElement>, state: State) => {
146
150
let hasDynamicKeys = false ;
147
151
148
152
const mergeArgs : ( t . CallExpression | t . ObjectExpression | t . Identifier ) [ ] = [ ] ;
153
+ const { mergeProps = true } = state . opts ;
149
154
props
150
155
. forEach ( ( prop ) => {
151
156
if ( prop . isJSXAttribute ( ) ) {
@@ -273,8 +278,8 @@ const buildProps = (path: NodePath<t.JSXElement>, state: State) => {
273
278
) ) ;
274
279
}
275
280
} else {
276
- if ( properties . length ) {
277
- mergeArgs . push ( t . objectExpression ( dedupeProperties ( properties ) ) ) ;
281
+ if ( properties . length && mergeProps ) {
282
+ mergeArgs . push ( t . objectExpression ( dedupeProperties ( properties , mergeProps ) ) ) ;
278
283
properties = [ ] ;
279
284
}
280
285
@@ -283,7 +288,8 @@ const buildProps = (path: NodePath<t.JSXElement>, state: State) => {
283
288
transformJSXSpreadAttribute (
284
289
path as NodePath ,
285
290
prop as NodePath < t . JSXSpreadAttribute > ,
286
- mergeArgs ,
291
+ mergeProps ,
292
+ mergeProps ? mergeArgs : properties ,
287
293
) ;
288
294
}
289
295
} ) ;
@@ -314,10 +320,9 @@ const buildProps = (path: NodePath<t.JSXElement>, state: State) => {
314
320
}
315
321
316
322
let propsExpression : t . Expression | t . ObjectProperty | t . Literal = t . nullLiteral ( ) ;
317
-
318
323
if ( mergeArgs . length ) {
319
324
if ( properties . length ) {
320
- mergeArgs . push ( t . objectExpression ( dedupeProperties ( properties ) ) ) ;
325
+ mergeArgs . push ( t . objectExpression ( dedupeProperties ( properties , mergeProps ) ) ) ;
321
326
}
322
327
if ( mergeArgs . length > 1 ) {
323
328
propsExpression = t . callExpression (
@@ -329,7 +334,12 @@ const buildProps = (path: NodePath<t.JSXElement>, state: State) => {
329
334
propsExpression = mergeArgs [ 0 ] ;
330
335
}
331
336
} else if ( properties . length ) {
332
- propsExpression = t . objectExpression ( dedupeProperties ( properties ) ) ;
337
+ // single no need for spread
338
+ if ( properties . length === 1 && t . isSpreadElement ( properties [ 0 ] ) ) {
339
+ propsExpression = ( properties [ 0 ] as unknown as t . SpreadElement ) . argument ;
340
+ } else {
341
+ propsExpression = t . objectExpression ( dedupeProperties ( properties , mergeProps ) ) ;
342
+ }
333
343
}
334
344
335
345
return {
0 commit comments