Skip to content

Commit 3dcfd2b

Browse files
committed
fix some super edge-casey edge cases
1 parent dd38e02 commit 3dcfd2b

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

src/serialiser.coffee

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ class Serialiser
4747
flushPairs = =>
4848
if pairAttrsBuffer.length
4949
serialisedChild = @serialiseAttributePairs(pairAttrsBuffer)
50-
assigns.push(serialisedChild) if serialisedChild # skip null
50+
if serialisedChild
51+
assigns.push(serialisedChild)
52+
else
53+
assigns.push(type: $.CJSX_WHITESPACE, value: pairAttrsBuffer.map(@serialiseNode.bind(this)).join('').replace('\n', '\\\n'))
5154
pairAttrsBuffer = [] # reset buffer
5255

5356
if firstNonWhitespaceChild(children)?.type is $.CJSX_ATTR_SPREAD
@@ -62,7 +65,20 @@ class Serialiser
6265

6366
flushPairs()
6467

65-
"React.__spread(#{joinList(assigns)})"
68+
accumulatedWhitespace = ''
69+
assignsWithWhitespace = []
70+
for assignItem, assignIndex in assigns
71+
if typeof assignItem is 'object' and assignItem?.type is $.CJSX_WHITESPACE
72+
accumulatedWhitespace += assignItem.value
73+
if typeof assignItem is 'string'
74+
assignsWithWhitespace.push accumulatedWhitespace+assignItem
75+
accumulatedWhitespace = ''
76+
77+
if assignsWithWhitespace.length
78+
lastAssignWithWhitespace = assignsWithWhitespace.pop()
79+
assignsWithWhitespace.push lastAssignWithWhitespace+accumulatedWhitespace
80+
81+
"React.__spread(#{joinList(assignsWithWhitespace)})"
6682

6783
serialiseAttributePairs: (children) ->
6884
# whitespace (particularly newlines) must be maintained

test/output-testcases.txt

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -538,18 +538,17 @@ complex spread attribute
538538
##input
539539
<Component {...x} a={b} {... x } b="c" {...$my_xtraCoolVar123 } />
540540
##expected
541-
React.createElement(Component, React.__spread({}, x, {"a": (b)}, x , {"b": "c"}, $my_xtraCoolVar123 ))
541+
React.createElement(Component, React.__spread({}, x, {"a": (b)}, x , {"b": "c"}, $my_xtraCoolVar123 ))
542542
##end
543543

544544
##desc
545545
multiline spread attribute
546546
##input
547-
<Component
548-
{...
547+
<Component {...
549548
x } a={b} {... x } b="c" {...z }>
550549
</Component>
551550
##expected
552-
React.createElement(Component, React.__spread({},
551+
React.createElement(Component, React.__spread({},
553552
x , {"a": (b)}, x , {"b": "c"}, z )
554553
)
555554
##end
@@ -573,16 +572,39 @@ React.createElement(Component, React.__spread({ \
573572
})
574573
)
575574
##end
575+
##desc
576+
multiline tag with spread attribute first
577+
##input
578+
<Component
579+
{...
580+
x}
581+
z="1"
582+
a={b}
583+
b="c"
584+
>
585+
</Component>
586+
##expected
587+
React.createElement(Component, React.__spread({}, \
588+
589+
x, { \
590+
"z": "1", \
591+
"a": (b), \
592+
"b": "c"
593+
})
594+
)
595+
##end
596+
576597
##desc
577598
complex multiline spread attribute
578599
##input
579600
<Component
580-
{...
601+
{...
581602
y} a={b} {... x } b="c" {...z }>
582603
<div code={someFunc({a:{b:{}, C:'}'}})} />
583604
</Component>
584605
##expected
585-
React.createElement(Component, React.__spread({},
606+
React.createElement(Component, React.__spread({}, \
607+
586608
y, {"a": (b)}, x , {"b": "c"}, z ),
587609
React.createElement("div", {"code": (someFunc({a:{b:{}, C:'}'}}))})
588610
)

test/test.coffee

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ tryCompile = (input, desc) ->
3737
e.message = """
3838
compile error in testcase: #{desc}
3939
40+
#{input}
41+
4042
#{e.stack}
4143
4244
"""

0 commit comments

Comments
 (0)