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"); this.push("this");
}; };
exports.CallExpression = function (node, print) { exports.CallExpression = function (node, print, parent) {
print(node.callee); print(node.callee);
this.push("("); 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(")"); this.push(")");
}; };

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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