Fix #5034: Adjacent JSX elements must be wrapped in an enclosing tag (#5046)

This commit is contained in:
Geoffrey Booth 2018-05-01 08:09:07 -07:00 committed by GitHub
parent 708e57586c
commit fe7554828a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 2 deletions

View File

@ -671,7 +671,7 @@
// A Block node does not return its entire body, rather it
// ensures that the final expression is returned.
makeReturn(res) {
var expr, len;
var csxCheckIndex, expr, j, len, ref1;
len = this.expressions.length;
while (len--) {
expr = this.expressions[len];
@ -679,6 +679,15 @@
if (expr instanceof Return && !expr.expression) {
this.expressions.splice(len, 1);
}
// We also need to check that were not returning a CSX tag if theres an
// adjacent one at the same level; JSX doesnt allow that.
if (expr.unwrapAll().csx) {
for (csxCheckIndex = j = ref1 = len; (ref1 <= 0 ? j <= 0 : j >= 0); csxCheckIndex = ref1 <= 0 ? ++j : --j) {
if (this.expressions[csxCheckIndex].unwrapAll().csx) {
expr.error('Adjacent JSX elements must be wrapped in an enclosing tag');
}
}
}
break;
}
return this;

View File

@ -482,6 +482,12 @@ exports.Block = class Block extends Base
expr = @expressions[len]
@expressions[len] = expr.makeReturn res
@expressions.splice(len, 1) if expr instanceof Return and not expr.expression
# We also need to check that were not returning a CSX tag if theres an
# adjacent one at the same level; JSX doesnt allow that.
if expr.unwrapAll().csx
for csxCheckIndex in [len..0]
if @expressions[csxCheckIndex].unwrapAll().csx
expr.error 'Adjacent JSX elements must be wrapped in an enclosing tag'
break
this

View File

@ -1662,6 +1662,27 @@ test 'CSX error: invalid attributes', ->
^^^^^^^^^^^^^^^^
'''
test '#5034: CSX error: Adjacent JSX elements must be wrapped in an enclosing tag', ->
assertErrorFormat '''
render = ->
<Row>a</Row>
<Row>b</Row>
''', '''
[stdin]:3:4: error: Adjacent JSX elements must be wrapped in an enclosing tag
<Row>b</Row>
^^^^^^^^^^^
'''
assertErrorFormat '''
render = -> (
<Row>a</Row>
<Row>b</Row>
)
''', '''
[stdin]:3:4: error: Adjacent JSX elements must be wrapped in an enclosing tag
<Row>b</Row>
^^^^^^^^^^^
'''
test 'Bound method called as callback before binding throws runtime error', ->
class Base
constructor: ->
@ -1840,4 +1861,4 @@ test "#3933: prevent implicit calls when cotrol flow is missing `THEN`", ->
[stdin]:2:10: error: unexpected ->
when a ->
^^
'''
'''