Merge pull request #15144 from Microsoft/fix14357

Fix down-level emit for captured loop variable in async function
This commit is contained in:
Ron Buckton 2017-04-12 11:10:05 -07:00 committed by GitHub
commit 60e056a217
4 changed files with 15 additions and 6 deletions

View File

@ -2875,7 +2875,12 @@ namespace ts {
!state.labeledNonLocalContinues;
const call = createCall(loopFunctionExpressionName, /*typeArguments*/ undefined, map(parameters, p => <Identifier>p.name));
const callResult = isAsyncBlockContainingAwait ? createYield(createToken(SyntaxKind.AsteriskToken), call) : call;
const callResult = isAsyncBlockContainingAwait
? createYield(
createToken(SyntaxKind.AsteriskToken),
setEmitFlags(call, EmitFlags.Iterator)
)
: call;
if (isSimpleLoop) {
statements.push(createStatement(callResult));
copyOutParameters(state.loopOutParameters, CopyDirection.ToOriginal, statements);

View File

@ -939,7 +939,10 @@ namespace ts {
const resumeLabel = defineLabel();
const expression = visitNode(node.expression, visitor, isExpression);
if (node.asteriskToken) {
emitYieldStar(createValuesHelper(context, expression, /*location*/ node), /*location*/ node);
const iterator = (getEmitFlags(node.expression) & EmitFlags.Iterator) === 0
? createValuesHelper(context, expression, /*location*/ node)
: expression;
emitYieldStar(iterator, /*location*/ node);
}
else {
emitYield(expression, /*location*/ node);

View File

@ -3937,6 +3937,7 @@ namespace ts {
CustomPrologue = 1 << 19, // Treat the statement as if it were a prologue directive (NOTE: Prologue directives are *not* transformed).
NoHoisting = 1 << 20, // Do not hoist this declaration in --module system
HasEndOfDeclarationMarker = 1 << 21, // Declaration has an associated NotEmittedStatement to mark the end of the declaration
Iterator = 1 << 22, // The expression to a `yield*` should be treated as an Iterator when down-leveling, not an Iterable.
}
export interface EmitHelper {

View File

@ -58,7 +58,7 @@ function fn1() {
_a.label = 1;
case 1:
if (!(i < 1)) return [3 /*break*/, 4];
return [5 /*yield**/, __values(_loop_1(i))];
return [5 /*yield**/, _loop_1(i)];
case 2:
_a.sent();
_a.label = 3;
@ -92,7 +92,7 @@ function fn2() {
_a.label = 1;
case 1:
if (!(i < 1)) return [3 /*break*/, 4];
return [5 /*yield**/, __values(_loop_2(i))];
return [5 /*yield**/, _loop_2(i)];
case 2:
state_1 = _a.sent();
if (state_1 === "break")
@ -128,7 +128,7 @@ function fn3() {
_a.label = 1;
case 1:
if (!(i < 1)) return [3 /*break*/, 4];
return [5 /*yield**/, __values(_loop_3(i))];
return [5 /*yield**/, _loop_3(i)];
case 2:
_a.sent();
_a.label = 3;
@ -162,7 +162,7 @@ function fn4() {
_a.label = 1;
case 1:
if (!(i < 1)) return [3 /*break*/, 4];
return [5 /*yield**/, __values(_loop_4(i))];
return [5 /*yield**/, _loop_4(i)];
case 2:
state_2 = _a.sent();
if (typeof state_2 === "object")