fix(jsx): Break opening elements with single text attribute if there's a comment (#3154)

* fix(jsx): Break opening elements with single text attribute if there's a comment

* Single text attribute with a comment on the JSXElement name

* Consolidate rules of comments in JSX
This commit is contained in:
Lucas Duailibe authored and Lucas Azzola committed 2017-11-07 08:05:59 +11:00
1 parent f5248d4c33
commit 2c5a792d9a
5 files changed
+142 -4

No files matched your search

+28 -4
View File
@@ -1770,7 +1770,20 @@ function genericPrintNoParens(path, options, print, args) {
if ( if (
n.attributes.length === 1 && n.attributes.length === 1 &&
n.attributes[0].value && n.attributes[0].value &&
isStringLiteral(n.attributes[0].value) isStringLiteral(n.attributes[0].value) &&
// We should break for the following cases:
// <div
// // comment
// attr="value"
// >
// <div
// attr="value"
// // comment
// >
!(
(n.name && n.name.comments && n.name.comments.length) ||
(n.attributes[0].comments && n.attributes[0].comments.length)
)
) { ) {
return group( return group(
concat([ concat([
@@ -1785,10 +1798,21 @@ function genericPrintNoParens(path, options, print, args) {
const bracketSameLine = const bracketSameLine =
options.jsxBracketSameLine && options.jsxBracketSameLine &&
// We should print the bracket in a new line for the following cases:
// <div
// // comment
// >
// <div
// attr // comment
// >
!( !(
n.name && (n.name &&
((n.name.trailingComments && n.name.trailingComments.length) || !(n.attributes && n.attributes.length) &&
(n.name.comments && n.name.comments.length)) n.name.comments &&
n.name.comments.length) ||
(n.attributes &&
n.attributes.length &&
hasTrailingComment(util.getLast(n.attributes)))
); );
return group( return group(
@@ -844,6 +844,24 @@ onClick={() => {}}>
{foo} {foo}
</div>; </div>;
<div
className="foo" // comment
>
{foo}
</div>;
<div
className="foo"
// comment
>
{foo}
</div>;
<div // comment
id="foo"
>
{children}
</div>;
<Wrapper> <Wrapper>
{} {}
@@ -929,6 +947,25 @@ onClick={() => {}}>
{foo} {foo}
</div>; </div>;
<div
className="foo" // comment
>
{foo}
</div>;
<div
className="foo"
// comment
>
{foo}
</div>;
<div // comment
id="foo"
>
{children}
</div>;
<Wrapper> <Wrapper>
{} {}
<Component /> <Component />
+18
View File
@@ -95,6 +95,24 @@ onClick={() => {}}>
{foo} {foo}
</div>; </div>;
<div
className="foo" // comment
>
{foo}
</div>;
<div
className="foo"
// comment
>
{foo}
</div>;
<div // comment
id="foo"
>
{children}
</div>;
<Wrapper> <Wrapper>
{} {}
@@ -6,6 +6,26 @@ exports[`jsx_same_line.js 1`] = `
> >
{foo} {foo}
</div>; </div>;
<div
// comment
attr="foo"
>
{foo}
</div>;
<div
attr="foo" // comment
>
{foo}
</div>;
<div
attr="foo"
// comment
>
{foo}
</div>;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<div <div
// comment // comment
@@ -13,4 +33,23 @@ exports[`jsx_same_line.js 1`] = `
{foo} {foo}
</div>; </div>;
<div
// comment
attr="foo">
{foo}
</div>;
<div
attr="foo" // comment
>
{foo}
</div>;
<div
attr="foo"
// comment
>
{foo}
</div>;
`; `;
@@ -3,3 +3,23 @@
> >
{foo} {foo}
</div>; </div>;
<div
// comment
attr="foo"
>
{foo}
</div>;
<div
attr="foo" // comment
>
{foo}
</div>;
<div
attr="foo"
// comment
>
{foo}
</div>;