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
master
Christopher Chedeau 2017-05-24 13:20:05 -07:00 committed by GitHub
parent 5a9bea1c2f
commit 58194e5375
3 changed files with 96 additions and 14 deletions

View File

@ -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])

View File

@ -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":

23
tests/switch/comments.js Normal file
View File

@ -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: {
}
}