make #683 more inline with the official jsx compiler

This commit is contained in:
Sebastian McKenzie
2015-02-04 10:38:35 +11:00
parent 6ede3986c7
commit ffc9244f88
2 changed files with 30 additions and 7 deletions

View File

@@ -5,8 +5,8 @@
// jsx
var esutils = require("esutils");
var isString = require("lodash/lang/isString");
var esutils = require("esutils");
var react = require("../../helpers/react");
var t = require("../../../types");
@@ -161,6 +161,8 @@ exports.JSXElement = {
callExpr.arguments.push(child);
}
callExpr.arguments = flatten(callExpr.arguments);
if (callExpr.arguments.length >= 3) {
callExpr._prettyCall = true;
}
@@ -169,6 +171,27 @@ exports.JSXElement = {
}
};
var isStringLiteral = function (node) {
return t.isLiteral(node) && isString(node.value);
};
var flatten = function (args) {
var flattened = [];
var last;
for (var i = 0; i < args.length; i++) {
var arg = args[i];
if (isStringLiteral(arg) && isStringLiteral(last)) {
last.value += arg.value;
} else {
last = arg;
flattened.push(arg);
}
}
return flattened;
};
var cleanJSXElementLiteralChild = function (child, args) {
var lines = child.value.split(/\r\n|\n|\r/);
@@ -186,6 +209,7 @@ var cleanJSXElementLiteralChild = function (child, args) {
var isFirstLine = i === 0;
var isLastLine = i === lines.length - 1;
var isLastNonEmptyLine = i === lastNonEmptyLine;
// replace rendered whitespace tabs with spaces
var trimmedLine = line.replace(/\t/g, " ");
@@ -201,12 +225,11 @@ var cleanJSXElementLiteralChild = function (child, args) {
}
if (trimmedLine) {
var lastArg = args[args.length - 1];
if (t.isLiteral(lastArg) && isString(lastArg.value)) {
lastArg.value += " " + trimmedLine;
} else {
args.push(t.literal(trimmedLine));
if (!isLastNonEmptyLine) {
trimmedLine += " ";
}
args.push(t.literal(trimmedLine));
}
}
};