diff --git a/src/printer.js b/src/printer.js index 20b172791..5a0d22b33 100644 --- a/src/printer.js +++ b/src/printer.js @@ -1173,13 +1173,15 @@ function genericPrintNoParens(path, options, print) { else parts.push("default:"); if (n.consequent.find(node => node.type !== "EmptyStatement")) { - const cons = path.call( - function(consequentPath) { - return printStatementSequence(consequentPath, options, print); - }, - "consequent" - ); - + const parent = path.getParentNode(); + const lastCase = util.getLast(parent.cases); + const cons = path.call(consequentPath => { + return join(hardline, consequentPath.map(p => { + const shouldAddLine = p.getParentNode() !== lastCase && + util.isNextLineEmpty(options.originalText, p.getValue()); + return concat([print(p), shouldAddLine ? hardline : ""]); + })); + }, "consequent"); parts.push( isCurlyBracket(cons) ? concat([" ", cons]) diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap index ea0781707..62a5da095 100644 --- a/tests/comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap @@ -1886,6 +1886,92 @@ function inlineComment() { " `; +exports[`switch.js 1`] = ` +"switch (node && node.type) { + case \\"Property\\": + case \\"MethodDefinition\\": + prop = node.key; + break; + + case \\"MemberExpression\\": + prop = node.property; + break; + + // no default +} + +switch (foo) { + case \\"bar\\": + doThing() + + // no default +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +switch (node && node.type) { + case \\"Property\\": + case \\"MethodDefinition\\": + prop = node.key; + break; + + case \\"MemberExpression\\": + prop = node.property; + break; + + // no default +} + +switch (foo) { + case \\"bar\\": + doThing(); + + // no default +} +" +`; + +exports[`switch.js 2`] = ` +"switch (node && node.type) { + case \\"Property\\": + case \\"MethodDefinition\\": + prop = node.key; + break; + + case \\"MemberExpression\\": + prop = node.property; + break; + + // no default +} + +switch (foo) { + case \\"bar\\": + doThing() + + // no default +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +switch (node && node.type) { + case \\"Property\\": + case \\"MethodDefinition\\": + prop = node.key; + break; + + case \\"MemberExpression\\": + prop = node.property; + break; + + // no default +} + +switch (foo) { + case \\"bar\\": + doThing(); + + // no default +} +" +`; + exports[`template-literal.js 1`] = ` "\` \${a // comment diff --git a/tests/comments/switch.js b/tests/comments/switch.js new file mode 100644 index 000000000..1cae16451 --- /dev/null +++ b/tests/comments/switch.js @@ -0,0 +1,19 @@ +switch (node && node.type) { + case "Property": + case "MethodDefinition": + prop = node.key; + break; + + case "MemberExpression": + prop = node.property; + break; + + // no default +} + +switch (foo) { + case "bar": + doThing() + + // no default +}