Skip to content

Commit 33ece12

Browse files
committed
Add test cases for top level "for await of".
1 parent a718929 commit 33ece12

20 files changed

+315
-7
lines changed
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
tests/cases/conformance/externalModules/topLevelAwait.ts(2,1): error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.
2+
tests/cases/conformance/externalModules/topLevelAwait.ts(6,5): error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.
23

34

4-
==== tests/cases/conformance/externalModules/topLevelAwait.ts (1 errors) ====
5+
==== tests/cases/conformance/externalModules/topLevelAwait.ts (2 errors) ====
56
export const x = 1;
67
await x;
78
~~~~~
89
!!! error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.
10+
11+
const arr = [Promise.resolve()];
12+
13+
for await (const item of arr) {
14+
~~~~~
15+
!!! error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.
16+
item;
17+
}
918

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,36 @@
11
//// [topLevelAwait.ts]
22
export const x = 1;
33
await x;
4+
5+
const arr = [Promise.resolve()];
6+
7+
for await (const item of arr) {
8+
item;
9+
}
410

511

612
//// [topLevelAwait.js]
13+
var __asyncValues = (this && this.__asyncValues) || function (o) {
14+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
15+
var m = o[Symbol.asyncIterator], i;
16+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
17+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
18+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
19+
};
20+
var e_1, _a;
721
export const x = 1;
822
await x;
23+
const arr = [Promise.resolve()];
24+
try {
25+
for (var arr_1 = __asyncValues(arr), arr_1_1; arr_1_1 = await arr_1.next(), !arr_1_1.done;) {
26+
const item = arr_1_1.value;
27+
item;
28+
}
29+
}
30+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
31+
finally {
32+
try {
33+
if (arr_1_1 && !arr_1_1.done && (_a = arr_1.return)) await _a.call(arr_1);
34+
}
35+
finally { if (e_1) throw e_1.error; }
36+
}

tests/baselines/reference/topLevelAwait(module=esnext,target=es2015).symbols

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,17 @@ export const x = 1;
55
await x;
66
>x : Symbol(x, Decl(topLevelAwait.ts, 0, 12))
77

8+
const arr = [Promise.resolve()];
9+
>arr : Symbol(arr, Decl(topLevelAwait.ts, 3, 5))
10+
>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
11+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
12+
>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
13+
14+
for await (const item of arr) {
15+
>item : Symbol(item, Decl(topLevelAwait.ts, 5, 16))
16+
>arr : Symbol(arr, Decl(topLevelAwait.ts, 3, 5))
17+
18+
item;
19+
>item : Symbol(item, Decl(topLevelAwait.ts, 5, 16))
20+
}
21+

tests/baselines/reference/topLevelAwait(module=esnext,target=es2015).types

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,19 @@ await x;
77
>await x : 1
88
>x : 1
99

10+
const arr = [Promise.resolve()];
11+
>arr : Promise<void>[]
12+
>[Promise.resolve()] : Promise<void>[]
13+
>Promise.resolve() : Promise<void>
14+
>Promise.resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
15+
>Promise : PromiseConstructor
16+
>resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
17+
18+
for await (const item of arr) {
19+
>item : void
20+
>arr : Promise<void>[]
21+
22+
item;
23+
>item : void
24+
}
25+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,36 @@
11
//// [topLevelAwait.ts]
22
export const x = 1;
33
await x;
4+
5+
const arr = [Promise.resolve()];
6+
7+
for await (const item of arr) {
8+
item;
9+
}
410

511

612
//// [topLevelAwait.js]
13+
var __asyncValues = (this && this.__asyncValues) || function (o) {
14+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
15+
var m = o[Symbol.asyncIterator], i;
16+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
17+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
18+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
19+
};
20+
var e_1, _a;
721
export const x = 1;
822
await x;
23+
const arr = [Promise.resolve()];
24+
try {
25+
for (var arr_1 = __asyncValues(arr), arr_1_1; arr_1_1 = await arr_1.next(), !arr_1_1.done;) {
26+
const item = arr_1_1.value;
27+
item;
28+
}
29+
}
30+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
31+
finally {
32+
try {
33+
if (arr_1_1 && !arr_1_1.done && (_a = arr_1.return)) await _a.call(arr_1);
34+
}
35+
finally { if (e_1) throw e_1.error; }
36+
}

