better jsx output #369

This commit is contained in:
Sebastian McKenzie
2015-01-07 22:42:03 +11:00
parent 7736fa11f2
commit a8a7587c1f
10 changed files with 46 additions and 45 deletions

View File

@@ -55,10 +55,30 @@ exports.ThisExpression = function () {
this.push("this");
};
exports.CallExpression = function (node, print) {
exports.CallExpression = function (node, print, parent) {
print(node.callee);
this.push("(");
print.join(node.arguments, { separator: ", " });
var separator = ",";
if (node._prettyPrint) {
separator += "\n";
this.newline();
this.indent();
} else {
separator += " ";
}
print.join(node.arguments, {
separator: separator
});
if (node._prettyPrint) {
this.newline();
this.dedent();
}
this.push(")");
};

View File

@@ -32,7 +32,7 @@ exports.XJSExpressionContainer = function (node) {
exports.XJSAttribute = {
exit: function (node) {
var value = node.value || t.literal(true);
return t.property("init", node.name, value);
return t.inherits(t.property("init", node.name, value), node);
}
};
@@ -103,30 +103,29 @@ exports.XJSOpeningElement = {
args.push(props);
var setPrettyPrint = function (call, args) {
if (args.length >= 3) {
call._prettyPrint = true;
}
return call;
};
if (reactCompat) {
if (tagName && isTag(tagName)) {
return t.callExpression(
return setPrettyPrint(t.callExpression(
t.memberExpression(
t.memberExpression(
t.identifier('React'),
t.identifier('DOM'),
false
),
t.memberExpression(t.identifier("React"), t.identifier("DOM")),
tagExpr,
t.isLiteral(tagExpr)
),
args
);
} else {
return t.callExpression(
tagExpr,
args
);
), args);
}
} else {
tagExpr = t.memberExpression(t.identifier("React"), t.identifier("createElement"));
return t.callExpression(tagExpr, args);
}
return setPrettyPrint(t.callExpression(tagExpr, args), args);
}
};
@@ -148,16 +147,16 @@ exports.XJSElement = {
var isLastLine = +i === lines.length - 1;
// replace rendered whitespace tabs with spaces
var trimmedLine = line.replace(/\t/g, ' ');
var trimmedLine = line.replace(/\t/g, " ");
// trim whitespace touching a newline
if (!isFirstLine) {
trimmedLine = trimmedLine.replace(/^[ ]+/, '');
trimmedLine = trimmedLine.replace(/^[ ]+/, "");
}
// trim whitespace touching an endline
if (!isLastLine) {
trimmedLine = trimmedLine.replace(/[ ]+$/, '');
trimmedLine = trimmedLine.replace(/[ ]+$/, "");
}
if (trimmedLine) {