Skip to content

Commit 38ff776

Browse files
authored
Fix temp variable scoping in async generators (microsoft#38121)
1 parent 689822c commit 38ff776

File tree

5 files changed

+85
-1
lines changed

5 files changed

+85
-1
lines changed

src/compiler/transformers/es2018.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,7 @@ namespace ts {
11151115
context.requestEmitHelper(asyncGeneratorHelper);
11161116

11171117
// Mark this node as originally an async function
1118-
(generatorFunc.emitNode || (generatorFunc.emitNode = {} as EmitNode)).flags |= EmitFlags.AsyncFunctionBody;
1118+
(generatorFunc.emitNode || (generatorFunc.emitNode = {} as EmitNode)).flags |= EmitFlags.AsyncFunctionBody | EmitFlags.ReuseTempVariableScope;
11191119

11201120
return createCall(
11211121
getUnscopedHelperName("__asyncGenerator"),
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [nullishCoalescingOperatorInAsyncGenerator.ts]
2+
// https://github.com/microsoft/TypeScript/issues/37686
3+
async function* f(a: { b?: number }) {
4+
let c = a.b ?? 10;
5+
while (c) {
6+
yield c--;
7+
}
8+
}
9+
10+
11+
//// [nullishCoalescingOperatorInAsyncGenerator.js]
12+
// https://github.com/microsoft/TypeScript/issues/37686
13+
function f(a) {
14+
var _a;
15+
return __asyncGenerator(this, arguments, function* f_1() {
16+
let c = (_a = a.b) !== null && _a !== void 0 ? _a : 10;
17+
while (c) {
18+
yield yield __await(c--);
19+
}
20+
});
21+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//// [nullishCoalescingOperatorInAsyncGenerator.ts]
2+
// https://github.com/microsoft/TypeScript/issues/37686
3+
async function* f(a: { b?: number }) {
4+
let c = a.b ?? 10;
5+
while (c) {
6+
yield c--;
7+
}
8+
}
9+
10+
11+
//// [nullishCoalescingOperatorInAsyncGenerator.js]
12+
// https://github.com/microsoft/TypeScript/issues/37686
13+
function f(a) {
14+
var _a;
15+
return __asyncGenerator(this, arguments, function f_1() {
16+
var c;
17+
return __generator(this, function (_b) {
18+
switch (_b.label) {
19+
case 0:
20+
c = (_a = a.b) !== null && _a !== void 0 ? _a : 10;
21+
_b.label = 1;
22+
case 1:
23+
if (!c) return [3 /*break*/, 4];
24+
return [4 /*yield*/, __await(c--)];
25+
case 2: return [4 /*yield*/, _b.sent()];
26+
case 3:
27+
_b.sent();
28+
return [3 /*break*/, 1];
29+
case 4: return [2 /*return*/];
30+
}
31+
});
32+
});
33+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [nullishCoalescingOperatorInAsyncGenerator.ts]
2+
// https://github.com/microsoft/TypeScript/issues/37686
3+
async function* f(a: { b?: number }) {
4+
let c = a.b ?? 10;
5+
while (c) {
6+
yield c--;
7+
}
8+
}
9+
10+
11+
//// [nullishCoalescingOperatorInAsyncGenerator.js]
12+
// https://github.com/microsoft/TypeScript/issues/37686
13+
async function* f(a) {
14+
let c = a.b ?? 10;
15+
while (c) {
16+
yield c--;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @target: esnext,es2015,es5
2+
// @lib: esnext
3+
// @noEmitHelpers: true
4+
// @noTypesAndSymbols: true
5+
6+
// https://github.com/microsoft/TypeScript/issues/37686
7+
async function* f(a: { b?: number }) {
8+
let c = a.b ?? 10;
9+
while (c) {
10+
yield c--;
11+
}
12+
}

0 commit comments

Comments
 (0)