-
Notifications
You must be signed in to change notification settings - Fork 12.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7690 from ivogabe/controlFlowTypesTest
Adds tests to control flow types branch
- Loading branch information
Showing
14 changed files
with
388 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
tests/cases/conformance/controlFlow/controlFlowAssignmentExpression.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
let x: string | boolean | number; | ||
let obj: any; | ||
|
||
x = ""; | ||
x = x.length; | ||
x; // number | ||
|
||
x = true; | ||
(x = "", obj).foo = (x = x.length); | ||
x; // number |
9 changes: 9 additions & 0 deletions
9
tests/cases/conformance/controlFlow/controlFlowBinaryAndExpression.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
let x: string | number | boolean; | ||
let cond: boolean; | ||
|
||
(x = "") && (x = 0); | ||
x; // string | number | ||
|
||
x = ""; | ||
cond && (x = 0); | ||
x; // string | number |
9 changes: 9 additions & 0 deletions
9
tests/cases/conformance/controlFlow/controlFlowBinaryOrExpression.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
let x: string | number | boolean; | ||
let cond: boolean; | ||
|
||
(x = "") || (x = 0); | ||
x; // string | number | ||
|
||
x = ""; | ||
cond || (x = 0); | ||
x; // string | number |
5 changes: 5 additions & 0 deletions
5
tests/cases/conformance/controlFlow/controlFlowConditionalExpression.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
let x: string | number | boolean; | ||
let cond: boolean; | ||
|
||
cond ? x = "" : x = 3; | ||
x; // string | number |
76 changes: 76 additions & 0 deletions
76
tests/cases/conformance/controlFlow/controlFlowDoWhileStatement.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
let cond: boolean; | ||
function a() { | ||
let x: string | number; | ||
x = ""; | ||
do { | ||
x; // string | ||
} while (cond) | ||
} | ||
function b() { | ||
let x: string | number; | ||
x = ""; | ||
do { | ||
x; // string | ||
x = 42; | ||
break; | ||
} while (cond) | ||
} | ||
function c() { | ||
let x: string | number; | ||
x = ""; | ||
do { | ||
x; // string | ||
x = undefined; | ||
if (typeof x === "string") continue; | ||
break; | ||
} while (cond) | ||
} | ||
function d() { | ||
let x: string | number; | ||
x = 1000; | ||
do { | ||
x; // number | ||
x = ""; | ||
} while (x = x.length) | ||
x; // number | ||
} | ||
function e() { | ||
let x: string | number; | ||
x = ""; | ||
do { | ||
x = 42; | ||
} while (cond) | ||
x; // number | ||
} | ||
function f() { | ||
let x: string | number | boolean | RegExp | Function; | ||
x = ""; | ||
do { | ||
if (cond) { | ||
x = 42; | ||
break; | ||
} | ||
if (cond) { | ||
x = true; | ||
continue; | ||
} | ||
x = /a/; | ||
} while (cond) | ||
x; // number | boolean | RegExp | ||
} | ||
function g() { | ||
let x: string | number | boolean | RegExp | Function; | ||
x = ""; | ||
do { | ||
if (cond) { | ||
x = 42; | ||
break; | ||
} | ||
if (cond) { | ||
x = true; | ||
continue; | ||
} | ||
x = /a/; | ||
} while (true) | ||
x; // number | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/cases/conformance/controlFlow/controlFlowForInStatement.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
let x: string | number | boolean | RegExp | Function; | ||
let obj: any; | ||
let cond: boolean; | ||
|
||
x = /a/; | ||
for (let y in obj) { | ||
x = y; | ||
if (cond) { | ||
x = 42; | ||
continue; | ||
} | ||
if (cond) { | ||
x = true; | ||
break; | ||
} | ||
} | ||
x; // RegExp | string | number | boolean |
10 changes: 10 additions & 0 deletions
10
tests/cases/conformance/controlFlow/controlFlowForOfStatement.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
let obj: number[]; | ||
let x: string | number | boolean | RegExp; | ||
|
||
function a() { | ||
x = true; | ||
for (x of obj) { | ||
x = x.toExponential(); | ||
} | ||
x; // number | boolean | ||
} |
41 changes: 41 additions & 0 deletions
41
tests/cases/conformance/controlFlow/controlFlowForStatement.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
let cond: boolean; | ||
function a() { | ||
let x: string | number | boolean; | ||
for (x = ""; cond; x = 5) { | ||
x; // string | number | ||
} | ||
} | ||
function b() { | ||
let x: string | number | boolean; | ||
for (x = 5; cond; x = x.length) { | ||
x; // number | ||
x = ""; | ||
} | ||
} | ||
function c() { | ||
let x: string | number | boolean; | ||
for (x = 5; x = x.toExponential(); x = 5) { | ||
x; // string | ||
} | ||
} | ||
function d() { | ||
let x: string | number | boolean; | ||
for (x = ""; typeof x === "string"; x = 5) { | ||
x; // string | ||
} | ||
} | ||
function e() { | ||
let x: string | number | boolean | RegExp; | ||
for (x = "" || 0; typeof x !== "string"; x = "" || true) { | ||
x; // number | boolean | ||
} | ||
} | ||
function f() { | ||
let x: string | number | boolean; | ||
for (; typeof x !== "string";) { | ||
x; // number | boolean | ||
if (typeof x === "number") break; | ||
x = undefined; | ||
} | ||
x; // string | number | ||
} |
36 changes: 36 additions & 0 deletions
36
tests/cases/conformance/controlFlow/controlFlowIfStatement.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
let x: string | number | boolean | RegExp; | ||
let cond: boolean; | ||
|
||
x = /a/; | ||
if (x /* RegExp */, (x = true)) { | ||
x; // boolean | ||
x = ""; | ||
} | ||
else { | ||
x; // boolean | ||
x = 42; | ||
} | ||
x; // string | number | ||
|
||
function a() { | ||
let x: string | number; | ||
if (cond) { | ||
x = 42; | ||
} | ||
else { | ||
x = ""; | ||
return; | ||
} | ||
x; // number | ||
} | ||
function b() { | ||
let x: string | number; | ||
if (cond) { | ||
x = 42; | ||
throw ""; | ||
} | ||
else { | ||
x = ""; | ||
} | ||
x; // string | ||
} |
75 changes: 75 additions & 0 deletions
75
tests/cases/conformance/controlFlow/controlFlowWhileStatement.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
let cond: boolean; | ||
function a() { | ||
let x: string | number; | ||
x = ""; | ||
while (cond) { | ||
x; // string | ||
} | ||
} | ||
function b() { | ||
let x: string | number; | ||
x = ""; | ||
while (cond) { | ||
x; // string | ||
x = 42; | ||
break; | ||
} | ||
} | ||
function c() { | ||
let x: string | number; | ||
x = ""; | ||
while (cond) { | ||
x; // string | ||
x = undefined; | ||
if (typeof x === "string") continue; | ||
break; | ||
} | ||
} | ||
function d() { | ||
let x: string | number; | ||
x = ""; | ||
while (x = x.length) { | ||
x; // number | ||
x = ""; | ||
} | ||
} | ||
function e() { | ||
let x: string | number; | ||
x = ""; | ||
while (cond) { | ||
x = 42; | ||
} | ||
x; // string | number | ||
} | ||
function f() { | ||
let x: string | number | boolean | RegExp | Function; | ||
x = ""; | ||
while (cond) { | ||
if (cond) { | ||
x = 42; | ||
break; | ||
} | ||
if (cond) { | ||
x = true; | ||
continue; | ||
} | ||
x = /a/; | ||
} | ||
x; // string | number | boolean | RegExp | ||
} | ||
function g() { | ||
let x: string | number | boolean | RegExp | Function; | ||
x = ""; | ||
while (true) { | ||
if (cond) { | ||
x = 42; | ||
break; | ||
} | ||
if (cond) { | ||
x = true; | ||
continue; | ||
} | ||
x = /a/; | ||
} | ||
x; // number | ||
} |
28 changes: 28 additions & 0 deletions
28
tests/cases/conformance/expressions/assignmentOperator/assignmentTypeNarrowing.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
let x: string | number | boolean | RegExp; | ||
|
||
x = ""; | ||
x; // string | ||
|
||
[x] = [true]; | ||
x; // boolean | ||
|
||
[x = ""] = [1]; | ||
x; // string | number | ||
|
||
({x} = {x: true}); | ||
x; // boolean | ||
|
||
({y: x} = {y: 1}); | ||
x; // number | ||
|
||
({x = ""} = {x: true}); | ||
x; // string | boolean | ||
|
||
({y: x = /a/} = {y: 1}); | ||
x; // number | RegExp | ||
|
||
let a: string[]; | ||
|
||
for (x of a) { | ||
x; // string | ||
} |
27 changes: 27 additions & 0 deletions
27
tests/cases/conformance/expressions/typeGuards/typeGuardsInDoStatement.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
let cond: boolean; | ||
function a(x: string | number | boolean) { | ||
x = true; | ||
do { | ||
x; // boolean | string | ||
x = undefined; | ||
} while (typeof x === "string") | ||
x; // number | boolean | ||
} | ||
function b(x: string | number | boolean) { | ||
x = true; | ||
do { | ||
x; // boolean | string | ||
if (cond) continue; | ||
x = undefined; | ||
} while (typeof x === "string") | ||
x; // number | boolean | ||
} | ||
function c(x: string | number) { | ||
x = ""; | ||
do { | ||
x; // string | ||
if (cond) break; | ||
x = undefined; | ||
} while (typeof x === "string") | ||
x; // string | number | ||
} |
21 changes: 21 additions & 0 deletions
21
tests/cases/conformance/expressions/typeGuards/typeGuardsInForStatement.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
let cond: boolean; | ||
function a(x: string | number) { | ||
for (x = undefined; typeof x !== "number"; x = undefined) { | ||
x; // string | ||
} | ||
x; // number | ||
} | ||
function b(x: string | number) { | ||
for (x = undefined; typeof x !== "number"; x = undefined) { | ||
x; // string | ||
if (cond) continue; | ||
} | ||
x; // number | ||
} | ||
function c(x: string | number) { | ||
for (x = undefined; typeof x !== "number"; x = undefined) { | ||
x; // string | ||
if (cond) break; | ||
} | ||
x; // string | number | ||
} |
Oops, something went wrong.