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 #1708master
parent
5a9bea1c2f
commit
58194e5375
|
@ -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])
|
||||
|
|
|
@ -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":
|
||||
|
|
|
@ -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: {
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue