diff --git a/tests/baselines/reference/controlFlowTruthiness.js b/tests/baselines/reference/controlFlowTruthiness.js new file mode 100644 index 0000000000..b595bcbd18 --- /dev/null +++ b/tests/baselines/reference/controlFlowTruthiness.js @@ -0,0 +1,134 @@ +//// [controlFlowTruthiness.ts] + +declare function foo(): string | undefined; + +function f1() { + let x = foo(); + if (x) { + x; // string + } + else { + x; // string | undefined + } +} + +function f2() { + let x: string | undefined; + x = foo(); + if (x) { + x; // string + } + else { + x; // string | undefined + } +} + +function f3() { + let x: string | undefined; + if (x = foo()) { + x; // string + } + else { + x; // string | undefined + } +} + +function f4() { + let x: string | undefined; + if (!(x = foo())) { + x; // string | undefined + } + else { + x; // string + } +} + +function f5() { + let x: string | undefined; + let y: string | undefined; + if (x = y = foo()) { + x; // string + y; // string | undefined + } + else { + x; // string | undefined + y; // string | undefined + } +} + +function f6() { + let x: string | undefined; + let y: string | undefined; + if (x = foo(), y = foo()) { + x; // string | undefined + y; // string + } + else { + x; // string | undefined + y; // string | undefined + } +} + + +//// [controlFlowTruthiness.js] +function f1() { + var x = foo(); + if (x) { + x; // string + } + else { + x; // string | undefined + } +} +function f2() { + var x; + x = foo(); + if (x) { + x; // string + } + else { + x; // string | undefined + } +} +function f3() { + var x; + if (x = foo()) { + x; // string + } + else { + x; // string | undefined + } +} +function f4() { + var x; + if (!(x = foo())) { + x; // string | undefined + } + else { + x; // string + } +} +function f5() { + var x; + var y; + if (x = y = foo()) { + x; // string + y; // string | undefined + } + else { + x; // string | undefined + y; // string | undefined + } +} +function f6() { + var x; + var y; + if (x = foo(), y = foo()) { + x; // string | undefined + y; // string + } + else { + x; // string | undefined + y; // string | undefined + } +} diff --git a/tests/baselines/reference/controlFlowTruthiness.symbols b/tests/baselines/reference/controlFlowTruthiness.symbols new file mode 100644 index 0000000000..37c1fb135f --- /dev/null +++ b/tests/baselines/reference/controlFlowTruthiness.symbols @@ -0,0 +1,143 @@ +=== tests/cases/conformance/controlFlow/controlFlowTruthiness.ts === + +declare function foo(): string | undefined; +>foo : Symbol(foo, Decl(controlFlowTruthiness.ts, 0, 0)) + +function f1() { +>f1 : Symbol(f1, Decl(controlFlowTruthiness.ts, 1, 43)) + + let x = foo(); +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 4, 7)) +>foo : Symbol(foo, Decl(controlFlowTruthiness.ts, 0, 0)) + + if (x) { +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 4, 7)) + + x; // string +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 4, 7)) + } + else { + x; // string | undefined +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 4, 7)) + } +} + +function f2() { +>f2 : Symbol(f2, Decl(controlFlowTruthiness.ts, 11, 1)) + + let x: string | undefined; +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 14, 7)) + + x = foo(); +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 14, 7)) +>foo : Symbol(foo, Decl(controlFlowTruthiness.ts, 0, 0)) + + if (x) { +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 14, 7)) + + x; // string +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 14, 7)) + } + else { + x; // string | undefined +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 14, 7)) + } +} + +function f3() { +>f3 : Symbol(f3, Decl(controlFlowTruthiness.ts, 22, 1)) + + let x: string | undefined; +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 25, 7)) + + if (x = foo()) { +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 25, 7)) +>foo : Symbol(foo, Decl(controlFlowTruthiness.ts, 0, 0)) + + x; // string +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 25, 7)) + } + else { + x; // string | undefined +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 25, 7)) + } +} + +function f4() { +>f4 : Symbol(f4, Decl(controlFlowTruthiness.ts, 32, 1)) + + let x: string | undefined; +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 35, 7)) + + if (!(x = foo())) { +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 35, 7)) +>foo : Symbol(foo, Decl(controlFlowTruthiness.ts, 0, 0)) + + x; // string | undefined +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 35, 7)) + } + else { + x; // string +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 35, 7)) + } +} + +function f5() { +>f5 : Symbol(f5, Decl(controlFlowTruthiness.ts, 42, 1)) + + let x: string | undefined; +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 45, 7)) + + let y: string | undefined; +>y : Symbol(y, Decl(controlFlowTruthiness.ts, 46, 7)) + + if (x = y = foo()) { +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 45, 7)) +>y : Symbol(y, Decl(controlFlowTruthiness.ts, 46, 7)) +>foo : Symbol(foo, Decl(controlFlowTruthiness.ts, 0, 0)) + + x; // string +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 45, 7)) + + y; // string | undefined +>y : Symbol(y, Decl(controlFlowTruthiness.ts, 46, 7)) + } + else { + x; // string | undefined +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 45, 7)) + + y; // string | undefined +>y : Symbol(y, Decl(controlFlowTruthiness.ts, 46, 7)) + } +} + +function f6() { +>f6 : Symbol(f6, Decl(controlFlowTruthiness.ts, 55, 1)) + + let x: string | undefined; +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 58, 7)) + + let y: string | undefined; +>y : Symbol(y, Decl(controlFlowTruthiness.ts, 59, 7)) + + if (x = foo(), y = foo()) { +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 58, 7)) +>foo : Symbol(foo, Decl(controlFlowTruthiness.ts, 0, 0)) +>y : Symbol(y, Decl(controlFlowTruthiness.ts, 59, 7)) +>foo : Symbol(foo, Decl(controlFlowTruthiness.ts, 0, 0)) + + x; // string | undefined +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 58, 7)) + + y; // string +>y : Symbol(y, Decl(controlFlowTruthiness.ts, 59, 7)) + } + else { + x; // string | undefined +>x : Symbol(x, Decl(controlFlowTruthiness.ts, 58, 7)) + + y; // string | undefined +>y : Symbol(y, Decl(controlFlowTruthiness.ts, 59, 7)) + } +} + diff --git a/tests/baselines/reference/controlFlowTruthiness.types b/tests/baselines/reference/controlFlowTruthiness.types new file mode 100644 index 0000000000..2d35856fe8 --- /dev/null +++ b/tests/baselines/reference/controlFlowTruthiness.types @@ -0,0 +1,160 @@ +=== tests/cases/conformance/controlFlow/controlFlowTruthiness.ts === + +declare function foo(): string | undefined; +>foo : () => string | undefined + +function f1() { +>f1 : () => void + + let x = foo(); +>x : string | undefined +>foo() : string | undefined +>foo : () => string | undefined + + if (x) { +>x : string | undefined + + x; // string +>x : string + } + else { + x; // string | undefined +>x : string | undefined + } +} + +function f2() { +>f2 : () => void + + let x: string | undefined; +>x : string | undefined + + x = foo(); +>x = foo() : string | undefined +>x : string | undefined +>foo() : string | undefined +>foo : () => string | undefined + + if (x) { +>x : string | undefined + + x; // string +>x : string + } + else { + x; // string | undefined +>x : string | undefined + } +} + +function f3() { +>f3 : () => void + + let x: string | undefined; +>x : string | undefined + + if (x = foo()) { +>x = foo() : string | undefined +>x : string | undefined +>foo() : string | undefined +>foo : () => string | undefined + + x; // string +>x : string + } + else { + x; // string | undefined +>x : string | undefined + } +} + +function f4() { +>f4 : () => void + + let x: string | undefined; +>x : string | undefined + + if (!(x = foo())) { +>!(x = foo()) : boolean +>(x = foo()) : string | undefined +>x = foo() : string | undefined +>x : string | undefined +>foo() : string | undefined +>foo : () => string | undefined + + x; // string | undefined +>x : string | undefined + } + else { + x; // string +>x : string + } +} + +function f5() { +>f5 : () => void + + let x: string | undefined; +>x : string | undefined + + let y: string | undefined; +>y : string | undefined + + if (x = y = foo()) { +>x = y = foo() : string | undefined +>x : string | undefined +>y = foo() : string | undefined +>y : string | undefined +>foo() : string | undefined +>foo : () => string | undefined + + x; // string +>x : string + + y; // string | undefined +>y : string | undefined + } + else { + x; // string | undefined +>x : string | undefined + + y; // string | undefined +>y : string | undefined + } +} + +function f6() { +>f6 : () => void + + let x: string | undefined; +>x : string | undefined + + let y: string | undefined; +>y : string | undefined + + if (x = foo(), y = foo()) { +>x = foo(), y = foo() : string | undefined +>x = foo() : string | undefined +>x : string | undefined +>foo() : string | undefined +>foo : () => string | undefined +>y = foo() : string | undefined +>y : string | undefined +>foo() : string | undefined +>foo : () => string | undefined + + x; // string | undefined +>x : string | undefined + + y; // string +>y : string + } + else { + x; // string | undefined +>x : string | undefined + + y; // string | undefined +>y : string | undefined + } +} + diff --git a/tests/cases/conformance/controlFlow/controlFlowTruthiness.ts b/tests/cases/conformance/controlFlow/controlFlowTruthiness.ts new file mode 100644 index 0000000000..ba1947da13 --- /dev/null +++ b/tests/cases/conformance/controlFlow/controlFlowTruthiness.ts @@ -0,0 +1,70 @@ +// @strictNullChecks: true + +declare function foo(): string | undefined; + +function f1() { + let x = foo(); + if (x) { + x; // string + } + else { + x; // string | undefined + } +} + +function f2() { + let x: string | undefined; + x = foo(); + if (x) { + x; // string + } + else { + x; // string | undefined + } +} + +function f3() { + let x: string | undefined; + if (x = foo()) { + x; // string + } + else { + x; // string | undefined + } +} + +function f4() { + let x: string | undefined; + if (!(x = foo())) { + x; // string | undefined + } + else { + x; // string + } +} + +function f5() { + let x: string | undefined; + let y: string | undefined; + if (x = y = foo()) { + x; // string + y; // string | undefined + } + else { + x; // string | undefined + y; // string | undefined + } +} + +function f6() { + let x: string | undefined; + let y: string | undefined; + if (x = foo(), y = foo()) { + x; // string | undefined + y; // string + } + else { + x; // string | undefined + y; // string | undefined + } +}