Skip to content

Resolve promise inside async generator #23887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions src/compiler/transformers/esnext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,28 +118,46 @@ namespace ts {
}

function visitYieldExpression(node: YieldExpression) {
if (enclosingFunctionFlags & FunctionFlags.Async && enclosingFunctionFlags & FunctionFlags.Generator && node.asteriskToken) {
const expression = visitNode(node.expression, visitor, isExpression);
if (enclosingFunctionFlags & FunctionFlags.Async && enclosingFunctionFlags & FunctionFlags.Generator) {
if (node.asteriskToken) {
const expression = visitNode(node.expression, visitor, isExpression);

return setOriginalNode(
setTextRange(
createYield(
createAwaitHelper(context,
updateYield(
node,
node.asteriskToken,
createAsyncDelegatorHelper(
context,
createAsyncValuesHelper(context, expression, expression),
expression
)
)
)
),
node
),
node
);
}

return setOriginalNode(
setTextRange(
createYield(
createAwaitHelper(context,
updateYield(
node,
node.asteriskToken,
createAsyncDelegatorHelper(
context,
createAsyncValuesHelper(context, expression, expression),
expression
)
)
createDownlevelAwait(
node.expression
? visitNode(node.expression, visitor, isExpression)
: createVoidZero()
)
),
node
),
node
);
}

return visitEachChild(node, visitor, context);
}

Expand Down
7 changes: 4 additions & 3 deletions tests/baselines/reference/asyncImportNestedYield.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ function foo() {
return __asyncGenerator(this, arguments, function foo_1() {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, "foo"];
case 1: return [4 /*yield*/, __await.apply(void 0, [Promise.resolve().then(function () { return require(_a.sent()); })])];
case 2:
case 0: return [4 /*yield*/, __await("foo")];
case 1: return [4 /*yield*/, _a.sent()];
case 2: return [4 /*yield*/, __await.apply(void 0, [Promise.resolve().then(function () { return require(_a.sent()); })])];
case 3:
Promise.resolve().then(function () { return require((_a.sent())["default"]); });
return [2 /*return*/];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
class C2 {
f() {
return __asyncGenerator(this, arguments, function* f_1() {
const x = yield;
const x = yield yield __await(void 0);
});
}
}
Expand All @@ -115,7 +115,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
class C3 {
f() {
return __asyncGenerator(this, arguments, function* f_1() {
const x = yield 1;
const x = yield yield __await(1);
});
}
}
Expand Down Expand Up @@ -175,7 +175,7 @@ var __asyncDelegator = (this && this.__asyncDelegator) || function (o) {
class C5 {
f() {
return __asyncGenerator(this, arguments, function* f_1() {
const x = yield __await(yield* __asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function* () { yield 1; }); })())));
const x = yield __await(yield* __asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function* () { yield yield __await(1); }); })())));
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,9 @@ var C2 = /** @class */ (function () {
var x;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/];
case 1:
case 0: return [4 /*yield*/, __await(void 0)];
case 1: return [4 /*yield*/, _a.sent()];
case 2:
x = _a.sent();
return [2 /*return*/];
}
Expand Down Expand Up @@ -218,8 +219,9 @@ var C3 = /** @class */ (function () {
var x;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, 1];
case 1:
case 0: return [4 /*yield*/, __await(1)];
case 1: return [4 /*yield*/, _a.sent()];
case 2:
x = _a.sent();
return [2 /*return*/];
}
Expand Down Expand Up @@ -377,8 +379,9 @@ var C5 = /** @class */ (function () {
switch (_a.label) {
case 0: return [5 /*yield**/, __values(__asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function () { return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, 1];
case 1:
case 0: return [4 /*yield*/, __await(1)];
case 1: return [4 /*yield*/, _a.sent()];
case 2:
_a.sent();
return [2 /*return*/];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
};
function f2() {
return __asyncGenerator(this, arguments, function* f2_1() {
const x = yield;
const x = yield yield __await(void 0);
});
}
//// [F3.js]
Expand All @@ -79,7 +79,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
};
function f3() {
return __asyncGenerator(this, arguments, function* f3_1() {
const x = yield 1;
const x = yield yield __await(1);
});
}
//// [F4.js]
Expand Down Expand Up @@ -135,7 +135,7 @@ var __asyncDelegator = (this && this.__asyncDelegator) || function (o) {
};
function f5() {
return __asyncGenerator(this, arguments, function* f5_1() {
const x = yield __await(yield* __asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function* () { yield 1; }); })())));
const x = yield __await(yield* __asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function* () { yield yield __await(1); }); })())));
});
}
//// [F6.js]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ function f2() {
var x;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/];
case 1:
case 0: return [4 /*yield*/, __await(void 0)];
case 1: return [4 /*yield*/, _a.sent()];
case 2:
x = _a.sent();
return [2 /*return*/];
}
Expand Down Expand Up @@ -174,8 +175,9 @@ function f3() {
var x;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, 1];
case 1:
case 0: return [4 /*yield*/, __await(1)];
case 1: return [4 /*yield*/, _a.sent()];
case 2:
x = _a.sent();
return [2 /*return*/];
}
Expand Down Expand Up @@ -323,8 +325,9 @@ function f5() {
switch (_a.label) {
case 0: return [5 /*yield**/, __values(__asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function () { return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, 1];
case 1:
case 0: return [4 /*yield*/, __await(1)];
case 1: return [4 /*yield*/, _a.sent()];
case 2:
_a.sent();
return [2 /*return*/];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
};
const f2 = function () {
return __asyncGenerator(this, arguments, function* () {
const x = yield;
const x = yield yield __await(void 0);
});
};
//// [F3.js]
Expand All @@ -79,7 +79,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
};
const f3 = function () {
return __asyncGenerator(this, arguments, function* () {
const x = yield 1;
const x = yield yield __await(1);
});
};
//// [F4.js]
Expand Down Expand Up @@ -135,7 +135,7 @@ var __asyncDelegator = (this && this.__asyncDelegator) || function (o) {
};
const f5 = function () {
return __asyncGenerator(this, arguments, function* () {
const x = yield __await(yield* __asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function* () { yield 1; }); })())));
const x = yield __await(yield* __asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function* () { yield yield __await(1); }); })())));
});
};
//// [F6.js]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ var f2 = function () {
var x;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/];
case 1:
case 0: return [4 /*yield*/, __await(void 0)];
case 1: return [4 /*yield*/, _a.sent()];
case 2:
x = _a.sent();
return [2 /*return*/];
}
Expand Down Expand Up @@ -174,8 +175,9 @@ var f3 = function () {
var x;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, 1];
case 1:
case 0: return [4 /*yield*/, __await(1)];
case 1: return [4 /*yield*/, _a.sent()];
case 2:
x = _a.sent();
return [2 /*return*/];
}
Expand Down Expand Up @@ -323,8 +325,9 @@ var f5 = function () {
switch (_a.label) {
case 0: return [5 /*yield**/, __values(__asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function () { return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, 1];
case 1:
case 0: return [4 /*yield*/, __await(1)];
case 1: return [4 /*yield*/, _a.sent()];
case 2:
_a.sent();
return [2 /*return*/];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
const o2 = {
f() {
return __asyncGenerator(this, arguments, function* f_1() {
const x = yield;
const x = yield yield __await(void 0);
});
}
};
Expand All @@ -98,7 +98,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
const o3 = {
f() {
return __asyncGenerator(this, arguments, function* f_1() {
const x = yield 1;
const x = yield yield __await(1);
});
}
};
Expand Down Expand Up @@ -158,7 +158,7 @@ var __asyncDelegator = (this && this.__asyncDelegator) || function (o) {
const o5 = {
f() {
return __asyncGenerator(this, arguments, function* f_1() {
const x = yield __await(yield* __asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function* () { yield 1; }); })())));
const x = yield __await(yield* __asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function* () { yield yield __await(1); }); })())));
});
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,9 @@ var o2 = {
var x;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/];
case 1:
case 0: return [4 /*yield*/, __await(void 0)];
case 1: return [4 /*yield*/, _a.sent()];
case 2:
x = _a.sent();
return [2 /*return*/];
}
Expand Down Expand Up @@ -193,8 +194,9 @@ var o3 = {
var x;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, 1];
case 1:
case 0: return [4 /*yield*/, __await(1)];
case 1: return [4 /*yield*/, _a.sent()];
case 2:
x = _a.sent();
return [2 /*return*/];
}
Expand Down Expand Up @@ -346,8 +348,9 @@ var o5 = {
switch (_a.label) {
case 0: return [5 /*yield**/, __values(__asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function () { return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, 1];
case 1:
case 0: return [4 /*yield*/, __await(1)];
case 1: return [4 /*yield*/, _a.sent()];
case 2:
_a.sent();
return [2 /*return*/];
}
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/emitter.forAwait.es2015.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function f3() {
let y;
try {
for (var y_1 = __asyncValues(y), y_1_1; y_1_1 = yield __await(y_1.next()), !y_1_1.done;) {
const x = yield __await(y_1_1.value);
const x = yield yield __await(__await(y_1_1.value));
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
Expand Down Expand Up @@ -164,7 +164,7 @@ function f4() {
let x, y;
try {
for (var y_1 = __asyncValues(y), y_1_1; y_1_1 = yield __await(y_1.next()), !y_1_1.done;) {
x = yield __await(y_1_1.value);
x = yield yield __await(__await(y_1_1.value));
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
Expand Down Expand Up @@ -235,7 +235,7 @@ function f6() {
let y;
try {
outer: for (var y_1 = __asyncValues(y), y_1_1; y_1_1 = yield __await(y_1.next()), !y_1_1.done;) {
const x = yield __await(y_1_1.value);
const x = yield yield __await(__await(y_1_1.value));
continue outer;
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/emitter.forAwait.es2017.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function f3() {
let y;
try {
for (var y_1 = __asyncValues(y), y_1_1; y_1_1 = yield __await(y_1.next()), !y_1_1.done;) {
const x = yield __await(y_1_1.value);
const x = yield yield __await(__await(y_1_1.value));
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
Expand Down Expand Up @@ -144,7 +144,7 @@ function f4() {
let x, y;
try {
for (var y_1 = __asyncValues(y), y_1_1; y_1_1 = yield __await(y_1.next()), !y_1_1.done;) {
x = yield __await(y_1_1.value);
x = yield yield __await(__await(y_1_1.value));
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
Expand Down Expand Up @@ -205,7 +205,7 @@ function f6() {
let y;
try {
outer: for (var y_1 = __asyncValues(y), y_1_1; y_1_1 = yield __await(y_1.next()), !y_1_1.done;) {
const x = yield __await(y_1_1.value);
const x = yield yield __await(__await(y_1_1.value));
continue outer;
}
}
Expand Down
Loading