From 38ff7762ec3b9eccf63161ddb449ab9a86975401 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Fri, 24 Apr 2020 12:10:29 -0700 Subject: [PATCH] Fix temp variable scoping in async generators (#38121) --- src/compiler/transformers/es2018.ts | 2 +- ...OperatorInAsyncGenerator(target=es2015).js | 21 ++++++++++++ ...ingOperatorInAsyncGenerator(target=es5).js | 33 +++++++++++++++++++ ...OperatorInAsyncGenerator(target=esnext).js | 18 ++++++++++ ...llishCoalescingOperatorInAsyncGenerator.ts | 12 +++++++ 5 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/nullishCoalescingOperatorInAsyncGenerator(target=es2015).js create mode 100644 tests/baselines/reference/nullishCoalescingOperatorInAsyncGenerator(target=es5).js create mode 100644 tests/baselines/reference/nullishCoalescingOperatorInAsyncGenerator(target=esnext).js create mode 100644 tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingOperatorInAsyncGenerator.ts diff --git a/src/compiler/transformers/es2018.ts b/src/compiler/transformers/es2018.ts index 9050ebc601b..a3ae750f6e7 100644 --- a/src/compiler/transformers/es2018.ts +++ b/src/compiler/transformers/es2018.ts @@ -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"), diff --git a/tests/baselines/reference/nullishCoalescingOperatorInAsyncGenerator(target=es2015).js b/tests/baselines/reference/nullishCoalescingOperatorInAsyncGenerator(target=es2015).js new file mode 100644 index 00000000000..6cbc459a37f --- /dev/null +++ b/tests/baselines/reference/nullishCoalescingOperatorInAsyncGenerator(target=es2015).js @@ -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--); + } + }); +} diff --git a/tests/baselines/reference/nullishCoalescingOperatorInAsyncGenerator(target=es5).js b/tests/baselines/reference/nullishCoalescingOperatorInAsyncGenerator(target=es5).js new file mode 100644 index 00000000000..972189a54d3 --- /dev/null +++ b/tests/baselines/reference/nullishCoalescingOperatorInAsyncGenerator(target=es5).js @@ -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*/]; + } + }); + }); +} diff --git a/tests/baselines/reference/nullishCoalescingOperatorInAsyncGenerator(target=esnext).js b/tests/baselines/reference/nullishCoalescingOperatorInAsyncGenerator(target=esnext).js new file mode 100644 index 00000000000..430355c3d42 --- /dev/null +++ b/tests/baselines/reference/nullishCoalescingOperatorInAsyncGenerator(target=esnext).js @@ -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--; + } +} diff --git a/tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingOperatorInAsyncGenerator.ts b/tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingOperatorInAsyncGenerator.ts new file mode 100644 index 00000000000..54f6923c3e6 --- /dev/null +++ b/tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingOperatorInAsyncGenerator.ts @@ -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--; + } +}