Break JSX element if a string literal prop has a newline (#4011)

This commit is contained in:
Lucas Duailibe authored and GitHub committed 2018-02-21 10:52:30 -03:00
1 parent f60bae61b8
commit 974b27dac2
3 files changed
+65 -1

No files matched your search

+14 -1
View File
@@ -1776,6 +1776,7 @@ function printPathNoParens(path, options, print, args) {
n.attributes.length === 1 &&
n.attributes[0].value &&
isStringLiteral(n.attributes[0].value) &&
!n.attributes[0].value.value.includes("\n") &&
// We should break for the following cases:
// <div
// // comment
@@ -1815,6 +1816,17 @@ function printPathNoParens(path, options, print, args) {
(!nameHasComments || n.attributes.length) &&
!lastAttrHasTrailingComments;
// We should print the opening element expanded if any prop value is a
// string literal with newlines
const shouldBreak =
n.attributes &&
n.attributes.some(
attr =>
attr.value &&
isStringLiteral(attr.value) &&
attr.value.value.includes("\n")
);
return group(
concat([
"<",
@@ -1828,7 +1840,8 @@ function printPathNoParens(path, options, print, args) {
n.selfClosing ? line : bracketSameLine ? ">" : softline
]),
n.selfClosing ? "/>" : bracketSameLine ? "" : ">"
])
]),
{ shouldBreak }
);
}
case "JSXClosingElement":
@@ -48,3 +48,48 @@ exports[`last_line.js 2`] = `
</SomeHighlyConfiguredComponent>;
`;
exports[`single_prop_multiline_string.js 1`] = `
<path d="M4.765 16.829l3.069-2.946 5.813 5.748
11.33-11.232 3.006 3.18-14.36 14.080z" />;
<Component text="Text
with
newlines">Content</Component>;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<path
d="M4.765 16.829l3.069-2.946 5.813 5.748
11.33-11.232 3.006 3.18-14.36 14.080z"
/>;
<Component
text="Text
with
newlines">
Content
</Component>;
`;
exports[`single_prop_multiline_string.js 2`] = `
<path d="M4.765 16.829l3.069-2.946 5.813 5.748
11.33-11.232 3.006 3.18-14.36 14.080z" />;
<Component text="Text
with
newlines">Content</Component>;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<path
d="M4.765 16.829l3.069-2.946 5.813 5.748
11.33-11.232 3.006 3.18-14.36 14.080z"
/>;
<Component
text="Text
with
newlines"
>
Content
</Component>;
`;
@@ -0,0 +1,6 @@
<path d="M4.765 16.829l3.069-2.946 5.813 5.748
11.33-11.232 3.006 3.18-14.36 14.080z" />;
<Component text="Text
with
newlines">Content</Component>;