Skip to content

Commit 6e422a2

Browse files
authored
Change to use spread syntax for shallow clones
Closes GH-8. Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent 6c35b60 commit 6e422a2

File tree

2 files changed

+177
-234
lines changed

2 files changed

+177
-234
lines changed

lib/index.js

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,9 @@ export function buildJsx(tree, options) {
281281

282282
/** @type {MemberExpression | Literal | Identifier} */
283283
let name
284-
/** @type {Array<Property>} */
285-
let fields = []
284+
/** @type {Array<Property | SpreadElement>} */
285+
const fields = []
286286
/** @type {Array<Expression>} */
287-
const objects = []
288-
/** @type {Array<Expression | SpreadElement>} */
289287
let parameters = []
290288
/** @type {Expression | undefined} */
291289
let key
@@ -314,12 +312,12 @@ export function buildJsx(tree, options) {
314312
const attribute = attributes[index]
315313

316314
if (attribute.type === 'JSXSpreadAttribute') {
317-
if (fields.length > 0) {
318-
objects.push({type: 'ObjectExpression', properties: fields})
319-
fields = []
315+
if (attribute.argument.type === 'ObjectExpression') {
316+
fields.push(...attribute.argument.properties)
317+
} else {
318+
fields.push({type: 'SpreadElement', argument: attribute.argument})
320319
}
321320

322-
objects.push(attribute.argument)
323321
spread = true
324322
} else {
325323
const prop = toProperty(attribute)
@@ -373,33 +371,11 @@ export function buildJsx(tree, options) {
373371
parameters = children
374372
}
375373

376-
if (fields.length > 0) {
377-
objects.push({type: 'ObjectExpression', properties: fields})
378-
}
379-
380-
/** @type {Expression | undefined} */
381-
let props
382374
/** @type {MemberExpression | Literal | Identifier} */
383375
let callee
384376

385-
if (objects.length > 1) {
386-
// Don’t mutate the first object, shallow clone instead.
387-
if (objects[0].type !== 'ObjectExpression') {
388-
objects.unshift({type: 'ObjectExpression', properties: []})
389-
}
390-
391-
props = {
392-
type: 'CallExpression',
393-
callee: toMemberExpression('Object.assign'),
394-
arguments: objects,
395-
optional: false
396-
}
397-
} else if (objects.length > 0) {
398-
props = objects[0]
399-
}
400-
401377
if (automatic) {
402-
parameters.push(props || {type: 'ObjectExpression', properties: []})
378+
parameters.push({type: 'ObjectExpression', properties: fields})
403379

404380
if (key) {
405381
parameters.push(key)
@@ -470,9 +446,10 @@ export function buildJsx(tree, options) {
470446
}
471447
// Classic.
472448
else {
473-
// There are props or children.
474-
if (props || parameters.length > 0) {
475-
parameters.unshift(props || {type: 'Literal', value: null})
449+
if (fields.length > 0) {
450+
parameters.unshift({type: 'ObjectExpression', properties: fields})
451+
} else if (parameters.length > 0) {
452+
parameters.unshift({type: 'Literal', value: null})
476453
}
477454

478455
callee = toMemberExpression(

0 commit comments

Comments
 (0)