tests/baselines/reference/topLevelAwait(module=esnext,target=es2017).symbols

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,17 @@ export const x = 1;
55
await x;
66
>x : Symbol(x, Decl(topLevelAwait.ts, 0, 12))
77

8+
const arr = [Promise.resolve()];
9+
>arr : Symbol(arr, Decl(topLevelAwait.ts, 3, 5))
10+
>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
11+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
12+
>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
13+
14+
for await (const item of arr) {
15+
>item : Symbol(item, Decl(topLevelAwait.ts, 5, 16))
16+
>arr : Symbol(arr, Decl(topLevelAwait.ts, 3, 5))
17+
18+
item;
19+
>item : Symbol(item, Decl(topLevelAwait.ts, 5, 16))
20+
}
21+

tests/baselines/reference/topLevelAwait(module=esnext,target=es2017).types

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,19 @@ await x;
77
>await x : 1
88
>x : 1
99

10+
const arr = [Promise.resolve()];
11+
>arr : Promise<void>[]
12+
>[Promise.resolve()] : Promise<void>[]
13+
>Promise.resolve() : Promise<void>
14+
>Promise.resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
15+
>Promise : PromiseConstructor
16+
>resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
17+
18+
for await (const item of arr) {
19+
>item : void
20+
>arr : Promise<void>[]
21+
22+
item;
23+
>item : void
24+
}
25+
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
tests/cases/conformance/externalModules/topLevelAwait.ts(2,1): error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.
2+
tests/cases/conformance/externalModules/topLevelAwait.ts(6,5): error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.
23

34

4-
==== tests/cases/conformance/externalModules/topLevelAwait.ts (1 errors) ====
5+
==== tests/cases/conformance/externalModules/topLevelAwait.ts (2 errors) ====
56
export const x = 1;
67
await x;
78
~~~~~
89
!!! error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.
10+
11+
const arr = [Promise.resolve()];
12+
13+
for await (const item of arr) {
14+
~~~~~
15+
!!! error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.
16+
item;
17+
}
918

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,45 @@
11
//// [topLevelAwait.ts]
22
export const x = 1;
33
await x;
4+
5+
const arr = [Promise.resolve()];
6+
7+
for await (const item of arr) {
8+
item;
9+
}
410

511

612
//// [topLevelAwait.js]
713
System.register([], function (exports_1, context_1) {
814
"use strict";
9-
var x;
15+
var __asyncValues = (this && this.__asyncValues) || function (o) {
16+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
17+
var m = o[Symbol.asyncIterator], i;
18+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
19+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
20+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
21+
};
22+
var e_1, _a, x, arr;
1023
var __moduleName = context_1 && context_1.id;
1124
return {
1225
setters: [],
1326
execute: async function () {
1427
exports_1("x", x = 1);
1528
await x;
29+
arr = [Promise.resolve()];
30+
try {
31+
for (var arr_1 = __asyncValues(arr), arr_1_1; arr_1_1 = await arr_1.next(), !arr_1_1.done;) {
32+
const item = arr_1_1.value;
33+
item;
34+
}
35+
}
36+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
37+
finally {
38+
try {
39+
if (arr_1_1 && !arr_1_1.done && (_a = arr_1.return)) await _a.call(arr_1);
40+
}
41+
finally { if (e_1) throw e_1.error; }
42+
}
1643
}
1744
};
1845
});

tests/baselines/reference/topLevelAwait(module=system,target=es2015).symbols

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,17 @@ export const x = 1;
55
await x;
66
>x : Symbol(x, Decl(topLevelAwait.ts, 0, 12))
77

8+
const arr = [Promise.resolve()];
9+
>arr : Symbol(arr, Decl(topLevelAwait.ts, 3, 5))
10+
>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
11+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
12+
>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
13+
14+
for await (const item of arr) {
15+
>item : Symbol(item, Decl(topLevelAwait.ts, 5, 16))
16+
>arr : Symbol(arr, Decl(topLevelAwait.ts, 3, 5))
17+
18+
item;
19+
>item : Symbol(item, Decl(topLevelAwait.ts, 5, 16))
20+
}
21+

tests/baselines/reference/topLevelAwait(module=system,target=es2015).types

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,19 @@ await x;
77
>await x : 1
88
>x : 1
99

