Fix additional empty line switch case comment (#936)

* Refactor condition in printStatementSequence, add new helper function.

* Add new test cases.

* Move logic in SwitchCase case.

* Revert unrelated changes, remove unecessary variables.

* Use util.getLast helper function.

* Move variables out of the loop.

* Simplify code.

* Fix mapping with mutated path.
This commit is contained in:
Davy Duperron authored and Christopher Chedeau committed 2017-03-17 08:08:36 -07:00
1 parent 76e26f08a0
commit dde8463ad9
3 files changed
+114 -7

No files matched your search

+9 -7
View File
@@ -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])
@@ -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
+19
View File
@@ -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
}