more react compliant whitespace - #165

This commit is contained in:
Sebastian McKenzie
2014-11-15 11:00:32 +11:00
parent 7a261a1db1
commit 206c828a56

View File

@@ -106,11 +106,44 @@ exports.XJSElement = {
exit: function (node) {
var callExpr = node.openingElement;
var childrenToRender = node.children.filter(function(child) {
return !(t.isLiteral(child) && _.isString(child.value) && child.value.match(/^[ \t]*[\r\n][ \t\r\n]*$/));
});
_.each(node.children, function (child) {
if (t.isLiteral(child)) {
var lines = child.value.split(/\r\n|\n|\r/);
var lastNonEmptyLine = 0;
_.each(lines, function (line, i) {
if (line.match(/[^ \t]/)) {
lastNonEmptyLine = i;
}
});
_.each(lines, function (line, i) {
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, ' ');
// trim whitespace touching a newline
if (!isFirstLine) {
trimmedLine = trimmedLine.replace(/^[ ]+/, '');
}
// trim whitespace touching an endline
if (!isLastLine) {
trimmedLine = trimmedLine.replace(/[ ]+$/, '');
}
if (trimmedLine || isLastNonEmptyLine) {
callExpr.arguments.push(t.literal(trimmedLine));
}
});
return;
}
_.each(childrenToRender, function (child) {
callExpr.arguments.push(child);
});