better jsx output #369
This commit is contained in:
parent
7736fa11f2
commit
a8a7587c1f
@ -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(")");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
33
lib/6to5/transformation/transformers/react.js
vendored
33
lib/6to5/transformation/transformers/react.js
vendored
@ -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) {
|
||||||
|
|||||||
@ -1,3 +1,2 @@
|
|||||||
React.createElement(Component, React.__spread({}, this.props, {
|
React.createElement(Component, React.__spread({}, this.props, {
|
||||||
sound: "moo"
|
sound: "moo" }));
|
||||||
}));
|
|
||||||
|
|||||||
@ -1,3 +1 @@
|
|||||||
var x = Component({
|
var x = Component({ foo: "bar" }, Namespace.Component(null));
|
||||||
foo: "bar"
|
|
||||||
}, Namespace.Component(null));
|
|
||||||
|
|||||||
@ -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));
|
|
||||||
|
|||||||
@ -1,3 +1 @@
|
|||||||
React.createElement(Component, {
|
React.createElement(Component, { constructor: "foo" });
|
||||||
constructor: "foo"
|
|
||||||
});
|
|
||||||
|
|||||||
@ -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" });
|
||||||
});
|
|
||||||
|
|||||||
@ -1,4 +1 @@
|
|||||||
React.createElement(Component, React.__spread({}, x, {
|
React.createElement(Component, React.__spread({}, x, { y: 2, z: true }));
|
||||||
y: 2,
|
|
||||||
z: true
|
|
||||||
}));
|
|
||||||
|
|||||||
@ -1,4 +1 @@
|
|||||||
React.createElement(Component, React.__spread({
|
React.createElement(Component, React.__spread({ y: 2, z: true }, x));
|
||||||
y: 2,
|
|
||||||
z: true
|
|
||||||
}, x));
|
|
||||||
|
|||||||
@ -1,5 +1 @@
|
|||||||
React.createElement(Component, React.__spread({
|
React.createElement(Component, React.__spread({ y: 2 }, x, { z: true }));
|
||||||
y: 2
|
|
||||||
}, x, {
|
|
||||||
z: true
|
|
||||||
}));
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user