Fix trailing comment on switch case (#1136)

* Fix trailing comment on switch case

* Use unshift and add another test case
master
Lucas Duailibe 2017-04-05 20:27:14 -03:00 committed by Christopher Chedeau
parent d0666c3da4
commit 8e9fb43841
5 changed files with 169 additions and 7 deletions

View File

@ -1175,15 +1175,15 @@ function genericPrintNoParens(path, options, print) {
if (n.test) parts.push("case ", path.call(print, "test"), ":");
else parts.push("default:");
const isFirstCase = path.getNode() === path.getParentNode().cases[0];
if (!isFirstCase && util.isPreviousLineEmpty(options.originalText, path.getValue())) {
parts.unshift(hardline);
}
if (n.consequent.find(node => node.type !== "EmptyStatement")) {
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 : ""]);
}));
return join(hardline, consequentPath.map(print));
}, "consequent");
parts.push(
isCurlyBracket(cons)

View File

@ -1009,6 +1009,25 @@ switch (foo) {
// no default
}
switch (foo) {
case \\"bar\\": //comment
doThing(); //comment
case \\"baz\\":
doOtherThing(); //comment
}
switch (foo) {
case \\"bar\\": {
doThing();
} //comment
case \\"baz\\": {
doThing();
} //comment
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
switch (node && node.type) {
case \\"Property\\":
@ -1029,6 +1048,24 @@ switch (foo) {
// no default
}
switch (foo) {
case \\"bar\\": //comment
doThing(); //comment
case \\"baz\\":
doOtherThing(); //comment
}
switch (foo) {
case \\"bar\\": {
doThing();
} //comment
case \\"baz\\": {
doThing();
} //comment
}
"
`;

View File

@ -17,3 +17,22 @@ switch (foo) {
// no default
}
switch (foo) {
case "bar": //comment
doThing(); //comment
case "baz":
doOtherThing(); //comment
}
switch (foo) {
case "bar": {
doThing();
} //comment
case "baz": {
doThing();
} //comment
}

View File

@ -1,5 +1,76 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`empty_lines.js 1`] = `
"switch (foo) {
case \\"bar\\":
doSomething();
case \\"baz\\":
doOtherThing();
}
switch (foo) {
case \\"bar\\":
doSomething();
case \\"baz\\":
doOtherThing();
}
switch (foo) {
case \\"bar\\":
doSomething();
case \\"baz\\":
doOtherThing();
}
switch (foo) {
case \\"bar\\":
doSomething();
case \\"baz\\":
doOtherThing();
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
switch (foo) {
case \\"bar\\":
doSomething();
case \\"baz\\":
doOtherThing();
}
switch (foo) {
case \\"bar\\":
doSomething();
case \\"baz\\":
doOtherThing();
}
switch (foo) {
case \\"bar\\":
doSomething();
case \\"baz\\":
doOtherThing();
}
switch (foo) {
case \\"bar\\":
doSomething();
case \\"baz\\":
doOtherThing();
}
"
`;
exports[`empty_switch.js 1`] = `
"switch (1) { default:; }
switch (1) {}

View File

@ -0,0 +1,35 @@
switch (foo) {
case "bar":
doSomething();
case "baz":
doOtherThing();
}
switch (foo) {
case "bar":
doSomething();
case "baz":
doOtherThing();
}
switch (foo) {
case "bar":
doSomething();
case "baz":
doOtherThing();
}
switch (foo) {
case "bar":
doSomething();
case "baz":
doOtherThing();
}