Skip to content

Commit 5caebaf

Browse files
committed
Fix down-level emit for captured loop variable in async function
1 parent 154d25c commit 5caebaf

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

src/compiler/transformers/es2015.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2869,7 +2869,12 @@ namespace ts {
28692869
!state.labeledNonLocalContinues;
28702870

28712871
const call = createCall(loopFunctionExpressionName, /*typeArguments*/ undefined, map(parameters, p => <Identifier>p.name));
2872-
const callResult = isAsyncBlockContainingAwait ? createYield(createToken(SyntaxKind.AsteriskToken), call) : call;
2872+
const callResult = isAsyncBlockContainingAwait
2873+
? createYield(
2874+
createToken(SyntaxKind.AsteriskToken),
2875+
setEmitFlags(call, EmitFlags.Iterator)
2876+
)
2877+
: call;
28732878
if (isSimpleLoop) {
28742879
statements.push(createStatement(callResult));
28752880
copyOutParameters(state.loopOutParameters, CopyDirection.ToOriginal, statements);

src/compiler/transformers/generators.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,10 @@ namespace ts {
939939
const resumeLabel = defineLabel();
940940
const expression = visitNode(node.expression, visitor, isExpression);
941941
if (node.asteriskToken) {
942-
emitYieldStar(createValuesHelper(context, expression, /*location*/ node), /*location*/ node);
942+
const iterator = (getEmitFlags(node.expression) & EmitFlags.Iterator) === 0
943+
? createValuesHelper(context, expression, /*location*/ node)
944+
: expression;
945+
emitYieldStar(iterator, /*location*/ node);
943946
}
944947
else {
945948
emitYield(expression, /*location*/ node);

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3925,6 +3925,7 @@ namespace ts {
39253925
CustomPrologue = 1 << 19, // Treat the statement as if it were a prologue directive (NOTE: Prologue directives are *not* transformed).
39263926
NoHoisting = 1 << 20, // Do not hoist this declaration in --module system
39273927
HasEndOfDeclarationMarker = 1 << 21, // Declaration has an associated NotEmittedStatement to mark the end of the declaration
3928+
Iterator = 1 << 22, // The expression to a `yield*` should be treated as an Iterator when down-leveling, not an Iterable.
39283929
}
39293930

39303931
export interface EmitHelper {

tests/baselines/reference/asyncAwaitWithCapturedBlockScopeVar.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function fn1() {
5858
_a.label = 1;
5959
case 1:
6060
if (!(i < 1)) return [3 /*break*/, 4];
61-
return [5 /*yield**/, __values(_loop_1(i))];
61+
return [5 /*yield**/, _loop_1(i)];
6262
case 2:
6363
_a.sent();
6464
_a.label = 3;
@@ -92,7 +92,7 @@ function fn2() {
9292
_a.label = 1;
9393
case 1:
9494
if (!(i < 1)) return [3 /*break*/, 4];
95-
return [5 /*yield**/, __values(_loop_2(i))];
95+
return [5 /*yield**/, _loop_2(i)];
9696
case 2:
9797
state_1 = _a.sent();
9898
if (state_1 === "break")
@@ -128,7 +128,7 @@ function fn3() {
128128
_a.label = 1;
129129
case 1:
130130
if (!(i < 1)) return [3 /*break*/, 4];
131-
return [5 /*yield**/, __values(_loop_3(i))];
131+
return [5 /*yield**/, _loop_3(i)];
132132
case 2:
133133
_a.sent();
134134
_a.label = 3;
@@ -162,7 +162,7 @@ function fn4() {
162162
_a.label = 1;
163163
case 1:
164164
if (!(i < 1)) return [3 /*break*/, 4];
165-
return [5 /*yield**/, __values(_loop_4(i))];
165+
return [5 /*yield**/, _loop_4(i)];
166166
case 2:
167167
state_2 = _a.sent();
168168
if (typeof state_2 === "object")

0 commit comments

Comments
 (0)