10+
const arr = [Promise.resolve()];
11+
>arr : Promise<void>[]
12+
>[Promise.resolve()] : Promise<void>[]
13+
>Promise.resolve() : Promise<void>
14+
>Promise.resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
15+
>Promise : PromiseConstructor
16+
>resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
17+
18+
for await (const item of arr) {
19+
>item : void
20+
>arr : Promise<void>[]
21+
22+
item;
23+
>item : void
24+
}
25+
Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,45 @@
11
//// [topLevelAwait.ts]
22
export const x = 1;
33
await x;
4+
5+
const arr = [Promise.resolve()];
6+
7+
for await (const item of arr) {
8+
item;
9+
}
410

511

612
//// [topLevelAwait.js]
713
System.register([], function (exports_1, context_1) {
814
"use strict";
9-
var x;
15+
var __asyncValues = (this && this.__asyncValues) || function (o) {
16+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
17+
var m = o[Symbol.asyncIterator], i;
18+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
19+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
20+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
21+
};
22+
var e_1, _a, x, arr;
1023
var __moduleName = context_1 && context_1.id;
1124
return {
1225
setters: [],
1326
execute: async function () {
1427
exports_1("x", x = 1);
1528
await x;
29+
arr = [Promise.resolve()];
30+
try {
31+
for (var arr_1 = __asyncValues(arr), arr_1_1; arr_1_1 = await arr_1.next(), !arr_1_1.done;) {
32+
const item = arr_1_1.value;
33+
item;
34+
}
35+
}
36+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
37+
finally {
38+
try {
39+
if (arr_1_1 && !arr_1_1.done && (_a = arr_1.return)) await _a.call(arr_1);
40+
}
41+
finally { if (e_1) throw e_1.error; }
42+
}
1643
}
1744
};
1845
});

tests/baselines/reference/topLevelAwait(module=system,target=es2017).symbols

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,17 @@ export const x = 1;
55
await x;
66
>x : Symbol(x, Decl(topLevelAwait.ts, 0, 12))
77

8+
const arr = [Promise.resolve()];
9+
>arr : Symbol(arr, Decl(topLevelAwait.ts, 3, 5))
10+
>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
11+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
12+
>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
13+
14+
for await (const item of arr) {
15+
>item : Symbol(item, Decl(topLevelAwait.ts, 5, 16))
16+
>arr : Symbol(arr, Decl(topLevelAwait.ts, 3, 5))
17+
18+
item;
19+
>item : Symbol(item, Decl(topLevelAwait.ts, 5, 16))
20+
}
21+

tests/baselines/reference/topLevelAwait(module=system,target=es2017).types

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,19 @@ await x;
77
>await x : 1
88
>x : 1
99

10+
const arr = [Promise.resolve()];
11+
>arr : Promise<void>[]
12+
>[Promise.resolve()] : Promise<void>[]
13+
>Promise.resolve() : Promise<void>
14+
>Promise.resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
15+
>Promise : PromiseConstructor
16+
>resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
17+
18+
for await (const item of arr) {
19+
>item : void
20+
>arr : Promise<void>[]
21+
22+
item;
23+
>item : void
24+
}
25+
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
tests/cases/conformance/externalModules/topLevelAwaitNonModule.ts(1,1): error TS1375: 'await' expressions are only allowed at the top level of a file when that file is a module, but this file has no imports or exports. Consider adding an empty 'export {}' to make this file a module.
22
tests/cases/conformance/externalModules/topLevelAwaitNonModule.ts(1,7): error TS2304: Cannot find name 'x'.
3+
tests/cases/conformance/externalModules/topLevelAwaitNonModule.ts(5,5): error TS1375: 'await' expressions are only allowed at the top level of a file when that file is a module, but this file has no imports or exports. Consider adding an empty 'export {}' to make this file a module.
34

45

5-
==== tests/cases/conformance/externalModules/topLevelAwaitNonModule.ts (2 errors) ====
6+
==== tests/cases/conformance/externalModules/topLevelAwaitNonModule.ts (3 errors) ====
67
await x;
78
~~~~~
89
!!! error TS1375: 'await' expressions are only allowed at the top level of a file when that file is a module, but this file has no imports or exports. Consider adding an empty 'export {}' to make this file a module.
910
~
1011
!!! error TS2304: Cannot find name 'x'.
12+
13+
const arr = [Promise.resolve()];
14+
15+
for await (const item of arr) {
16+
~~~~~
17+
!!! error TS1375: 'await' expressions are only allowed at the top level of a file when that file is a module, but this file has no imports or exports. Consider adding an empty 'export {}' to make this file a module.
18+
item;
19+
}
1120

0 commit comments

Comments
 (0)