Files
test-repo/tests/refi/switch.js
T
Christopher Chedeau cf45afba61 Add testing
- This brings in the flow test suite that contains a ton of JavaScript parsing edge cases
- This creates snapshot tests using the pretty printer for all of them
- If uncomment `RUN_AST_TESTS` line in `tests/run_specs.js`, it checks ast(pretty_print(x)) == ast(x). Right now, "178 failed, 197 passed, 375 of 377 total". So half of the tests are not passing, most of them are crashes and many of the rest are subtle issues.
2016-12-23 19:51:53 +00:00

64 lines
1.2 KiB
JavaScript

// @flow
function foo(a,b,c) {
switch (c) {
case a.x.y: // OK
case b.x.y: // OK
return;
default:
return;
}
}
// test refis out of switches that are exhaustive without default case
function exhaustion1(x): number {
var foo;
switch (x) {
case 0: // falls through
case 1:
foo = 3;
break;
default:
throw new Error('Invalid state');
}
return foo; // no error
}
function exhaustion2(x, y): number {
var foo;
switch (x) {
case 0:
if (y) {
break; // leaks uninitialized foo out of switch
}
/**
* TODO this shouldn't cause an error, because the path that
* runs it will always go on to assign a number to foo. But
* we'll need true isolation in env snapshots to model this.
* Currently tvars are always shared between clones - we'd
* have to rework envs pretty extensively to avoid it.
*
foo = "";
*/
case 1:
foo = 3;
break;
default:
throw new Error('Invalid state');
}
return foo; // error, possibly uninitialized
}
function exhaustion3(x): number {
let foo = null;
switch (x) {
case 0: // falls through
case 1:
foo = 3;
break;
default:
throw new Error('Invalid state');
}
return foo; // no error
}