Skip to content

Commit

Permalink
Break JSXOpeningElement between attributes (fixes #15)
Browse files Browse the repository at this point in the history
  • Loading branch information
rattrayalex committed Jan 11, 2017
1 parent 0eda444 commit fbc0969
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 8 deletions.
9 changes: 7 additions & 2 deletions src/printer.js
Expand Up @@ -1108,8 +1108,13 @@ function genericPrintNoParens(path, options, print) {
concat([
"<",
path.call(print, "name"),
concat(path.map(attr => concat([" ", print(attr)]), "attributes")),
n.selfClosing ? " />" : ">"
multilineGroup(concat([
indent(options.tabWidth,
concat(path.map(attr => concat([line, print(attr)]), "attributes"))
),
n.selfClosing ? line : softline,
])),
n.selfClosing ? "/>" : ">"
])
);
case "JSXClosingElement":
Expand Down
6 changes: 3 additions & 3 deletions tests/jsx_intrinsics.builtin/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -39,9 +39,9 @@ var c: React.Element<any> = <div not_a_real_attr=\"asdf\" />;
// different attributes.
var d: React.Element<{ doesntmatch: string }> = <div not_a_real_attr=\"asdf\" />;
// No error as long as expectations are consistent, though.
var e: React.Element<{
not_a_real_attr: string
}> = <div not_a_real_attr=\"asdf\" />;
var e: React.Element<{ not_a_real_attr: string }> = <div
not_a_real_attr=\"asdf\"
/>;
"
`;
Expand Down
6 changes: 3 additions & 3 deletions tests/jsx_intrinsics.custom/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -32,9 +32,9 @@ var b: React.Element<{ prop1: string }> = <CustomComponent prop=\"asdf\" />;
<div id={42} />;
// Error: (\`id\` prop) number ~> string
var c: React.Element<{ id: string }> = <div id=\"asdf\" />;
var d: React.Element<{
id: number
}> = <div id=\"asdf\" />; // Error: Props<{id:string}> ~> Props<{id:number}>
var d: React.Element<{ id: number }> = <div
id=\"asdf\"
/>; // Error: Props<{id:string}> ~> Props<{id:number}>
"
`;

Expand Down
48 changes: 48 additions & 0 deletions tests/prettier/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -13,6 +13,54 @@ function fn() {
"
`;

exports[`test jsx_long_name_closed.js 1`] = `
"<BaseForm url=\"/auth/google\" method=\"GET\" colour=\"blue\" size=\"large\" submitLabel=\"Sign in with Google\"/>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<BaseForm
url=\"/auth/google\"
method=\"GET\"
colour=\"blue\"
size=\"large\"
submitLabel=\"Sign in with Google\"
/>;
"
`;

exports[`test jsx_long_name_open.js 1`] = `
"<BaseForm url=\"/auth/google\" method=\"GET\" colour=\"blue\" size=\"large\" submitLabel=\"Sign in with Google\">
hello
</BaseForm>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<BaseForm
url=\"/auth/google\"
method=\"GET\"
colour=\"blue\"
size=\"large\"
submitLabel=\"Sign in with Google\"
>
hello
</BaseForm>;
"
`;

exports[`test jsx_short_name_closed.js 1`] = `
"<BaseForm url=\"/auth/google\" method=\"GET\"/>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<BaseForm url=\"/auth/google\" method=\"GET\" />;
"
`;

exports[`test jsx_short_name_open.js 1`] = `
"<BaseForm url=\"/auth/google\" method=\"GET\">
hello
</BaseForm>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<BaseForm url=\"/auth/google\" method=\"GET\">
hello
</BaseForm>;
"
`;
exports[`test optional-type-name.js 1`] = `
"type Foo = (any) => string
Expand Down
1 change: 1 addition & 0 deletions tests/prettier/jsx_long_name_closed.js
@@ -0,0 +1 @@
<BaseForm url="/auth/google" method="GET" colour="blue" size="large" submitLabel="Sign in with Google"/>
3 changes: 3 additions & 0 deletions tests/prettier/jsx_long_name_open.js
@@ -0,0 +1,3 @@
<BaseForm url="/auth/google" method="GET" colour="blue" size="large" submitLabel="Sign in with Google">
hello
</BaseForm>
1 change: 1 addition & 0 deletions tests/prettier/jsx_short_name_closed.js
@@ -0,0 +1 @@
<BaseForm url="/auth/google" method="GET"/>
3 changes: 3 additions & 0 deletions tests/prettier/jsx_short_name_open.js
@@ -0,0 +1,3 @@
<BaseForm url="/auth/google" method="GET">
hello
</BaseForm>

0 comments on commit fbc0969

Please sign in to comment.