Files
test-repo/tests/refinements/missing-property-cond.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

51 lines
1.1 KiB
JavaScript

// @flow
function foo1(o: { x: number }) {
if (o.p1) { // OK, this is an idiomatic way of testing property existence
o.x;
}
}
function foo2(o: { x: number }) {
if (o.p2) { // OK
o.p2.x; // error, since o.p2's type is unknown (e.g., could be `number`)
}
}
function foo3(o: { x: number }) {
o.p3.x; // usual error outside conditional
}
function foo4(o: $Exact<{ x: number }>) {
if (o.p4) { // OK
o.p4.x; // currently OK, should be unreachable
} else {
o.p4.x; // error
}
}
function foo5() {
const o = { };
_foo5();
if (o.p) { o.p(); }
function _foo5() {
o.p = function() { }
}
}
function foo6(o: mixed) {
if (o.bar) {} // error, any lookup on mixed is unsafe
}
function foo7(o: mixed) {
if (typeof o.bar === 'string') {} // error
if (o && typeof o.bar === 'string') {} // ok
if (o != null && typeof o.bar === 'string') {} // ok
if (o !== null && o !== undefined && typeof o.bar === 'string') {} // ok
}
function foo8(o: { p: mixed }) {
if (o.p && o.p.q) {} // this is ok because o.p is truthy, so o.p.q is safe
if (o.p && o.p.q && o.p.q.r) {}
}