Fix temp variable scoping in async generators (#38121)

This commit is contained in:
Ron Buckton 2020-04-24 12:10:29 -07:00 committed by GitHub
parent 689822c183
commit 38ff7762ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 85 additions and 1 deletions

View File

@ -1115,7 +1115,7 @@ namespace ts {
context.requestEmitHelper(asyncGeneratorHelper);
// Mark this node as originally an async function
(generatorFunc.emitNode || (generatorFunc.emitNode = {} as EmitNode)).flags |= EmitFlags.AsyncFunctionBody;
(generatorFunc.emitNode || (generatorFunc.emitNode = {} as EmitNode)).flags |= EmitFlags.AsyncFunctionBody | EmitFlags.ReuseTempVariableScope;
return createCall(
getUnscopedHelperName("__asyncGenerator"),

View File

@ -0,0 +1,21 @@
//// [nullishCoalescingOperatorInAsyncGenerator.ts]
// https://github.com/microsoft/TypeScript/issues/37686
async function* f(a: { b?: number }) {
let c = a.b ?? 10;
while (c) {
yield c--;
}
}
//// [nullishCoalescingOperatorInAsyncGenerator.js]
// https://github.com/microsoft/TypeScript/issues/37686
function f(a) {
var _a;
return __asyncGenerator(this, arguments, function* f_1() {
let c = (_a = a.b) !== null && _a !== void 0 ? _a : 10;
while (c) {
yield yield __await(c--);
}
});
}

View File

@ -0,0 +1,33 @@
//// [nullishCoalescingOperatorInAsyncGenerator.ts]
// https://github.com/microsoft/TypeScript/issues/37686
async function* f(a: { b?: number }) {
let c = a.b ?? 10;
while (c) {
yield c--;
}
}
//// [nullishCoalescingOperatorInAsyncGenerator.js]
// https://github.com/microsoft/TypeScript/issues/37686
function f(a) {
var _a;
return __asyncGenerator(this, arguments, function f_1() {
var c;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
c = (_a = a.b) !== null && _a !== void 0 ? _a : 10;
_b.label = 1;
case 1:
if (!c) return [3 /*break*/, 4];
return [4 /*yield*/, __await(c--)];
case 2: return [4 /*yield*/, _b.sent()];
case 3:
_b.sent();
return [3 /*break*/, 1];
case 4: return [2 /*return*/];
}
});
});
}

View File

@ -0,0 +1,18 @@
//// [nullishCoalescingOperatorInAsyncGenerator.ts]
// https://github.com/microsoft/TypeScript/issues/37686
async function* f(a: { b?: number }) {
let c = a.b ?? 10;
while (c) {
yield c--;
}
}
//// [nullishCoalescingOperatorInAsyncGenerator.js]
// https://github.com/microsoft/TypeScript/issues/37686
async function* f(a) {
let c = a.b ?? 10;
while (c) {
yield c--;
}
}

View File

@ -0,0 +1,12 @@
// @target: esnext,es2015,es5
// @lib: esnext
// @noEmitHelpers: true
// @noTypesAndSymbols: true
// https://github.com/microsoft/TypeScript/issues/37686
async function* f(a: { b?: number }) {
let c = a.b ?? 10;
while (c) {
yield c--;
}
}