From fe7554828a25f228c7c9afbd602730f0514c9f7f Mon Sep 17 00:00:00 2001 From: Geoffrey Booth Date: Tue, 1 May 2018 08:09:07 -0700 Subject: [PATCH] Fix #5034: Adjacent JSX elements must be wrapped in an enclosing tag (#5046) --- lib/coffeescript/nodes.js | 11 ++++++++++- src/nodes.coffee | 6 ++++++ test/error_messages.coffee | 23 ++++++++++++++++++++++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/coffeescript/nodes.js b/lib/coffeescript/nodes.js index 71d23048..ab0e43ab 100644 --- a/lib/coffeescript/nodes.js +++ b/lib/coffeescript/nodes.js @@ -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 we’re not returning a CSX tag if there’s an + // adjacent one at the same level; JSX doesn’t 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; diff --git a/src/nodes.coffee b/src/nodes.coffee index 44347f19..ae513b9f 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -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 we’re not returning a CSX tag if there’s an + # adjacent one at the same level; JSX doesn’t 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 diff --git a/test/error_messages.coffee b/test/error_messages.coffee index 8b85f3d5..cacdc590 100644 --- a/test/error_messages.coffee +++ b/test/error_messages.coffee @@ -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 = -> + a + b + ''', ''' + [stdin]:3:4: error: Adjacent JSX elements must be wrapped in an enclosing tag + b + ^^^^^^^^^^^ + ''' + assertErrorFormat ''' + render = -> ( + a + b + ) + ''', ''' + [stdin]:3:4: error: Adjacent JSX elements must be wrapped in an enclosing tag + b + ^^^^^^^^^^^ + ''' + 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 -> ^^ - ''' \ No newline at end of file + '''