Fix trailing comment on switch case (#1136)
* Fix trailing comment on switch case * Use unshift and add another test casemaster
parent
d0666c3da4
commit
8e9fb43841
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
"
|
||||
`;
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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) {}
|
||||
|
|
|
@ -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();
|
||||
}
|
Loading…
Reference in New Issue