From 58194e53759fd2745bc9199cf95837059d8a2c78 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Wed, 24 May 2017 13:20:05 -0700 Subject: [PATCH] Fix empty lines with comments between switch cases (#1709) The function isPreviousLineEmpty comment doesn't skip comments (on purpose, see comment above that method :P) so the detection is messed up. Turns out, it's easier to just use isNextLineEmpty like everywhere else. Fixes #1708 --- src/printer.js | 36 ++++++++----- tests/switch/__snapshots__/jsfmt.spec.js.snap | 51 +++++++++++++++++++ tests/switch/comments.js | 23 +++++++++ 3 files changed, 96 insertions(+), 14 deletions(-) create mode 100644 tests/switch/comments.js diff --git a/src/printer.js b/src/printer.js index c8f968e44..249b8dbd0 100644 --- a/src/printer.js +++ b/src/printer.js @@ -1385,7 +1385,24 @@ function genericPrintNoParens(path, options, print, args) { path.call(print, "discriminant"), ") {", n.cases.length > 0 - ? indent(concat([hardline, join(hardline, path.map(print, "cases"))])) + ? indent( + concat([ + hardline, + join( + hardline, + path.map(casePath => { + const caseNode = casePath.getValue(); + return concat([ + casePath.call(print), + n.cases.indexOf(caseNode) !== n.cases.length - 1 && + util.isNextLineEmpty(options.originalText, caseNode) + ? hardline + : "" + ]); + }, "cases") + ) + ]) + ) : "", hardline, "}" @@ -1397,16 +1414,11 @@ function genericPrintNoParens(path, options, print, args) { parts.push("default:"); } - const isFirstCase = path.getNode() === path.getParentNode().cases[0]; + const consequent = n.consequent.filter( + node => node.type !== "EmptyStatement" + ); - if ( - !isFirstCase && - util.isPreviousLineEmpty(options.originalText, path.getValue()) - ) { - parts.unshift(hardline); - } - - if (n.consequent.find(node => node.type !== "EmptyStatement")) { + if (consequent.length > 0) { const cons = path.call(consequentPath => { return join( hardline, @@ -1424,10 +1436,6 @@ function genericPrintNoParens(path, options, print, args) { ); }, "consequent"); - const consequent = n.consequent.filter( - node => node.type !== "EmptyStatement" - ); - parts.push( consequent.length === 1 && consequent[0].type === "BlockStatement" ? concat([" ", cons]) diff --git a/tests/switch/__snapshots__/jsfmt.spec.js.snap b/tests/switch/__snapshots__/jsfmt.spec.js.snap index 73ac0e839..2f325581f 100644 --- a/tests/switch/__snapshots__/jsfmt.spec.js.snap +++ b/tests/switch/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,56 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`comments.js 1`] = ` +switch (true) { + case true: + // Good luck getting here + + case false: +} + +switch (true) { + case true: + + // Good luck getting here + case false: +} + +switch(x) { + case x: { + } + + // other + + case y: { + } +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +switch (true) { + case true: + // Good luck getting here + + case false: +} + +switch (true) { + case true: + + // Good luck getting here + case false: +} + +switch (x) { + case x: { + } + + // other + + case y: { + } +} + +`; + exports[`empty_lines.js 1`] = ` switch (foo) { case "bar": diff --git a/tests/switch/comments.js b/tests/switch/comments.js new file mode 100644 index 000000000..d91b1e9ed --- /dev/null +++ b/tests/switch/comments.js @@ -0,0 +1,23 @@ +switch (true) { + case true: + // Good luck getting here + + case false: +} + +switch (true) { + case true: + + // Good luck getting here + case false: +} + +switch(x) { + case x: { + } + + // other + + case y: { + } +}