Skip to content

Commit

Permalink
more react compliant whitespace - #165
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmck committed Nov 15, 2014
1 parent 7a261a1 commit 206c828
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions lib/6to5/transformation/transformers/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down

0 comments on commit 206c828

Please sign in to comment.