Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force else to break before anything inside it (fixes #616) #1032

Merged
merged 1 commit into from
Mar 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.