diff --git a/src/printer.js b/src/printer.js index d2adf0837..42f993fdb 100644 --- a/src/printer.js +++ b/src/printer.js @@ -959,21 +959,20 @@ function genericPrintNoParens(path, options, print) { ]); case "IfStatement": const con = adjustClause(path.call(print, "consequent"), options); - - parts = [ + const opening = group(concat([ "if (", - group( - concat([ - indent( - options.tabWidth, - concat([softline, path.call(print, "test")]) - ), - softline - ]) - ), + group(concat([ + indent( + options.tabWidth, + concat([softline, path.call(print, "test")]) + ), + softline + ])), ")", con - ]; + ])); + + parts.push(opening); if (n.alternate) { const hasBraces = isCurlyBracket(con); @@ -982,22 +981,19 @@ function genericPrintNoParens(path, options, print) { if (hasBraces && !isEmpty) { parts.push(" else"); } else { - // We use `conditionalGroup` to suppress break propagation. - // This allows us to provide a hardline without forcing the - // entire `if` clause to break up. - parts.push(conditionalGroup([concat([hardline, "else"])])); + parts.push(hardline, "else"); } parts.push( - adjustClause( + group(adjustClause( path.call(print, "alternate"), options, n.alternate.type === "IfStatement" - ) + )) ); } - return group(concat(parts)); + return concat(parts); case "ForStatement": { const body = adjustClause(path.call(print, "body"), options); diff --git a/tests/flow/refinements/__snapshots__/jsfmt.spec.js.snap b/tests/flow/refinements/__snapshots__/jsfmt.spec.js.snap index eef9be20d..76d1192e6 100644 --- a/tests/flow/refinements/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow/refinements/__snapshots__/jsfmt.spec.js.snap @@ -2506,8 +2506,7 @@ type Breakfast = Apple | Orange | Broccoli | Carrot; function bar(x: Breakfast) { if (x.kind === \\"Fruit\\") { (x.taste: \\"Good\\"); - } else // error, Apple.taste = Bad - (x.raw: \\"No\\"); // error, Carrot.raw = Maybe + } else (x.raw: \\"No\\"); // error, Apple.taste = Bad // error, Carrot.raw = Maybe } function qux(x: Breakfast) { diff --git a/tests/flow/type-destructors/__snapshots__/jsfmt.spec.js.snap b/tests/flow/type-destructors/__snapshots__/jsfmt.spec.js.snap index d4b77c71c..81bf2922a 100644 --- a/tests/flow/type-destructors/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow/type-destructors/__snapshots__/jsfmt.spec.js.snap @@ -19,8 +19,7 @@ function foo(x: ?string): $NonMaybeType { function foo(x: ?string): $NonMaybeType { if (x != null) { return x; - } else - return 0; // this should be an error + } else return 0; // this should be an error } //(foo(): string); // should not be necessary to expose the error above diff --git a/tests/if_comments/__snapshots__/jsfmt.spec.js.snap b/tests/if/__snapshots__/jsfmt.spec.js.snap similarity index 69% rename from tests/if_comments/__snapshots__/jsfmt.spec.js.snap rename to tests/if/__snapshots__/jsfmt.spec.js.snap index 93274c62a..a961d8bfc 100644 --- a/tests/if_comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/if/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,89 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`else.js 1`] = ` +"// Both functions below should be formatted exactly the same + +function f() { + if (position) + return {name: pair}; + else + return {name: pair.substring(0, position), value: pair.substring(position + 1)}; +} + +function f() { + if (position) + return {name: pair}; + else + return { + name: pair.substring(0, position), + value: pair.substring(position + 1) + }; +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// Both functions below should be formatted exactly the same + +function f() { + if (position) return { name: pair }; + else + return { + name: pair.substring(0, position), + value: pair.substring(position + 1) + }; +} + +function f() { + if (position) return { name: pair }; + else + return { + name: pair.substring(0, position), + value: pair.substring(position + 1) + }; +} +" +`; + +exports[`else.js 2`] = ` +"// Both functions below should be formatted exactly the same + +function f() { + if (position) + return {name: pair}; + else + return {name: pair.substring(0, position), value: pair.substring(position + 1)}; +} + +function f() { + if (position) + return {name: pair}; + else + return { + name: pair.substring(0, position), + value: pair.substring(position + 1) + }; +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// Both functions below should be formatted exactly the same + +function f() { + if (position) return { name: pair }; + else + return { + name: pair.substring(0, position), + value: pair.substring(position + 1) + }; +} + +function f() { + if (position) return { name: pair }; + else + return { + name: pair.substring(0, position), + value: pair.substring(position + 1) + }; +} +" +`; + exports[`if_comments.js 1`] = ` "async function f() { if (untrackedChoice === 0) /* Cancel */ { diff --git a/tests/if/else.js b/tests/if/else.js new file mode 100644 index 000000000..a12865afe --- /dev/null +++ b/tests/if/else.js @@ -0,0 +1,18 @@ +// Both functions below should be formatted exactly the same + +function f() { + if (position) + return {name: pair}; + else + return {name: pair.substring(0, position), value: pair.substring(position + 1)}; +} + +function f() { + if (position) + return {name: pair}; + else + return { + name: pair.substring(0, position), + value: pair.substring(position + 1) + }; +} diff --git a/tests/if_comments/if_comments.js b/tests/if/if_comments.js similarity index 100% rename from tests/if_comments/if_comments.js rename to tests/if/if_comments.js diff --git a/tests/if_comments/jsfmt.spec.js b/tests/if/jsfmt.spec.js similarity index 100% rename from tests/if_comments/jsfmt.spec.js rename to tests/if/jsfmt.spec.js