Skip to content

Commit

Permalink
Ignore empty statement in switch newline detection (#1220)
Browse files Browse the repository at this point in the history
Fixes #1200
  • Loading branch information
vjeux committed Apr 13, 2017
1 parent 3a7559b commit ee7cfa9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/printer.js
Expand Up @@ -1189,12 +1189,17 @@ function genericPrintNoParens(path, options, print, args) {
const cons = path.call(consequentPath => {
return join(
hardline,
consequentPath.map((p, i) => {
const shouldAddLine =
i !== n.consequent.length - 1 &&
util.isNextLineEmpty(options.originalText, p.getValue());
return concat([print(p), shouldAddLine ? hardline : ""]);
})
consequentPath
.map((p, i) => {
if (n.consequent[i].type === "EmptyStatement") {
return null;
}
const shouldAddLine =
i !== n.consequent.length - 1 &&
util.isNextLineEmpty(options.originalText, p.getValue());
return concat([print(p), shouldAddLine ? hardline : ""]);
})
.filter(e => e !== null)
);
}, "consequent");
parts.push(
Expand Down
13 changes: 13 additions & 0 deletions tests/switch/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -48,6 +48,12 @@ switch (x) {
break;
}
switch (a) {
case b:
if (1) {};
c;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
switch (foo) {
case "bar":
Expand Down Expand Up @@ -93,6 +99,13 @@ switch (x) {
break;
}
switch (a) {
case b:
if (1) {
}
c;
}
`;

exports[`empty_switch.js 1`] = `
Expand Down
6 changes: 6 additions & 0 deletions tests/switch/empty_lines.js
Expand Up @@ -45,3 +45,9 @@ switch (x) {

break;
}

switch (a) {
case b:
if (1) {};
c;
}

0 comments on commit ee7cfa9

Please sign in to comment.