Skip to content

Commit

Permalink
Separate if and else groups (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 ee96775
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 23 deletions.
34 changes: 15 additions & 19 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -958,21 +958,20 @@ function genericPrintNoParens(path, options, print) {
]);
case "IfStatement":
const con = adjustClause(path.call(print, "consequent"), options);

parts = [
const opening = group(concat([
"if (",
group(
concat([
indent(
options.tabWidth,
concat([softline, path.call(print, "test")])
),
softline
])
),
group(concat([
indent(
options.tabWidth,
concat([softline, path.call(print, "test")])
),
softline
])),
")",
con
];
]));

parts.push(opening);

if (n.alternate) {
const hasBraces = isCurlyBracket(con);
Expand All @@ -981,22 +980,19 @@ function genericPrintNoParens(path, options, print) {
if (hasBraces && !isEmpty) {
parts.push(" else");
} else {
// We use `conditionalGroup` to suppress break propagation.
// This allows us to provide a hardline without forcing the
// entire `if` clause to break up.
parts.push(conditionalGroup([concat([hardline, "else"])]));
parts.push(hardline, "else");
}

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

return group(concat(parts));
return concat(parts);
case "ForStatement": {
const body = adjustClause(path.call(print, "body"), options);

Expand Down
3 changes: 1 addition & 2 deletions tests/flow/refinements/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2506,8 +2506,7 @@ type Breakfast = Apple | Orange | Broccoli | Carrot;
function bar(x: Breakfast) {
if (x.kind === \\"Fruit\\") {
(x.taste: \\"Good\\");
} else // error, Apple.taste = Bad
(x.raw: \\"No\\"); // error, Carrot.raw = Maybe
} else (x.raw: \\"No\\"); // error, Apple.taste = Bad // error, Carrot.raw = Maybe
}
function qux(x: Breakfast) {
Expand Down
3 changes: 1 addition & 2 deletions tests/flow/type-destructors/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ function foo(x: ?string): $NonMaybeType<?string> {
function foo(x: ?string): $NonMaybeType<?string> {
if (x != null) {
return x;
} else
return 0; // this should be an error
} else return 0; // this should be an error
}
//(foo(): string); // should not be necessary to expose the error above
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,89 @@
// 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 ee96775

Please sign in to comment.