Skip to content

Commit c0aaa0f

Browse files
authored
Don't print JSX bracket on same line when it has trailing comments (#3088)
1 parent 11d7213 commit c0aaa0f

6 files changed

Lines changed: 52 additions & 2 deletions

File tree

src/printer.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,6 +1774,14 @@ function genericPrintNoParens(path, options, print, args) {
17741774
);
17751775
}
17761776

1777+
const bracketSameLine =
1778+
options.jsxBracketSameLine &&
1779+
!(
1780+
n.name &&
1781+
((n.name.trailingComments && n.name.trailingComments.length) ||
1782+
(n.name.comments && n.name.comments.length))
1783+
);
1784+
17771785
return group(
17781786
concat([
17791787
"<",
@@ -1784,9 +1792,9 @@ function genericPrintNoParens(path, options, print, args) {
17841792
path.map(attr => concat([line, print(attr)]), "attributes")
17851793
)
17861794
),
1787-
n.selfClosing ? line : options.jsxBracketSameLine ? ">" : softline
1795+
n.selfClosing ? line : bracketSameLine ? ">" : softline
17881796
]),
1789-
n.selfClosing ? "/>" : options.jsxBracketSameLine ? "" : ">"
1797+
n.selfClosing ? "/>" : bracketSameLine ? "" : ">"
17901798
])
17911799
);
17921800
}

tests/comments/__snapshots__/jsfmt.spec.js.snap

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,13 @@ onClick={() => {}}>
838838
839839
</div>;
840840
841+
<div
842+
// comment
843+
>
844+
{foo}
845+
</div>;
846+
847+
841848
<Wrapper>
842849
{}
843850
<Component />
@@ -916,6 +923,12 @@ onClick={() => {}}>
916923
onClick={() => {}}
917924
/>;
918925
926+
<div
927+
// comment
928+
>
929+
{foo}
930+
</div>;
931+
919932
<Wrapper>
920933
{}
921934
<Component />

tests/comments/jsx.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ onClick={() => {}}>
8989

9090
</div>;
9191

92+
<div
93+
// comment
94+
>
95+
{foo}
96+
</div>;
97+
98+
9299
<Wrapper>
93100
{}
94101
<Component />
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`jsx_same_line.js 1`] = `
4+
<div
5+
// comment
6+
>
7+
{foo}
8+
</div>;
9+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10+
<div
11+
// comment
12+
>
13+
{foo}
14+
</div>;
15+
16+
`;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
run_spec(__dirname, { jsxBracketSameLine: true }, ["babylon", "typescript"]);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div
2+
// comment
3+
>
4+
{foo}
5+
</div>;

0 commit comments

Comments
 (0)