Files
test-repo/tests/instanceof/instanceof.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

88 lines
1.5 KiB
JavaScript

/* @flow */
// x instancof t
class X1 { foo: number; };
class X2 { foo: string; };
function x(b) { return b ? new X1 : new X2; }
function consumer1(b) {
var g = x(b);
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
}
// x.y instanceof t
class Y1 { bar: X1; };
class Y2 { bar: X2; };
function y(b) { return b ? new Y1 : new Y2; }
function consumer3(b) {
var g = y(b);
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
}
// x.y.z instance of t
class Z1 { baz: Y1; };
class Z2 { baz: Y2; };
function z(b) { return b ? new Z1 : new Z2; }
function consumer5(b) {
var g = z(b);
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
}
// this instanceof t
class C {
m() {
if (this instanceof D)
alert(this.s);
else
alert("nope");
}
}
class D extends C {
s: string;
constructor() {
super();
this.s = "yup";
}
}
function foo0(x: Array<number> | number) {
if (x instanceof Array) {
x[0] = 123;
} else {
x++;
}
}
function foo1(x: Array<number> | number) {
if (x instanceof Array) {
x++; // error
} else {
x[0] = 123; // error
}
}