From 35b28cf72262089cd5ff8ebdbcd02a6570be5cab Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 11 Nov 2014 15:34:29 +1100 Subject: [PATCH] more reliable jsx literal whitespace --- lib/6to5/transformation/transformers/jsx.js | 15 +++++++++++---- .../transformation/jsx/everything/expected.js | 2 +- .../jsx/tags-with-literals/expected.js | 8 ++++---- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/6to5/transformation/transformers/jsx.js b/lib/6to5/transformation/transformers/jsx.js index 2b3bd90c9b..40bbc4911e 100644 --- a/lib/6to5/transformation/transformers/jsx.js +++ b/lib/6to5/transformation/transformers/jsx.js @@ -80,10 +80,17 @@ exports.XJSElement = { var callExpr = node.openingElement; var children = node.children; - _.each(children, function (child) { - if (t.isLiteral(child) && _.isString(child.value)) { - child.value = child.value.trim().replace(/\n(\s+)/g, " "); - if (!child.value) return; + _.each(children, function (child, i) { + var val = child.value; + if (t.isLiteral(child) && _.isString(val)) { + val = val.replace(/\n(\s+)/g, " "); + + i = +i; + if (i === 0) val = val.trimLeft(); + if (i === children.length - 1) val = val.trimRight(); + + if (!val) return; + child.value = val; } callExpr.arguments.push(child); diff --git a/test/fixtures/transformation/jsx/everything/expected.js b/test/fixtures/transformation/jsx/everything/expected.js index c798178588..09090cb36e 100644 --- a/test/fixtures/transformation/jsx/everything/expected.js +++ b/test/fixtures/transformation/jsx/everything/expected.js @@ -3,5 +3,5 @@ X({ "data-prop": x ? Y({ prop: 2 - }) : Z(null, "\n") + }) : Z(null) }); diff --git a/test/fixtures/transformation/jsx/tags-with-literals/expected.js b/test/fixtures/transformation/jsx/tags-with-literals/expected.js index e6e7dcb074..539d0d28d8 100644 --- a/test/fixtures/transformation/jsx/tags-with-literals/expected.js +++ b/test/fixtures/transformation/jsx/tags-with-literals/expected.js @@ -1,9 +1,9 @@ "use strict"; -(X(null, " ")); +(X(null)); -(X(null, "\n")); +(X(null)); -(X(null, "\n string\n")); +(X(null, "string")); -(X(null, "\n string\n string\n ")); +(X(null, "string string"));