Don't break to new line if logical/loop statements are without brackets. (#194)

Closes #14.
master
Benjamin Tan 2017-01-15 12:37:13 +08:00 committed by James Long
parent 3f31a87da1
commit f70c9ec6d1
14 changed files with 73 additions and 145 deletions

View File

@ -2080,7 +2080,7 @@ function adjustClause(clause, options, forceSpace) {
return concat([ " ", clause ]);
}
return indent(options.tabWidth, concat([ hardline, clause ]));
return indent(options.tabWidth, concat([ line, clause ]));
}
function isCurlyBracket(doc) {

View File

@ -33,8 +33,7 @@ function foo() {
function bar(x: number) {}
function foo() {
var x = null;
if (x == null)
return;
if (x == null) return;
bar(x);
}
"

View File

@ -83,8 +83,7 @@ function foo(b) {
function bar(b) {
var x = b ? null : false;
if (x == null)
return;
if (x == null) return;
switch (\"\") {
case 0:
var y: number = x;
@ -101,8 +100,7 @@ function bar(b) {
function bar2(b) {
var x = b ? null : false;
if (x == null)
return;
if (x == null) return;
switch (\"\") {
case 0: {
let y: number = x;

View File

@ -104,16 +104,13 @@ function x(b) {
function consumer1(b) {
var g = x(b);
if (g instanceof X2)
g.foo = \"1337\";
else
g.foo = 1337;
if (g instanceof X2) g.foo = \"1337\";
else g.foo = 1337;
}
function consumer2(b) {
var g = x(b);
if (g instanceof X1)
g.foo = \"1337\"; // oops
if (g instanceof X1) g.foo = \"1337\"; // oops
}
// x.y instanceof t
@ -131,16 +128,13 @@ function y(b) {
function consumer3(b) {
var g = y(b);
if (g.bar instanceof X2)
g.bar.foo = \"1337\";
else
g.bar.foo = 1337;
if (g.bar instanceof X2) g.bar.foo = \"1337\";
else g.bar.foo = 1337;
}
function consumer4(b) {
var g = y(b);
if (g.bar instanceof X1)
g.bar.foo = \"1337\"; // oops
if (g.bar instanceof X1) g.bar.foo = \"1337\"; // oops
}
// x.y.z instance of t
@ -158,25 +152,20 @@ function z(b) {
function consumer5(b) {
var g = z(b);
if (g.baz.bar instanceof X2)
g.baz.bar.foo = \"1337\";
else
g.baz.bar.foo = 1337;
if (g.baz.bar instanceof X2) g.baz.bar.foo = \"1337\";
else g.baz.bar.foo = 1337;
}
function consumer6(b) {
var g = z(b);
if (g.baz.bar instanceof X1)
g.baz.bar.foo = \"1337\"; // oops
if (g.baz.bar instanceof X1) g.baz.bar.foo = \"1337\"; // oops
}
// this instanceof t
class C {
m() {
if (this instanceof D)
alert(this.s);
else
alert(\"nope\");
if (this instanceof D) alert(this.s);
else alert(\"nope\");
}
}

View File

@ -48,11 +48,9 @@ function corge(x: number) {}
var x = bar();
// x: ?string
if (x != null)
qux(x);
if (x != null) qux(x);
// x: ?string | null
if (x != null)
corge(x);
if (x != null) corge(x);
// x: ?string | null
function grault() {
x = null;

View File

@ -9,8 +9,7 @@ var z:number = x;
var x = 1;
while (typeof x == \"number\" || typeof x == \"string\") {
x = x + 1;
if (true)
x = \"\";
if (true) x = \"\";
}
var z: number = x;
"

View File

@ -80,8 +80,7 @@ function foo() {
}
function bar(x: ?string): number {
if (x == null)
x = \"\";
if (x == null) x = \"\";
return x.length;
}
"

View File

@ -85,8 +85,7 @@ var tests = [
}
},
function() {
if (x == null)
return;
if (x == null) return;
var y: string = x; // ok
},
function() {
@ -383,8 +382,7 @@ var tests = [
},
function() {
var x: { p: ?string } = { p: \"xxx\" };
if (x.p == null)
return;
if (x.p == null) return;
var y: string = x.p; // ok
},
function() {
@ -762,8 +760,7 @@ var paths = [
},
function() {
var x: ?string = \"xxx\";
if (x == null)
return;
if (x == null) return;
var y: string = x; // ok
},
function() {
@ -1071,28 +1068,22 @@ class C {
p: ?string;
ensure0(): string {
if (this.p != null)
return this.p;
else
return \"\";
if (this.p != null) return this.p;
else return \"\";
}
ensure1(): string {
if (this.p == null)
return \"\";
if (this.p == null) return \"\";
return this.p;
}
ensure2(): string | void {
if (this.p !== null)
return this.p;
else
return \"\";
if (this.p !== null) return this.p;
else return \"\";
}
ensure3(): string | void {
if (this.p === null)
return \"\";
if (this.p === null) return \"\";
return this.p;
}
}
@ -1100,17 +1091,13 @@ class C {
// super.p op null
class D extends C {
ensure100(): string {
if (super.p != null)
return super.p;
else
return \"\";
if (super.p != null) return super.p;
else return \"\";
}
ensure101(): string {
if (super.p == null)
return \"\";
else
return super.p;
if (super.p == null) return \"\";
else return super.p;
}
ensure103(): string {
@ -1486,31 +1473,23 @@ class A {
p: ?string;
ensure0(): string {
if (typeof this.p == \"string\")
return this.p;
else
return \"\";
if (typeof this.p == \"string\") return this.p;
else return \"\";
}
ensure1(): string {
if (typeof this.p != \"string\")
return \"\";
else
return this.p;
if (typeof this.p != \"string\") return \"\";
else return this.p;
}
ensure2(): string | void {
if (typeof this.p === \"string\")
return this.p;
else
return \"\";
if (typeof this.p === \"string\") return this.p;
else return \"\";
}
ensure3(): string | void {
if (typeof this.p !== \"string\")
return \"\";
else
return this.p;
if (typeof this.p !== \"string\") return \"\";
else return this.p;
}
}
"
@ -1648,17 +1627,13 @@ class A {
p: ?string;
ensure0(): string {
if (this.p !== undefined && this.p !== null)
return this.p;
else
return \"\";
if (this.p !== undefined && this.p !== null) return this.p;
else return \"\";
}
ensure1(): string {
if (this.p === undefined || this.p === null)
return \"\";
else
return this.p;
if (this.p === undefined || this.p === null) return \"\";
else return this.p;
}
}
"
@ -1796,17 +1771,13 @@ class A {
p: ?string;
ensure0(): string {
if (this.p !== void 0 && this.p !== null)
return this.p;
else
return \"\";
if (this.p !== void 0 && this.p !== null) return this.p;
else return \"\";
}
ensure1(): string {
if (this.p === void 0 || this.p === null)
return \"\";
else
return this.p;
if (this.p === void 0 || this.p === null) return \"\";
else return this.p;
}
}
"

View File

@ -287,8 +287,7 @@ type NonNullType = { kind: \"NonNullType\", type: Name | ListType | BadType };
type BadType = {};
function getTypeASTName(typeAST: Type): string {
if (!typeAST.type)
throw new Error(\"Must be wrapping type\");
if (!typeAST.type) throw new Error(\"Must be wrapping type\");
// OK
return getTypeASTName(typeAST.type); // error, BadType not a subtype of Type
}
@ -2466,8 +2465,7 @@ type Breakfast = Apple | Orange | Broccoli | Carrot;
function bar(x: Breakfast) {
if (x.kind === \"Fruit\") {
(x.taste: \"Good\");
} // error, Apple.taste = Bad else
(x.raw: \"No\"); // error, Carrot.raw = Maybe
} // error, Apple.taste = Bad else (x.raw: \"No\"); // error, Carrot.raw = Maybe
}
function qux(x: Breakfast) {
@ -2480,8 +2478,7 @@ function qux(x: Breakfast) {
// example 4
function list(n) {
if (n > 0)
return { kind: \"cons\", next: list(n - 1) };
if (n > 0) return { kind: \"cons\", next: list(n - 1) };
return { kind: \"nil\" };
}
function length(l) {
@ -2493,8 +2490,7 @@ function length(l) {
}
}
function check(n) {
if (n >= 0)
return n === length(list(n));
if (n >= 0) return n === length(list(n));
return true;
}
@ -2520,10 +2516,8 @@ kind({ kind: EnumKind.A, A: 1 });
type Citizen = { citizen: true };
type NonCitizen = { citizen: false, nationality: string };
function nationality(x: Citizen | NonCitizen) {
if (x.citizen)
return \"Shire\";
else
return x.nationality;
if (x.citizen) return \"Shire\";
else return x.nationality;
}
let tests = [

View File

@ -211,10 +211,8 @@ class G extends A {
class H extends A {
y: number;
constructor() {
if (Math.random() < 0.5)
super();
else
super();
if (Math.random() < 0.5) super();
else super();
this.y;
// OK
this.x; // OK
@ -262,8 +260,7 @@ class K_ {
class K extends K_ {
constructor() {
super(() => {
if (_this)
_this.foo();
if (_this) _this.foo();
});
// OK
var _this = this;

View File

@ -15,8 +15,7 @@ class A {
var x = 1;
while (typeof x == \"number\" || typeof x == \"string\") {
x = x + 1;
if (true)
x = \"\";
if (true) x = \"\";
}
var z: number = x;
}
@ -41,8 +40,7 @@ class A {
var x = 1;
while (typeof x == \"number\" || typeof x == \"string\") {
x = x + 1;
if (true)
x = \"\";
if (true) x = \"\";
}
var z: number = x;
}

View File

@ -17,8 +17,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
@ -51,10 +50,8 @@ type Obj_Prop_x = $PropertyType<Obj, \"x\">;
(42: Obj_Prop_x);
function foo(o: Obj): $PropertyType<Obj, \"x\"> {
if (false)
return o.x;
else
return 0;
if (false) return o.x;
else return 0;
}
"
`;

View File

@ -47,8 +47,7 @@ function foo() {
function bar() {
var x: ?{ bar(): void };
if (x)
x.bar();
if (x) x.bar();
}
function qux(x?: number, y: string = \"\", z) {}

View File

@ -330,10 +330,8 @@ function hello(x:X): string {
type X = ({ a: true } & { b: string }) | ({ a: false } & { c: string });
//type X = {a:true, b:string} | {a:false, c:string}; // this works.
function hello(x: X): string {
if (x.a === true)
return x.b;
else
return x.c;
if (x.a === true) return x.b;
else return x.c;
}
"
`;
@ -1242,10 +1240,8 @@ type Cons = { kind: \"cons\", next: List };
// disjoint unions
function length(list: List) {
if (list.kind === \"cons\")
return length(list.next) + 1;
else
return 0;
if (list.kind === \"cons\") return length(list.next) + 1;
else return 0;
}
length({ kind: \"nil\" });
@ -1619,10 +1615,8 @@ type Error = { type: \"ERROR\" } & Empty;
export type T = Success | Error;
function foo(x: T) {
if (x.type === \"SUCCESS\")
return x.result;
else
return x.result;
if (x.type === \"SUCCESS\") return x.result;
else return x.result;
}
"
`;
@ -1827,17 +1821,13 @@ function hello4(x:X): string {
type X = ({ a: true } & { b: string }) | ({ a: false } & { c: string });
//type X = {a:true, b:string} | {a:false, c:string}; // this works.
function hello1(x: X): string {
if (x.a === true)
return x.b;
else
return x.c;
if (x.a === true) return x.b;
else return x.c;
}
function hello2(x: X): string {
if (x.a === false)
return x.c;
else
return x.b;
if (x.a === false) return x.c;
else return x.b;
}
function hello3(x: X): string {