Add support for JSXSpreadChild and fix comments inside JSXSpread (#3163)

* Add support for JSXSpreadChild and fix comments inside JSXSpread

* Dangling comments

* Use another approach
master
Lucas Duailibe 2017-11-07 01:23:10 -02:00 committed by Lucas Azzola
parent 2c5a792d9a
commit 8249b1c841
5 changed files with 185 additions and 2 deletions

View File

@ -1716,7 +1716,28 @@ function genericPrintNoParens(path, options, print, args) {
case "TSQualifiedName":
return join(".", [path.call(print, "left"), path.call(print, "right")]);
case "JSXSpreadAttribute":
return concat(["{...", path.call(print, "argument"), "}"]);
case "JSXSpreadChild": {
return concat([
"{",
path.call(p => {
const printed = concat(["...", print(p)]);
const n = p.getValue();
if (!n.comments || !n.comments.length) {
return printed;
}
return concat([
indent(
concat([
softline,
comments.printComments(p, () => printed, options)
])
),
softline
]);
}, n.type === "JSXSpreadAttribute" ? "argument" : "expression"),
"}"
]);
}
case "JSXExpressionContainer": {
const parent = path.getParentNode(0);
@ -4973,7 +4994,9 @@ function printAstToDoc(ast, options, addAlignmentSize) {
if (
((node && node.type === "JSXElement") ||
(parent &&
(parent.type === "UnionTypeAnnotation" ||
(parent.type === "JSXSpreadAttribute" ||
parent.type === "JSXSpreadChild" ||
parent.type === "UnionTypeAnnotation" ||
parent.type === "TSUnionType" ||
((parent.type === "ClassDeclaration" ||
parent.type === "ClassExpression") &&

View File

@ -0,0 +1,117 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`attribute.js 1`] = `
<div {...a}/>;
<div {...a /* comment */}/>;
<div {/* comment */...a}/>;
<div {...a //comment
}/>;
<div {...a
//comment
}/>;
<div {
//comment
...a
}/>;
<div {//comment
...a // comment
}/>;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<div {...a} />;
<div {...a /* comment */} />;
<div {/* comment */ ...a} />;
<div
{
...a //comment
}
/>;
<div
{
...a
//comment
}
/>;
<div
{
//comment
...a
}
/>;
<div
{
//comment
...a // comment
}
/>;
`;
exports[`child.js 1`] = `
<div>{...a}</div>;
<div>{...a /* comment */}</div>;
<div>{/* comment */...a}</div>;
<div>{...a //comment
}</div>;
<div>{...a
//comment
}</div>;
<div>{
//comment
...a
}</div>;
<div>{//comment
...a // comment
}</div>;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<div>{...a}</div>;
<div>{...a /* comment */}</div>;
<div>{/* comment */ ...a}</div>;
<div>
{
...a //comment
}
</div>;
<div>
{
...a
//comment
}
</div>;
<div>
{
//comment
...a
}
</div>;
<div>
{
//comment
...a // comment
}
</div>;
`;

View File

@ -0,0 +1,21 @@
<div {...a}/>;
<div {...a /* comment */}/>;
<div {/* comment */...a}/>;
<div {...a //comment
}/>;
<div {...a
//comment
}/>;
<div {
//comment
...a
}/>;
<div {//comment
...a // comment
}/>;

21
tests/jsx_spread/child.js Normal file
View File

@ -0,0 +1,21 @@
<div>{...a}</div>;
<div>{...a /* comment */}</div>;
<div>{/* comment */...a}</div>;
<div>{...a //comment
}</div>;
<div>{...a
//comment
}</div>;
<div>{
//comment
...a
}</div>;
<div>{//comment
...a // comment
}</div>;

View File

@ -0,0 +1 @@
run_spec(__dirname, { parser: "babylon" });