Skip to content

JSXTransformer, concatenate consecutive strings #489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/dom/components/__tests__/ReactDOMTextarea-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ describe('ReactDOMTextarea', function() {

expect(function() {
ReactTestUtils.renderIntoDocument(
<textarea>{'hello'}{'there'}</textarea>
<textarea>{'hello'}{'there'+'avoid jsx concat'}</textarea>
);
}).toThrow();

Expand Down
25 changes: 18 additions & 7 deletions vendor/fbtransform/transforms/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ var JSX_ATTRIBUTE_TRANSFORMS = {
}
};

function isChildString(child) {
return child.type === Syntax.Literal ||
child.type === Syntax.XJSExpressionContainer &&
child.expression.raw && child.expression.raw.match(/^(|'[^']*'|"[^"]*")$/);
}

function visitReactTag(traverse, object, path, state) {
var jsxObjIdent = utils.getDocblock(state).jsx;

Expand Down Expand Up @@ -134,18 +140,23 @@ function visitReactTag(traverse, object, path, state) {
if (childrenToRender.length > 0) {
utils.append(', ', state);

object.children.forEach(function(child) {
if (child.type === Syntax.Literal && !child.value.match(/\S/)) {
return;
}
childrenToRender.forEach(function(child, index) {
utils.catchup(child.range[0], state);

var isLast = child === childrenToRender[childrenToRender.length - 1];
var isLast = index === childrenToRender.length - 1;

if (child.type === Syntax.Literal) {
renderXJSLiteral(child, isLast, state);
var concat = !isLast && isChildString(childrenToRender[index + 1]);

renderXJSLiteral(child, isLast, state, null, null, concat);
} else if (child.type === Syntax.XJSExpressionContainer) {
renderXJSExpressionContainer(traverse, child, isLast, path, state);
var concat = !isLast &&
isChildString(child) &&
isChildString(childrenToRender[index + 1]);

renderXJSExpressionContainer(
traverse, child, isLast, path, state, concat
);
} else {
traverse(child, path, state);
if (!isLast) {
Expand Down
10 changes: 6 additions & 4 deletions vendor/fbtransform/transforms/xjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ function trimWithSingleSpace(string) {
* "line "+
* "line"
*/
function renderXJSLiteral(object, isLast, state, start, end) {
function renderXJSLiteral(object, isLast, state, start, end, concat) {
/** Added blank check filtering and triming*/
var trimmedChildValue = safeTrim(object.value);
var hasFinalNewLine = false;
Expand Down Expand Up @@ -253,7 +253,7 @@ function renderXJSLiteral(object, isLast, state, start, end) {

// add comma before trailing whitespace
if (!isLast) {
utils.append(',', state);
utils.append(concat ? '+' : ',', state);
}

// tail whitespace
Expand All @@ -264,14 +264,16 @@ function renderXJSLiteral(object, isLast, state, start, end) {
utils.move(object.range[1], state);
}

function renderXJSExpressionContainer(traverse, object, isLast, path, state) {
function renderXJSExpressionContainer(
traverse, object, isLast, path, state, concat
) {
// Plus 1 to skip `{`.
utils.move(object.range[0] + 1, state);
traverse(object.expression, path, state);
if (!isLast && object.expression.type !== Syntax.XJSEmptyExpression) {
// If we need to append a comma, make sure to do so after the expression.
utils.catchup(object.expression.range[1], state);
utils.append(',', state);
utils.append(concat ? '+' : ',', state);
}

// Minus 1 to skip `}`.
Expand Down