From 6bff3f2de81f670e5fa93fe5f92e6f683bfd5d94 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Mon, 10 Apr 2017 10:50:02 -0700 Subject: [PATCH] Fix switch new lines (#1156) .#1136 accidentally removed all the empty lines between statements inside of switch cases. I just brough back the logic and made sure to only use it for everything but the last line. --- src/printer.js | 6 ++++- tests/switch/__snapshots__/jsfmt.spec.js.snap | 24 +++++++++++++++++++ tests/switch/empty_lines.js | 12 ++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/printer.js b/src/printer.js index ed59bfc7..488b021e 100644 --- a/src/printer.js +++ b/src/printer.js @@ -1193,7 +1193,11 @@ function genericPrintNoParens(path, options, print) { if (n.consequent.find(node => node.type !== "EmptyStatement")) { const cons = path.call(consequentPath => { - return join(hardline, consequentPath.map(print)); + return join(hardline, consequentPath.map((p, i) => { + const shouldAddLine = i !== n.consequent.length - 1 && + util.isNextLineEmpty(options.originalText, p.getValue()); + return concat([print(p), shouldAddLine ? hardline : ""]); + })); }, "consequent"); parts.push( isCurlyBracket(cons) diff --git a/tests/switch/__snapshots__/jsfmt.spec.js.snap b/tests/switch/__snapshots__/jsfmt.spec.js.snap index eed54119..9d65ac16 100644 --- a/tests/switch/__snapshots__/jsfmt.spec.js.snap +++ b/tests/switch/__snapshots__/jsfmt.spec.js.snap @@ -36,6 +36,18 @@ switch (foo) { case \\"baz\\": doOtherThing(); } + +switch (x) { + case y: + call(); + + break; + + case z: + call(); + + break; +} ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ switch (foo) { case \\"bar\\": @@ -68,6 +80,18 @@ switch (foo) { case \\"baz\\": doOtherThing(); } + +switch (x) { + case y: + call(); + + break; + + case z: + call(); + + break; +} " `; diff --git a/tests/switch/empty_lines.js b/tests/switch/empty_lines.js index 5972dbe8..4ecca081 100644 --- a/tests/switch/empty_lines.js +++ b/tests/switch/empty_lines.js @@ -33,3 +33,15 @@ switch (foo) { case "baz": doOtherThing(); } + +switch (x) { + case y: + call(); + + break; + + case z: + call(); + + break; +}