Improve if/else printing and other clauses

This commit is contained in:
James Long committed 2017-01-05 12:10:14 -05:00
1 parent fc186c8df8
commit fcd5436b38
20 files changed
+262 -385

No files matched your search

+29 -16
View File
@@ -860,13 +860,6 @@ function genericPrintNoParens(path, options, print) {
") ", ") ",
path.call(print, "body") path.call(print, "body")
]); ]);
// var con = adjustClause(path.call(print, "consequent"), options),
// parts = ["if (", path.call(print, "test"), ")", con];
// if (n.alternate)
// parts.push(
// endsWithBrace(con) ? " else" : "\nelse",
// adjustClause(path.call(print, "alternate"), options));
// return concat(parts);
case "IfStatement": case "IfStatement":
const con = adjustClause(path.call(print, "consequent"), options); const con = adjustClause(path.call(print, "consequent"), options);
@@ -884,15 +877,25 @@ function genericPrintNoParens(path, options, print) {
]; ];
if (n.alternate) { if (n.alternate) {
const hasBraces = getFirstString(con) === "{"; const hasBraces = isCurlyBracket(con);
const isEmpty = isEmptyBlock(con);
parts.push((hasBraces ? " else" : "\nelse"), adjustClause( if (hasBraces && !isEmpty) {
path.call(print, "alternate"), parts.push(" else");
options } else {
)); parts.push(concat([hardline, "else"]));
}
parts.push(
adjustClause(
path.call(print, "alternate"),
options,
n.alternate.type === "IfStatement"
)
);
} }
return concat(parts); return group(concat(parts));
case "ForStatement": case "ForStatement":
// TODO Get the for (;;) case right. // TODO Get the for (;;) case right.
return concat([ return concat([
@@ -952,7 +955,7 @@ function genericPrintNoParens(path, options, print) {
var clause = adjustClause(path.call(print, "body"), options); var clause = adjustClause(path.call(print, "body"), options);
var doBody = concat([ "do", clause ]); var doBody = concat([ "do", clause ]);
var parts = [ doBody ]; var parts = [ doBody ];
const hasBraces = getFirstString(clause) === "{"; const hasBraces = isCurlyBracket(clause);
if (hasBraces) if (hasBraces)
parts.push(" while"); parts.push(" while");
@@ -1898,14 +1901,24 @@ function printClass(path, print) {
return parts; return parts;
} }
function adjustClause(clause, options) { function adjustClause(clause, options, forceSpace) {
if (getFirstString(clause) === "{") { if (isCurlyBracket(clause) || forceSpace) {
return concat([ " ", clause ]); return concat([ " ", clause ]);
} }
return indent(options.tabWidth, concat([ hardline, clause ])); return indent(options.tabWidth, concat([ hardline, clause ]));
} }
function isCurlyBracket(doc) {
const str = getFirstString(doc);
return str === "{" || str === "{}";
}
function isEmptyBlock(doc) {
const str = getFirstString(doc);
return str === "{}";
}
function lastNonSpaceCharacter(lines) { function lastNonSpaceCharacter(lines) {
var pos = lines.lastPos(); var pos = lines.lastPos();
do { do {
@@ -118,14 +118,11 @@ let tests = [
false in []; false in [];
}, },
function() { function() {
if (\"foo\" in 123) if (\"foo\" in 123) {}
{}
if (!\"foo\" in {}) if (!\"foo\" in {}) {}
{}
if (!(\"foo\" in {})) if (!(\"foo\" in {})) {}
{}
}, },
function(x: Object, y: mixed) { function(x: Object, y: mixed) {
\"foo\" in x; \"foo\" in x;
+6 -12
View File
@@ -705,43 +705,37 @@ function try_scope() {
function for_scope_let() { function for_scope_let() {
let a: number = 0; let a: number = 0;
for (let a = \"\"; ; ) for (let a = \"\"; ; ) {}
{}
} }
function for_scope_var() { function for_scope_var() {
var a: number = 0; var a: number = 0;
for (var a = \"\"; ; ) for (var a = \"\"; ; ) {}
{}
} }
function for_in_scope_let(o: Object) { function for_in_scope_let(o: Object) {
let a: number = 0; let a: number = 0;
for (let a in o) for (let a in o) {}
{}
} }
function for_in_scope_var(o: Object) { function for_in_scope_var(o: Object) {
var a: number = 0; var a: number = 0;
for (var a in o) for (var a in o) {}
{}
} }
function for_of_scope_let(xs: string[]) { function for_of_scope_let(xs: string[]) {
let a: number = 0; let a: number = 0;
for (let a of xs) for (let a of xs) {}
{}
} }
function for_of_scope_var(xs: string[]) { function for_of_scope_var(xs: string[]) {
var a: number = 0; var a: number = 0;
for (var a of xs) for (var a of xs) {}
{}
} }
function default_param_1() { function default_param_1() {
+9 -18
View File
@@ -192,32 +192,23 @@ a.delete(\"xx\");
a.delete(3); a.delete(3);
for (let x: string of a.keys()) for (let x: string of a.keys()) {}
{}
for (let x: number of a.keys()) for (let x: number of a.keys()) {}
{}
for (let x: string | File of a.values()) for (let x: string | File of a.values()) {}
{}
for (let x: string | File | Blob of a.values()) for (let x: string | File | Blob of a.values()) {}
{}
for (let [ x, y ]: [string, string | File] of a.entries()) for (let [ x, y ]: [string, string | File] of a.entries()) {}
{}
for (let [ x, y ]: [string, string | File | Blob] of a.entries()) for (let [ x, y ]: [string, string | File | Blob] of a.entries()) {}
{}
for (let [ x, y ]: [number, string] of a.entries()) for (let [ x, y ]: [number, string] of a.entries()) {}
{}
for (let [ x, y ]: [string, number] of a.entries()) for (let [ x, y ]: [string, number] of a.entries()) {}
{}
for (let [ x, y ]: [number, number] of a.entries()) for (let [ x, y ]: [number, number] of a.entries()) {}
{}
" "
`; `;
+18 -22
View File
@@ -313,17 +313,15 @@ function h(x: number | string | boolean) {
call_me = () => { call_me = () => {
var y: number = const_x; var y: number = const_x;
}; };
} else } else if (typeof const_x == \"string\") {
if (typeof const_x == \"string\") { call_me = () => {
call_me = () => { var y: string = const_x;
var y: string = const_x; };
}; } else if (typeof const_x == \"boolean\") {
} else call_me = () => {
if (typeof const_x == \"boolean\") { var y: boolean = const_x;
call_me = () => { };
var y: boolean = const_x; }
};
}
var var_x = x; var var_x = x;
@@ -331,17 +329,15 @@ function h(x: number | string | boolean) {
call_me = () => { call_me = () => {
var y: number = var_x; var y: number = var_x;
}; };
} else } else if (typeof var_x == \"string\") {
if (typeof var_x == \"string\") { call_me = () => {
call_me = () => { var y: string = var_x;
var y: string = var_x; };
}; } else if (typeof var_x == \"boolean\") {
} else call_me = () => {
if (typeof var_x == \"boolean\") { var y: boolean = var_x;
call_me = () => { };
var y: boolean = var_x; }
};
}
} }
call_me(); call_me();
@@ -1,7 +1,6 @@
exports[`test errors.js 1`] = ` exports[`test errors.js 1`] = `
"if (typeof define === \'function\' && define.amd) { } "if (typeof define === \'function\' && define.amd) { }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (typeof define === \"function\" && define.amd) if (typeof define === \"function\" && define.amd) {}
{}
" "
`; `;
@@ -180,14 +180,11 @@ class GeneratorExamples {
*widen_next() { *widen_next() {
var x = yield 0; var x = yield 0;
if (typeof x === \"number\") if (typeof x === \"number\") {}
{} else if (typeof x === \"boolean\") {}
else else {
if (typeof x === \"boolean\") (x: string);
{} }
else {
(x: string);
}
} }
*widen_yield() { *widen_yield() {
yield 0; yield 0;
@@ -244,14 +241,11 @@ for (var x of examples.infer_stmt()) {
var infer_stmt_next = examples.infer_stmt().next(0).value; var infer_stmt_next = examples.infer_stmt().next(0).value;
if (typeof infer_stmt_next === \"undefined\") if (typeof infer_stmt_next === \"undefined\") {}
{} else if (typeof infer_stmt_next === \"number\") {}
else
if (typeof infer_stmt_next === \"number\")
{}
else { else {
(infer_stmt_next: boolean); (infer_stmt_next: boolean);
} }
examples.widen_next().next(0); examples.widen_next().next(0);
@@ -260,14 +254,11 @@ examples.widen_next().next(\"\");
examples.widen_next().next(true); examples.widen_next().next(true);
for (var x of examples.widen_yield()) { for (var x of examples.widen_yield()) {
if (typeof x === \"number\") if (typeof x === \"number\") {}
{} else if (typeof x === \"boolean\") {}
else else {
if (typeof x === \"boolean\") (x: string);
{} }
else {
(x: string);
}
} }
examples.delegate_next_generator().next(\"\"); examples.delegate_next_generator().next(\"\");
@@ -325,14 +316,11 @@ for (var x of examples.infer_stmt()) {
var infer_stmt_next = examples.infer_stmt().next(0).value; var infer_stmt_next = examples.infer_stmt().next(0).value;
if (typeof infer_stmt_next === \"undefined\") if (typeof infer_stmt_next === \"undefined\") {}
{} else if (typeof infer_stmt_next === \"number\") {}
else
if (typeof infer_stmt_next === \"number\")
{}
else { else {
(infer_stmt_next: boolean); (infer_stmt_next: boolean);
} }
" "
`; `;
@@ -525,26 +513,20 @@ for (var x of infer_stmt()) {
var infer_stmt_next = infer_stmt().next(0).value; var infer_stmt_next = infer_stmt().next(0).value;
if (typeof infer_stmt_next === \"undefined\") if (typeof infer_stmt_next === \"undefined\") {}
{} else if (typeof infer_stmt_next === \"number\") {}
else
if (typeof infer_stmt_next === \"number\")
{}
else { else {
(infer_stmt_next: boolean); (infer_stmt_next: boolean);
} }
function* widen_next() { function* widen_next() {
var x = yield 0; var x = yield 0;
if (typeof x === \"number\") if (typeof x === \"number\") {}
{} else if (typeof x === \"boolean\") {}
else else {
if (typeof x === \"boolean\") (x: string);
{} }
else {
(x: string);
}
} }
widen_next().next(0); widen_next().next(0);
@@ -562,14 +544,11 @@ function* widen_yield() {
} }
for (var x of widen_yield()) { for (var x of widen_yield()) {
if (typeof x === \"number\") if (typeof x === \"number\") {}
{} else if (typeof x === \"boolean\") {}
else else {
if (typeof x === \"boolean\") (x: string);
{} }
else {
(x: string);
}
} }
function* delegate_next_generator() { function* delegate_next_generator() {
@@ -113,7 +113,7 @@ function consumer1(b) {
if (g instanceof X2) if (g instanceof X2)
g.foo = \"1337\"; g.foo = \"1337\";
else else
g.foo = 1337; g.foo = 1337;
} }
@@ -140,7 +140,7 @@ function consumer3(b) {
if (g.bar instanceof X2) if (g.bar instanceof X2)
g.bar.foo = \"1337\"; g.bar.foo = \"1337\";
else else
g.bar.foo = 1337; g.bar.foo = 1337;
} }
@@ -167,7 +167,7 @@ function consumer5(b) {
if (g.baz.bar instanceof X2) if (g.baz.bar instanceof X2)
g.baz.bar.foo = \"1337\"; g.baz.bar.foo = \"1337\";
else else
g.baz.bar.foo = 1337; g.baz.bar.foo = 1337;
} }
@@ -182,7 +182,7 @@ class C {
m() { m() {
if (this instanceof D) if (this instanceof D)
alert(this.s); alert(this.s);
else else
alert(\"nope\"); alert(\"nope\");
} }
} }
+1 -2
View File
@@ -81,7 +81,6 @@ for (var x in null) {
foo(x); foo(x);
} }
for (var y in this) for (var y in this) {}
{}
" "
`; `;
+1 -2
View File
@@ -21,7 +21,6 @@ a.delete(\"foobar\");
var b = undefined; var b = undefined;
if (undefined) if (undefined) {}
{}
" "
`; `;
@@ -111,8 +111,7 @@ function goofy() {
if (typeof x == \"function\") { if (typeof x == \"function\") {
x(); x();
} else } else {}
{}
} }
function goofy2() { function goofy2() {
@@ -126,8 +125,7 @@ function goofy2() {
if (typeof y == \"function\") { if (typeof y == \"function\") {
y(); y();
} else } else {}
{}
} }
module.exports = true; module.exports = true;
@@ -252,8 +250,7 @@ class D {
} }
function baz(x: D) { function baz(x: D) {
if (x instanceof A) if (x instanceof A) {}
{}
} }
module.exports = \"sigma\"; module.exports = \"sigma\";
@@ -67,15 +67,13 @@ if (m.bind(o)) {
class D { class D {
m: Function; m: Function;
n() { n() {
if (this.m({})) if (this.m({})) {}
{}
} }
} }
declare var m: Function; declare var m: Function;
const x = \"\"; const x = \"\";
if (m.bind(this)(x)) if (m.bind(this)(x)) {}
{}
" "
`; `;
@@ -246,8 +244,7 @@ function f(_this: { m: ?Meeting }): string {
return \"0\"; return \"0\";
} }
if (_this.m.es.some(a => a.fbid === 0)) if (_this.m.es.some(a => a.fbid === 0)) {}
{}
return \"3\"; return \"3\";
} }
+79 -116
View File
@@ -95,9 +95,8 @@ var tests = [
} }
}, },
function() { function() {
if (x == null) if (x == null) {}
{} else {
else {
var y: string = x; var y: string = x;
} }
}, },
@@ -108,22 +107,19 @@ else {
var y: string = x; var y: string = x;
}, },
function() { function() {
if (!(x != null)) if (!(x != null)) {}
{} else {
else {
var y: string = x; var y: string = x;
} }
}, },
function() { function() {
if (x != null) if (x != null) {}
{}
var y: string = x; var y: string = x;
}, },
function() { function() {
if (x != null) if (x != null) {}
{} else {
else {
var y: string = x; var y: string = x;
} }
}, },
@@ -425,9 +421,8 @@ var tests = [
function() { function() {
var x: { p: ?string } = { p: \"xxx\" }; var x: { p: ?string } = { p: \"xxx\" };
if (x.p == null) if (x.p == null) {}
{} else {
else {
var y: string = x.p; var y: string = x.p;
} }
}, },
@@ -442,9 +437,8 @@ else {
function() { function() {
var x: { p: ?string } = { p: \"xxx\" }; var x: { p: ?string } = { p: \"xxx\" };
if (!(x.p != null)) if (!(x.p != null)) {}
{} else {
else {
var y: string = x.p; var y: string = x.p;
} }
}, },
@@ -469,17 +463,15 @@ else {
function() { function() {
var x: { p: ?string } = { p: \"xxx\" }; var x: { p: ?string } = { p: \"xxx\" };
if (x.p != null) if (x.p != null) {}
{}
var y: string = x.p; var y: string = x.p;
}, },
function() { function() {
var x: { p: ?string } = { p: \"xxx\" }; var x: { p: ?string } = { p: \"xxx\" };
if (x.p != null) if (x.p != null) {}
{} else {
else {
var y: string = x.p; var y: string = x.p;
} }
}, },
@@ -512,9 +504,8 @@ else {
function() { function() {
var x: { y: ?string } = { y: null }; var x: { y: ?string } = { y: null };
if (x.y) if (x.y) {}
{} else {
else {
x.y = \"foo\"; x.y = \"foo\";
} }
@@ -554,10 +545,9 @@ else {
if (typeof x.y == \"number\") { if (typeof x.y == \"number\") {
x.y = \"foo\"; x.y = \"foo\";
} else } else if (typeof x.y == \"boolean\") {
if (typeof x.y == \"boolean\") { x.y = \"bar\";
x.y = \"bar\"; }
}
(x.y: boolean); (x.y: boolean);
}, },
@@ -613,14 +603,12 @@ else {
(x.y: string); (x.y: string);
}, },
function(x: string) { function(x: string) {
if (x === \"a\") if (x === \"a\") {}
{}
(x: \"b\"); (x: \"b\");
}, },
function(x: mixed) { function(x: mixed) {
if (typeof x.bar === \"string\") if (typeof x.bar === \"string\") {}
{}
(x: string & number); (x: string & number);
}, },
@@ -628,8 +616,7 @@ else {
let x: { foo: ?string } = { foo: null }; let x: { foo: ?string } = { foo: null };
if (!x.foo) { if (!x.foo) {
if (false) if (false) {}
{}
x.foo = \"foo\"; x.foo = \"foo\";
} }
@@ -640,8 +627,7 @@ else {
let x: { foo: ?string } = { foo: null }; let x: { foo: ?string } = { foo: null };
if (!x.foo) { if (!x.foo) {
while (false) while (false) {}
{}
x.foo = \"foo\"; x.foo = \"foo\";
} }
@@ -652,8 +638,7 @@ else {
let x: { foo: ?string } = { foo: null }; let x: { foo: ?string } = { foo: null };
if (!x.foo) { if (!x.foo) {
for (var i = 0; i < 0; i++) for (var i = 0; i < 0; i++) {}
{}
x.foo = \"foo\"; x.foo = \"foo\";
} }
@@ -882,9 +867,8 @@ var paths = [
function() { function() {
var x: ?string = \"xxx\"; var x: ?string = \"xxx\";
if (x == null) if (x == null) {}
{} else {
else {
var y: string = x; var y: string = x;
} }
}, },
@@ -899,9 +883,8 @@ else {
function() { function() {
var x: ?string = \"xxx\"; var x: ?string = \"xxx\";
if (!(x != null)) if (!(x != null)) {}
{} else {
else {
var y: string = x; var y: string = x;
} }
}, },
@@ -917,17 +900,15 @@ else {
function() { function() {
var x: ?string = \"xxx\"; var x: ?string = \"xxx\";
if (x != null) if (x != null) {}
{}
var y: string = x; var y: string = x;
}, },
function() { function() {
var x: ?string = \"xxx\"; var x: ?string = \"xxx\";
if (x != null) if (x != null) {}
{} else {
else {
var y: string = x; var y: string = x;
} }
}, },
@@ -1169,27 +1150,24 @@ var null_tests = [
function() { function() {
var x: ?string = \"xxx\"; var x: ?string = \"xxx\";
if (x == null) if (x == null) {}
{} else {
else {
var y: string = x; var y: string = x;
} }
}, },
function() { function() {
var x: { p: ?string } = { p: \"xxx\" }; var x: { p: ?string } = { p: \"xxx\" };
if (x.p == null) if (x.p == null) {}
{} else {
else {
var y: string = x.p; var y: string = x.p;
} }
}, },
function() { function() {
var x: { p: { q: ?string } } = { p: { q: \"xxx\" } }; var x: { p: { q: ?string } } = { p: { q: \"xxx\" } };
if (x.p.q == null) if (x.p.q == null) {}
{} else {
else {
var y: string = x.p.q; var y: string = x.p.q;
} }
}, },
@@ -1217,27 +1195,24 @@ else {
function() { function() {
var x: ?string = \"xxx\"; var x: ?string = \"xxx\";
if (x === null) if (x === null) {}
{} else {
else {
var y: string | void = x; var y: string | void = x;
} }
}, },
function() { function() {
var x: { p: ?string } = { p: \"xxx\" }; var x: { p: ?string } = { p: \"xxx\" };
if (x.p === null) if (x.p === null) {}
{} else {
else {
var y: string | void = x.p; var y: string | void = x.p;
} }
}, },
function() { function() {
var x: { p: { q: ?string } } = { p: { q: \"xxx\" } }; var x: { p: { q: ?string } } = { p: { q: \"xxx\" } };
if (x.p.q === null) if (x.p.q === null) {}
{} else {
else {
var y: string | void = x.p.q; var y: string | void = x.p.q;
} }
} }
@@ -1247,7 +1222,7 @@ class C {
ensure0(): string { ensure0(): string {
if (this.p != null) if (this.p != null)
return this.p; return this.p;
else else
return \"\"; return \"\";
} }
ensure1(): string { ensure1(): string {
@@ -1259,7 +1234,7 @@ else
ensure2(): string | void { ensure2(): string | void {
if (this.p !== null) if (this.p !== null)
return this.p; return this.p;
else else
return \"\"; return \"\";
} }
ensure3(): string | void { ensure3(): string | void {
@@ -1273,13 +1248,13 @@ class D extends C {
ensure100(): string { ensure100(): string {
if (super.p != null) if (super.p != null)
return super.p; return super.p;
else else
return \"\"; return \"\";
} }
ensure101(): string { ensure101(): string {
if (super.p == null) if (super.p == null)
return \"\"; return \"\";
else else
return super.p; return super.p;
} }
ensure103(): string { ensure103(): string {
@@ -1623,27 +1598,24 @@ var null_tests = [
function() { function() {
var x: ?string = \"xxx\"; var x: ?string = \"xxx\";
if (typeof x != \"string\") if (typeof x != \"string\") {}
{} else {
else {
var y: string = x; var y: string = x;
} }
}, },
function() { function() {
var x: { p: ?string } = { p: \"xxx\" }; var x: { p: ?string } = { p: \"xxx\" };
if (typeof x.p != \"string\") if (typeof x.p != \"string\") {}
{} else {
else {
var y: string = x.p; var y: string = x.p;
} }
}, },
function() { function() {
var x: { p: { q: ?string } } = { p: { q: \"xxx\" } }; var x: { p: { q: ?string } } = { p: { q: \"xxx\" } };
if (typeof x.p.q != \"string\") if (typeof x.p.q != \"string\") {}
{} else {
else {
var y: string = x.p.q; var y: string = x.p.q;
} }
}, },
@@ -1671,27 +1643,24 @@ else {
function() { function() {
var x: ?string = \"xxx\"; var x: ?string = \"xxx\";
if (typeof x !== \"string\") if (typeof x !== \"string\") {}
{} else {
else {
var y: string = x; var y: string = x;
} }
}, },
function() { function() {
var x: { p: ?string } = { p: \"xxx\" }; var x: { p: ?string } = { p: \"xxx\" };
if (typeof x.p !== \"string\") if (typeof x.p !== \"string\") {}
{} else {
else {
var y: string = x.p; var y: string = x.p;
} }
}, },
function() { function() {
var x: { p: { q: ?string } } = { p: { q: \"xxx\" } }; var x: { p: { q: ?string } } = { p: { q: \"xxx\" } };
if (typeof x.p.q !== \"string\") if (typeof x.p.q !== \"string\") {}
{} else {
else {
var y: string = x.p.q; var y: string = x.p.q;
} }
} }
@@ -1701,25 +1670,25 @@ class A {
ensure0(): string { ensure0(): string {
if (typeof this.p == \"string\") if (typeof this.p == \"string\")
return this.p; return this.p;
else else
return \"\"; return \"\";
} }
ensure1(): string { ensure1(): string {
if (typeof this.p != \"string\") if (typeof this.p != \"string\")
return \"\"; return \"\";
else else
return this.p; return this.p;
} }
ensure2(): string | void { ensure2(): string | void {
if (typeof this.p === \"string\") if (typeof this.p === \"string\")
return this.p; return this.p;
else else
return \"\"; return \"\";
} }
ensure3(): string | void { ensure3(): string | void {
if (typeof this.p !== \"string\") if (typeof this.p !== \"string\")
return \"\"; return \"\";
else else
return this.p; return this.p;
} }
} }
@@ -1845,27 +1814,24 @@ var undef_tests = [
function() { function() {
var x: ?string = \"xxx\"; var x: ?string = \"xxx\";
if (x === undefined || x === null) if (x === undefined || x === null) {}
{} else {
else {
var y: string = x; var y: string = x;
} }
}, },
function() { function() {
var x: { p: ?string } = { p: \"xxx\" }; var x: { p: ?string } = { p: \"xxx\" };
if (x.p === undefined || x.p === null) if (x.p === undefined || x.p === null) {}
{} else {
else {
var y: string = x.p; var y: string = x.p;
} }
}, },
function() { function() {
var x: { p: { q: ?string } } = { p: { q: \"xxx\" } }; var x: { p: { q: ?string } } = { p: { q: \"xxx\" } };
if (x.p.q === undefined || x.p.q === null) if (x.p.q === undefined || x.p.q === null) {}
{} else {
else {
var y: string = x.p.q; var y: string = x.p.q;
} }
} }
@@ -1875,13 +1841,13 @@ class A {
ensure0(): string { ensure0(): string {
if (this.p !== undefined && this.p !== null) if (this.p !== undefined && this.p !== null)
return this.p; return this.p;
else else
return \"\"; return \"\";
} }
ensure1(): string { ensure1(): string {
if (this.p === undefined || this.p === null) if (this.p === undefined || this.p === null)
return \"\"; return \"\";
else else
return this.p; return this.p;
} }
} }
@@ -2007,27 +1973,24 @@ var void_tests = [
function() { function() {
var x: ?string = \"xxx\"; var x: ?string = \"xxx\";
if (x === void 0 || x === null) if (x === void 0 || x === null) {}
{} else {
else {
var y: string = x; var y: string = x;
} }
}, },
function() { function() {
var x: { p: ?string } = { p: \"xxx\" }; var x: { p: ?string } = { p: \"xxx\" };
if (x.p === void 0 || x.p === null) if (x.p === void 0 || x.p === null) {}
{} else {
else {
var y: string = x.p; var y: string = x.p;
} }
}, },
function() { function() {
var x: { p: { q: ?string } } = { p: { q: \"xxx\" } }; var x: { p: { q: ?string } } = { p: { q: \"xxx\" } };
if (x.p.q === void 0 || x.p.q === null) if (x.p.q === void 0 || x.p.q === null) {}
{} else {
else {
var y: string = x.p.q; var y: string = x.p.q;
} }
} }
@@ -2037,13 +2000,13 @@ class A {
ensure0(): string { ensure0(): string {
if (this.p !== void 0 && this.p !== null) if (this.p !== void 0 && this.p !== null)
return this.p; return this.p;
else else
return \"\"; return \"\";
} }
ensure1(): string { ensure1(): string {
if (this.p === void 0 || this.p === null) if (this.p === void 0 || this.p === null)
return \"\"; return \"\";
else else
return this.p; return this.p;
} }
} }
@@ -390,29 +390,22 @@ let tests = [
// ok // ok
let tests = [ let tests = [
function(x: string, y: number) { function(x: string, y: number) {
if (x == y) if (x == y) {}
{}
if (x === y) if (x === y) {}
{}
}, },
function(x: string) { function(x: string) {
if (x == undefined) if (x == undefined) {}
{}
if (x == void 0) if (x == void 0) {}
{}
}, },
function(x: string) { function(x: string) {
if (x == null) if (x == null) {}
{}
}, },
function(x: { y: \"foo\" } | { y: \"bar\" }) { function(x: { y: \"foo\" } | { y: \"bar\" }) {
if (x.y == 123) if (x.y == 123) {}
{}
if (x.y === 123) if (x.y === 123) {}
{}
} }
]; ];
" "
@@ -869,30 +862,23 @@ function foo5() {
} }
function foo6(o: mixed) { function foo6(o: mixed) {
if (o.bar) if (o.bar) {}
{}
} }
function foo7(o: mixed) { function foo7(o: mixed) {
if (typeof o.bar === \"string\") if (typeof o.bar === \"string\") {}
{}
if (o && typeof o.bar === \"string\") if (o && typeof o.bar === \"string\") {}
{}
if (o != null && typeof o.bar === \"string\") if (o != null && typeof o.bar === \"string\") {}
{}
if (o !== null && o !== undefined && typeof o.bar === \"string\") if (o !== null && o !== undefined && typeof o.bar === \"string\") {}
{}
} }
function foo8(o: { p: mixed }) { function foo8(o: { p: mixed }) {
if (o.p && o.p.q) if (o.p && o.p.q) {}
{}
if (o.p && o.p.q && o.p.q.r) if (o.p && o.p.q && o.p.q.r) {}
{}
} }
" "
`; `;
@@ -1536,12 +1522,10 @@ let tests = [
} }
}, },
function(num: number, obj: { foo: number }) { function(num: number, obj: { foo: number }) {
if (num === obj.bar) if (num === obj.bar) {}
{}
}, },
function(num: number, obj: { [key: string]: number }) { function(num: number, obj: { [key: string]: number }) {
if (num === obj.bar) if (num === obj.bar) {}
{}
}, },
function(n: number): Mode { function(n: number): Mode {
if (n !== 0 && n !== 1 && n !== 2) { if (n !== 0 && n !== 1 && n !== 2) {
@@ -1553,10 +1537,9 @@ let tests = [
function(s: number): ?Mode { function(s: number): ?Mode {
if (s === 0) { if (s === 0) {
return s; return s;
} else } else if (s === 3) {
if (s === 3) { return s;
return s; }
}
}, },
function(mode: Mode) { function(mode: Mode) {
switch (mode) { switch (mode) {
@@ -1928,15 +1911,13 @@ function waldo() {
function global_in_conditional0(x: number) { function global_in_conditional0(x: number) {
if (x != 0) { if (x != 0) {
if (BAZ) if (BAZ) {}
{}
} }
} }
function global_in_conditional2(x: number) { function global_in_conditional2(x: number) {
for (var i = 0; i < 100; i++) { for (var i = 0; i < 100; i++) {
if (BAZ) if (BAZ) {}
{}
} }
} }
" "
@@ -2166,12 +2147,10 @@ let tests = [
} }
}, },
function(str: string, obj: { foo: string }) { function(str: string, obj: { foo: string }) {
if (str === obj.bar) if (str === obj.bar) {}
{}
}, },
function(str: string, obj: { [key: string]: string }) { function(str: string, obj: { [key: string]: string }) {
if (str === obj.bar) if (str === obj.bar) {}
{}
}, },
function(str: string): Mode { function(str: string): Mode {
var ch = str[0]; var ch = str[0];
@@ -2185,10 +2164,9 @@ let tests = [
function(s: string): ?Mode { function(s: string): ?Mode {
if (s === \"a\") { if (s === \"a\") {
return s; return s;
} else } else if (s === \"d\") {
if (s === \"d\") { return s;
return s; }
}
}, },
function(mode: Mode) { function(mode: Mode) {
switch (mode) { switch (mode) {
@@ -2800,7 +2778,7 @@ type NonCitizen = { citizen: false, nationality: string };
function nationality(x: Citizen | NonCitizen) { function nationality(x: Citizen | NonCitizen) {
if (x.citizen) if (x.citizen)
return \"Shire\"; return \"Shire\";
else else
return x.nationality; return x.nationality;
} }
@@ -2811,18 +2789,14 @@ let tests = [
} }
}, },
function test8(x: { foo: { bar: 1 } }) { function test8(x: { foo: { bar: 1 } }) {
if (x.foo.bar === 1) if (x.foo.bar === 1) {}
{}
if (x.fooTypo.bar === 1) if (x.fooTypo.bar === 1) {}
{}
}, },
function(x: A) { function(x: A) {
if (x.kind === null.toString()) if (x.kind === null.toString()) {}
{}
if ({ kind: 1 }.kind === null.toString()) if ({ kind: 1 }.kind === null.toString()) {}
{}
}, },
function( function(
x: Array<string>, x: Array<string>,
@@ -2833,8 +2807,7 @@ let tests = [
s: Function, s: Function,
t: () => void t: () => void
) { ) {
if (x.length === 0) if (x.length === 0) {}
{}
if (x.legnth === 0) { if (x.legnth === 0) {
(x.legnth: number); (x.legnth: number);
@@ -2842,8 +2815,7 @@ let tests = [
(x.legnth: string); (x.legnth: string);
} }
if (y.length === 0) if (y.length === 0) {}
{}
if (y.legnth === 0) { if (y.legnth === 0) {
(y.legnth: number); (y.legnth: number);
@@ -2851,8 +2823,7 @@ let tests = [
(y.legnth: string); (y.legnth: string);
} }
if (z.toString === 0) if (z.toString === 0) {}
{}
if (z.toStirng === 0) { if (z.toStirng === 0) {
(z.toStirng: number); (z.toStirng: number);
@@ -2860,8 +2831,7 @@ let tests = [
(z.toStirng: string); (z.toStirng: string);
} }
if (q.valueOf === 0) if (q.valueOf === 0) {}
{}
if (q.valeuOf === 0) { if (q.valeuOf === 0) {
(q.valeuOf: number); (q.valeuOf: number);
@@ -2873,15 +2843,13 @@ let tests = [
(r.toStirng: empty); (r.toStirng: empty);
} }
if (s.call === 0) if (s.call === 0) {}
{}
if (s.calll === 0) { if (s.calll === 0) {
(t.calll: empty); (t.calll: empty);
} }
if (t.call === 0) if (t.call === 0) {}
{}
if (t.calll === 0) { if (t.calll === 0) {
(t.calll: empty); (t.calll: empty);
@@ -3276,9 +3244,8 @@ function undef_var(x: ?number) {
} }
function undef_var_rev(x: ?number) { function undef_var_rev(x: ?number) {
if (x === null || x === undefined) if (x === null || x === undefined) {}
{} else {
else {
var y = x * 1000; var y = x * 1000;
} }
} }
@@ -3290,9 +3257,8 @@ function undef_prop(x: { x: ?number }) {
} }
function undef_prop_rev(x: { x: ?number }) { function undef_prop_rev(x: { x: ?number }) {
if (x.x === null || x.x === undefined) if (x.x === null || x.x === undefined) {}
{} else {
else {
var y = x.x * 1000; var y = x.x * 1000;
} }
} }
@@ -3304,9 +3270,8 @@ function undef_var_fail(x: ?number) {
} }
function undef_var_fail_rev(x: ?number) { function undef_var_fail_rev(x: ?number) {
if (x === undefined) if (x === undefined) {}
{} else {
else {
var y = x * 1000; var y = x * 1000;
} }
} }
@@ -3318,9 +3283,8 @@ function undef_prop_fail(x: { x: ?number }) {
} }
function undef_prop_fail_rev(x: { x: ?number }) { function undef_prop_fail_rev(x: { x: ?number }) {
if (x.x === undefined) if (x.x === undefined) {}
{} else {
else {
var y = x.x * 1000; var y = x.x * 1000;
} }
} }
@@ -3511,9 +3475,8 @@ function void_var(x: ?number) {
} }
function void_var_rev(x: ?number) { function void_var_rev(x: ?number) {
if (x === null || x === void 0) if (x === null || x === void 0) {}
{} else {
else {
var y = x * 1000; var y = x * 1000;
} }
} }
@@ -3525,9 +3488,8 @@ function void_pro(x: { x: ?number }) {
} }
function void_pro_rev(x: { x: ?number }) { function void_pro_rev(x: { x: ?number }) {
if (x.x === null || x.x === void 0) if (x.x === null || x.x === void 0) {}
{} else {
else {
var y = x.x * 1000; var y = x.x * 1000;
} }
} }
@@ -3539,9 +3501,8 @@ function void_var_fail(x: ?number) {
} }
function void_var_fail_rev(x: ?number) { function void_var_fail_rev(x: ?number) {
if (x === void 0) if (x === void 0) {}
{} else {
else {
var y = x * 1000; var y = x * 1000;
} }
} }
@@ -3553,9 +3514,8 @@ function void_pro_fail(x: { x: ?number }) {
} }
function void_pro_fail_rev(x: { x: ?number }) { function void_pro_fail_rev(x: { x: ?number }) {
if (x.x === void 0) if (x.x === void 0) {}
{} else {
else {
var y = x.x * 1000; var y = x.x * 1000;
} }
} }
@@ -3567,9 +3527,8 @@ function void_var_side_effect(x: ?number) {
} }
function void_var_side_effect_rev(x: ?number) { function void_var_side_effect_rev(x: ?number) {
if (x === null || x === void (x * 1000)) if (x === null || x === void (x * 1000)) {}
{} else {
else {
var y = x * 1000; var y = x * 1000;
} }
} }
@@ -3581,9 +3540,8 @@ function void_prop_side_effect(x: { x: ?number }) {
} }
function void_prop_side_effect_rev(x: { x: ?number }) { function void_prop_side_effect_rev(x: { x: ?number }) {
if (x.x === null || x.x === void (x.x * 1000)) if (x.x === null || x.x === void (x.x * 1000)) {}
{} else {
else {
var y = x.x * 1000; var y = x.x * 1000;
} }
} }
+1 -1
View File
@@ -238,7 +238,7 @@ class H extends A {
constructor() { constructor() {
if (Math.random() < 0.5) if (Math.random() < 0.5)
super(); super();
else else
super(); super();
this.y; this.y;
@@ -45,12 +45,11 @@ type Foobar = Foo | Bar;
function foobar(x: Foobar): string { function foobar(x: Foobar): string {
if (x.type === \"foo\") { if (x.type === \"foo\") {
return foo(x); return foo(x);
} else } else if (x.type === \"bar\") {
if (x.type === \"bar\") { return bar(x);
return bar(x); } else {
} else { return \"unknown\";
return \"unknown\"; }
}
} }
function foo(x: Foo): string { function foo(x: Foo): string {
+1 -2
View File
@@ -572,8 +572,7 @@ function bar(response) {
throw new Error(\"...\"); throw new Error(\"...\");
} }
if (payload.error) if (payload.error) {}
{}
} }
function qux() { function qux() {
@@ -209,14 +209,11 @@ if (Array.isArray(x)) {}
/* @flow */ /* @flow */
let x = 0; let x = 0;
if (x == null) if (x == null) {}
{}
if (x == undefined) if (x == undefined) {}
{}
if (Array.isArray(x)) if (Array.isArray(x)) {}
{}
" "
`; `;
@@ -55,7 +55,7 @@ type Obj_Prop_x = $PropertyType<Obj, \"x\">;
function foo(o: Obj): $PropertyType<Obj, \"x\"> { function foo(o: Obj): $PropertyType<Obj, \"x\"> {
if (false) if (false)
return o.x; return o.x;
else else
return 0; return 0;
} }
" "
@@ -314,7 +314,7 @@ type X = ({ a: true } & { b: string }) | ({ a: false } & { c: string });
function hello(x: X): string { function hello(x: X): string {
if (x.a === true) if (x.a === true)
return x.b; return x.b;
else else
return x.c; return x.c;
} }
" "
@@ -1192,7 +1192,7 @@ type Cons = { kind: \"cons\", next: List };
function length(list: List) { function length(list: List) {
if (list.kind === \"cons\") if (list.kind === \"cons\")
return length(list.next) + 1; return length(list.next) + 1;
else else
return 0; return 0;
} }
@@ -1548,7 +1548,7 @@ export type T = Success | Error;
function foo(x: T) { function foo(x: T) {
if (x.type === \"SUCCESS\") if (x.type === \"SUCCESS\")
return x.result; return x.result;
else else
return x.result; return x.result;
} }
" "
@@ -1748,14 +1748,14 @@ type X = ({ a: true } & { b: string }) | ({ a: false } & { c: string });
function hello1(x: X): string { function hello1(x: X): string {
if (x.a === true) if (x.a === true)
return x.b; return x.b;
else else
return x.c; return x.c;
} }
function hello2(x: X): string { function hello2(x: X): string {
if (x.a === false) if (x.a === false)
return x.c; return x.c;
else else
return x.b; return x.b;
} }