Skip to content

Commit

Permalink
Wrap Stateless JSX Arrow Functions in Parens (fixes part of #73)
Browse files Browse the repository at this point in the history
  • Loading branch information
rattrayalex committed Jan 11, 2017
1 parent 4872459 commit 1601500
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/printer.js
Expand Up @@ -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 ");
Expand Down
51 changes: 51 additions & 0 deletions tests/prettier/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -56,6 +56,57 @@ short_open = <BaseForm url=\"/auth/google\" method=\"GET\">
"
`;
exports[`test jsx-stateless-arrow-fn.js 1`] = `
"const render1 = ({ styles }) => (
<div style={styles} key=\"something\">
Keep the wrapping params. Put each key on it\'s own line.
</div>
);
const render2 = ({ styles }) => <div style={styles} key=\"something\">
Create wrapping params.
</div>;
const render3 = ({ styles }) => <div style={styles} key=\"something\">Bump to next line without parens</div>;
const render4 = ({ styles }) => <div style={styles} key=\"something\">Create wrapping parens and indent <strong>all the things</strong>.</div>;
const render5 = ({ styles }) => <div>Keep it on one line.</div>;
const notJSX = (aaaaaaaaaaaaaaaaa, bbbbbbbbbbb) => this.someLongCallWithParams(aaaaaa, bbbbbbb).anotherLongCallWithParams(cccccccccccc, dddddddddddddddddddddd)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const render1 = ({ styles }) => (
<div style={styles} key=\"something\">
Keep the wrapping params. Put each key on it\'s own line.
</div>
);
const render2 = ({ styles }) => (
<div style={styles} key=\"something\">
Create wrapping params.
</div>
);
const render3 = ({ styles }) => (
<div style={styles} key=\"something\">Bump to next line without parens</div>
);
const render4 = ({ styles }) => (
<div
style={styles}
key=\"something\"
>Create wrapping parens and indent<strong>all the things</strong>.</div>
);
const render5 = ({ styles }) => <div>Keep it on one line.</div>;
const notJSX = (aaaaaaaaaaaaaaaaa, bbbbbbbbbbb) =>
this
.someLongCallWithParams(aaaaaa, bbbbbbb)
.anotherLongCallWithParams(cccccccccccc, dddddddddddddddddddddd);
"
`;

exports[`test optional-type-name.js 1`] = `
"type Foo = (any) => string
Expand Down
17 changes: 17 additions & 0 deletions tests/prettier/jsx-stateless-arrow-fn.js
@@ -0,0 +1,17 @@
const render1 = ({ styles }) => (
<div style={styles} key="something">
Keep the wrapping params. Put each key on it's own line.
</div>
);

const render2 = ({ styles }) => <div style={styles} key="something">
Create wrapping params.
</div>;

const render3 = ({ styles }) => <div style={styles} key="something">Bump to next line without parens</div>;

const render4 = ({ styles }) => <div style={styles} key="something">Create wrapping parens and indent <strong>all the things</strong>.</div>;

const render5 = ({ styles }) => <div>Keep it on one line.</div>;

const notJSX = (aaaaaaaaaaaaaaaaa, bbbbbbbbbbb) => this.someLongCallWithParams(aaaaaa, bbbbbbb).anotherLongCallWithParams(cccccccccccc, dddddddddddddddddddddd)

0 comments on commit 1601500

Please sign in to comment.