Skip to content

Commit

Permalink
Force else to break before anything inside it (fixes #616)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlongster committed Mar 17, 2017
1 parent 748dcbf commit 95c2315
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -958,8 +958,7 @@ function genericPrintNoParens(path, options, print) {
]);
case "IfStatement":
const con = adjustClause(path.call(print, "consequent"), options);

parts = [
const opening = concat([
"if (",
group(
concat([
Expand All @@ -972,7 +971,9 @@ function genericPrintNoParens(path, options, print) {
),
")",
con
];
]);

parts.push(opening);

if (n.alternate) {
const hasBraces = isCurlyBracket(con);
Expand All @@ -988,11 +989,11 @@ function genericPrintNoParens(path, options, print) {
}

parts.push(
adjustClause(
group(adjustClause(
path.call(print, "alternate"),
options,
n.alternate.type === "IfStatement"
)
), { shouldBreak: willBreak(opening) })
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,91 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`else.js 1`] = `
"// Both functions below should be formatted exactly the same
function f() {
if (position)
return {name: pair};
else
return {name: pair.substring(0, position), value: pair.substring(position + 1)};
}
function f() {
if (position)
return {name: pair};
else
return {
name: pair.substring(0, position),
value: pair.substring(position + 1)
};
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Both functions below should be formatted exactly the same
function f() {
if (position) return { name: pair };
else
return {
name: pair.substring(0, position),
value: pair.substring(position + 1)
};
}
function f() {
if (position)
return { name: pair };
else
return {
name: pair.substring(0, position),
value: pair.substring(position + 1)
};
}
"
`;

exports[`else.js 2`] = `
"// Both functions below should be formatted exactly the same
function f() {
if (position)
return {name: pair};
else
return {name: pair.substring(0, position), value: pair.substring(position + 1)};
}
function f() {
if (position)
return {name: pair};
else
return {
name: pair.substring(0, position),
value: pair.substring(position + 1)
};
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Both functions below should be formatted exactly the same
function f() {
if (position) return { name: pair };
else
return {
name: pair.substring(0, position),
value: pair.substring(position + 1)
};
}
function f() {
if (position)
return { name: pair };
else
return {
name: pair.substring(0, position),
value: pair.substring(position + 1)
};
}
"
`;

exports[`if_comments.js 1`] = `
"async function f() {
if (untrackedChoice === 0) /* Cancel */ {
Expand Down
18 changes: 18 additions & 0 deletions tests/if/else.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Both functions below should be formatted exactly the same

function f() {
if (position)
return {name: pair};
else
return {name: pair.substring(0, position), value: pair.substring(position + 1)};
}

function f() {
if (position)
return {name: pair};
else
return {
name: pair.substring(0, position),
value: pair.substring(position + 1)
};
}
File renamed without changes.
File renamed without changes.

0 comments on commit 95c2315

Please sign in to comment.