Skip to content

Commit

Permalink
Add support for JSXSpreadChild and fix comments inside JSXSpread (#3163)
Browse files Browse the repository at this point in the history
* Add support for JSXSpreadChild and fix comments inside JSXSpread

* Dangling comments

* Use another approach
  • Loading branch information
duailibe authored and azz committed Nov 7, 2017
1 parent 2c5a792 commit 8249b1c
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/printer.js
Expand Up @@ -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);

Expand Down Expand Up @@ -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") &&
Expand Down
117 changes: 117 additions & 0 deletions tests/jsx_spread/__snapshots__/jsfmt.spec.js.snap
@@ -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>;
`;
21 changes: 21 additions & 0 deletions tests/jsx_spread/attribute.js
@@ -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 changes: 21 additions & 0 deletions tests/jsx_spread/child.js
@@ -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>;
1 change: 1 addition & 0 deletions tests/jsx_spread/jsfmt.spec.js
@@ -0,0 +1 @@
run_spec(__dirname, { parser: "babylon" });

0 comments on commit 8249b1c

Please sign in to comment.