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) {

View File

@ -1,3 +1,2 @@
React.createElement(Component, React.__spread({}, this.props, {
sound: "moo"
}));
sound: "moo" }));

View File

@ -1,3 +1 @@
var x = Component({
foo: "bar"
}, Namespace.Component(null));
var x = Component({ foo: "bar" }, Namespace.Component(null));

View File

@ -1,3 +1 @@
var x = React.DOM.div({
foo: "bar"
}, React.DOM["font-face"](null));
var x = React.DOM.div({ foo: "bar" }, React.DOM["font-face"](null));

View File

@ -1,3 +1 @@
React.createElement(Component, {
constructor: "foo"
});
React.createElement(Component, { constructor: "foo" });

View File

@ -2,5 +2,4 @@ var x = React.createElement("div", {
attr1: "foo" + "bar",
attr2: "foo" + "bar" + "baz" + "bug",
attr3: "foo" + "bar" + "baz" + "bug",
attr4: "baz"
});
attr4: "baz" });

View File

@ -1,4 +1 @@
React.createElement(Component, React.__spread({}, x, {
y: 2,
z: true
}));
React.createElement(Component, React.__spread({}, x, { y: 2, z: true }));

View File

@ -1,4 +1 @@
React.createElement(Component, React.__spread({
y: 2,
z: true
}, x));
React.createElement(Component, React.__spread({ y: 2, z: true }, x));

View File

@ -1,5 +1 @@
React.createElement(Component, React.__spread({
y: 2
}, x, {
z: true
}));
React.createElement(Component, React.__spread({ y: 2 }, x, { z: true }));