diff --git a/src/printer.js b/src/printer.js index 080bafdd3fe3..15bff733c2fc 100644 --- a/src/printer.js +++ b/src/printer.js @@ -338,12 +338,26 @@ function genericPrintNoParens(path, options, print) { parts.push(" =>"); - return conditionalGroup([ - concat([ concat(parts), " ", path.call(print, "body") ]), - concat([ concat(parts), - indent(options.tabWidth, - concat([line, path.call(print, "body")]))]) - ]) + const body = path.call(print, "body"); + + const groups = []; + if (n.body.type === "JSXElement") { + // special-case JSX to wrap in parens (unless it's a one-liner) + if (!hasHardLine(body)) groups.push(concat([ concat(parts), " ", body ])); + groups.push(concat([ + concat(parts), + " (", + indent(options.tabWidth, concat([ hardline, body ])), + hardline, + ")", + ])); + } else { + groups.push(concat([ concat(parts), " ", body ])); + groups.push(concat([ + concat(parts), indent(options.tabWidth, concat([ line, body ])) + ])); + } + return conditionalGroup(groups); case "MethodDefinition": if (n.static) { parts.push("static "); diff --git a/tests/prettier/__snapshots__/jsfmt.spec.js.snap b/tests/prettier/__snapshots__/jsfmt.spec.js.snap index ed100d1bcf50..24877d90cc1d 100644 --- a/tests/prettier/__snapshots__/jsfmt.spec.js.snap +++ b/tests/prettier/__snapshots__/jsfmt.spec.js.snap @@ -56,6 +56,57 @@ short_open = " `; +exports[`test jsx-stateless-arrow-fn.js 1`] = ` +"const render1 = ({ styles }) => ( +
+ Keep the wrapping params. Put each key on it\'s own line. +
+); + +const render2 = ({ styles }) =>
+ Create wrapping params. +
; + +const render3 = ({ styles }) =>
Bump to next line without parens
; + +const render4 = ({ styles }) =>
Create wrapping parens and indent all the things.
; + +const render5 = ({ styles }) =>
Keep it on one line.
; + +const notJSX = (aaaaaaaaaaaaaaaaa, bbbbbbbbbbb) => this.someLongCallWithParams(aaaaaa, bbbbbbb).anotherLongCallWithParams(cccccccccccc, dddddddddddddddddddddd) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +const render1 = ({ styles }) => ( +
+ Keep the wrapping params. Put each key on it\'s own line. +
+); + +const render2 = ({ styles }) => ( +
+ Create wrapping params. +
+); + +const render3 = ({ styles }) => ( +
Bump to next line without parens
+); + +const render4 = ({ styles }) => ( +
Create wrapping parens and indentall the things.
+); + +const render5 = ({ styles }) =>
Keep it on one line.
; + +const notJSX = (aaaaaaaaaaaaaaaaa, bbbbbbbbbbb) => + this + .someLongCallWithParams(aaaaaa, bbbbbbb) + .anotherLongCallWithParams(cccccccccccc, dddddddddddddddddddddd); +" +`; + exports[`test optional-type-name.js 1`] = ` "type Foo = (any) => string diff --git a/tests/prettier/jsx-stateless-arrow-fn.js b/tests/prettier/jsx-stateless-arrow-fn.js new file mode 100644 index 000000000000..c724b9b4b6b8 --- /dev/null +++ b/tests/prettier/jsx-stateless-arrow-fn.js @@ -0,0 +1,17 @@ +const render1 = ({ styles }) => ( +
+ Keep the wrapping params. Put each key on it's own line. +
+); + +const render2 = ({ styles }) =>
+ Create wrapping params. +
; + +const render3 = ({ styles }) =>
Bump to next line without parens
; + +const render4 = ({ styles }) =>
Create wrapping parens and indent all the things.
; + +const render5 = ({ styles }) =>
Keep it on one line.
; + +const notJSX = (aaaaaaaaaaaaaaaaa, bbbbbbbbbbb) => this.someLongCallWithParams(aaaaaa, bbbbbbb).anotherLongCallWithParams(cccccccccccc, dddddddddddddddddddddd)