Skip to content

Commit fa94fb0

Browse files
author
Artem Tyurin
committed
Always yield await in async generator
1 parent 3e42791 commit fa94fb0

14 files changed

+137
-108
lines changed

src/compiler/transformers/esnext.ts

+33-17
Original file line numberDiff line numberDiff line change
@@ -118,28 +118,44 @@ namespace ts {
118118
}
119119

120120
function visitYieldExpression(node: YieldExpression) {
121-
if (enclosingFunctionFlags & FunctionFlags.Async && enclosingFunctionFlags & FunctionFlags.Generator && node.asteriskToken) {
122-
const expression = visitNode(node.expression, visitor, isExpression);
123-
return setOriginalNode(
124-
setTextRange(
125-
createYield(
126-
createAwaitHelper(context,
127-
updateYield(
128-
node,
129-
node.asteriskToken,
130-
createAsyncDelegatorHelper(
131-
context,
132-
createAsyncValuesHelper(context, expression, expression),
133-
expression
121+
if (enclosingFunctionFlags & FunctionFlags.Async && enclosingFunctionFlags & FunctionFlags.Generator) {
122+
if (node.asteriskToken) {
123+
const expression = visitNode(node.expression, visitor, isExpression);
124+
125+
return setOriginalNode(
126+
setTextRange(
127+
createYield(
128+
createAwaitHelper(context,
129+
updateYield(
130+
node,
131+
node.asteriskToken,
132+
createAsyncDelegatorHelper(
133+
context,
134+
createAsyncValuesHelper(context, expression, expression),
135+
expression
136+
)
134137
)
135138
)
136-
)
139+
),
140+
node
137141
),
138142
node
139-
),
140-
node
141-
);
143+
);
144+
}
145+
146+
if (node.expression && node.expression.kind !== SyntaxKind.AwaitExpression) {
147+
return setOriginalNode(
148+
setTextRange(
149+
createYield(
150+
createDownlevelAwait(node.expression)
151+
),
152+
node
153+
),
154+
node
155+
);
156+
}
142157
}
158+
143159
return visitEachChild(node, visitor, context);
144160
}
145161

tests/baselines/reference/asyncImportNestedYield.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ function foo() {
5454
return __asyncGenerator(this, arguments, function foo_1() {
5555
return __generator(this, function (_a) {
5656
switch (_a.label) {
57-
case 0: return [4 /*yield*/, "foo"];
58-
case 1: return [4 /*yield*/, __await.apply(void 0, [Promise.resolve().then(function () { return require(_a.sent()); })])];
59-
case 2:
57+
case 0: return [4 /*yield*/, __await("foo")];
58+
case 1: return [4 /*yield*/, _a.sent()];
59+
case 2: return [4 /*yield*/, __await.apply(void 0, [Promise.resolve().then(function () { return require(_a.sent()); })])];
60+
case 3:
6061
Promise.resolve().then(function () { return require((_a.sent())["default"]); });
6162
return [2 /*return*/];
6263
}

tests/baselines/reference/emitter.asyncGenerators.classMethods.es2015.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
136136
class C3 {
137137
f() {
138138
return __asyncGenerator(this, arguments, function* f_1() {
139-
const x = yield 1;
139+
const x = yield yield __await(1);
140140
});
141141
}
142142
}
@@ -210,7 +210,7 @@ var __asyncDelegator = (this && this.__asyncDelegator) || function (o) {
210210
class C5 {
211211
f() {
212212
return __asyncGenerator(this, arguments, function* f_1() {
213-
const x = yield __await(yield* __asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function* () { yield 1; }); })())));
213+
const x = yield __await(yield* __asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function* () { yield yield __await(1); }); })())));
214214
});
215215
}
216216
}

tests/baselines/reference/emitter.asyncGenerators.classMethods.es5.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,9 @@ var C3 = /** @class */ (function () {
239239
var x;
240240
return __generator(this, function (_a) {
241241
switch (_a.label) {
242-
case 0: return [4 /*yield*/, 1];
243-
case 1:
242+
case 0: return [4 /*yield*/, __await(1)];
243+
case 1: return [4 /*yield*/, _a.sent()];
244+
case 2:
244245
x = _a.sent();
245246
return [2 /*return*/];
246247
}
@@ -412,8 +413,9 @@ var C5 = /** @class */ (function () {
412413
switch (_a.label) {
413414
case 0: return [5 /*yield**/, __values(__asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function () { return __generator(this, function (_a) {
414415
switch (_a.label) {
415-
case 0: return [4 /*yield*/, 1];
416-
case 1:
416+
case 0: return [4 /*yield*/, __await(1)];
417+
case 1: return [4 /*yield*/, _a.sent()];
418+
case 2:
417419
_a.sent();
418420
return [2 /*return*/];
419421
}

tests/baselines/reference/emitter.asyncGenerators.functionDeclarations.es2015.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
100100
};
101101
function f3() {
102102
return __asyncGenerator(this, arguments, function* f3_1() {
103-
const x = yield 1;
103+
const x = yield yield __await(1);
104104
});
105105
}
106106
//// [F4.js]
@@ -170,7 +170,7 @@ var __asyncDelegator = (this && this.__asyncDelegator) || function (o) {
170170
};
171171
function f5() {
172172
return __asyncGenerator(this, arguments, function* f5_1() {
173-
const x = yield __await(yield* __asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function* () { yield 1; }); })())));
173+
const x = yield __await(yield* __asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function* () { yield yield __await(1); }); })())));
174174
});
175175
}
176176
//// [F6.js]

tests/baselines/reference/emitter.asyncGenerators.functionDeclarations.es5.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,9 @@ function f3() {
195195
var x;
196196
return __generator(this, function (_a) {
197197
switch (_a.label) {
198-
case 0: return [4 /*yield*/, 1];
199-
case 1:
198+
case 0: return [4 /*yield*/, __await(1)];
199+
case 1: return [4 /*yield*/, _a.sent()];
200+
case 2:
200201
x = _a.sent();
201202
return [2 /*return*/];
202203
}
@@ -358,8 +359,9 @@ function f5() {
358359
switch (_a.label) {
359360
case 0: return [5 /*yield**/, __values(__asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function () { return __generator(this, function (_a) {
360361
switch (_a.label) {
361-
case 0: return [4 /*yield*/, 1];
362-
case 1:
362+
case 0: return [4 /*yield*/, __await(1)];
363+
case 1: return [4 /*yield*/, _a.sent()];
364+
case 2:
363365
_a.sent();
364366
return [2 /*return*/];
365367
}

tests/baselines/reference/emitter.asyncGenerators.functionExpressions.es2015.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
100100
};
101101
const f3 = function () {
102102
return __asyncGenerator(this, arguments, function* () {
103-
const x = yield 1;
103+
const x = yield yield __await(1);
104104
});
105105
};
106106
//// [F4.js]
@@ -170,7 +170,7 @@ var __asyncDelegator = (this && this.__asyncDelegator) || function (o) {
170170
};
171171
const f5 = function () {
172172
return __asyncGenerator(this, arguments, function* () {
173-
const x = yield __await(yield* __asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function* () { yield 1; }); })())));
173+
const x = yield __await(yield* __asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function* () { yield yield __await(1); }); })())));
174174
});
175175
};
176176
//// [F6.js]

tests/baselines/reference/emitter.asyncGenerators.functionExpressions.es5.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,9 @@ var f3 = function () {
195195
var x;
196196
return __generator(this, function (_a) {
197197
switch (_a.label) {
198-
case 0: return [4 /*yield*/, 1];
199-
case 1:
198+
case 0: return [4 /*yield*/, __await(1)];
199+
case 1: return [4 /*yield*/, _a.sent()];
200+
case 2:
200201
x = _a.sent();
201202
return [2 /*return*/];
202203
}
@@ -358,8 +359,9 @@ var f5 = function () {
358359
switch (_a.label) {
359360
case 0: return [5 /*yield**/, __values(__asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function () { return __generator(this, function (_a) {
360361
switch (_a.label) {
361-
case 0: return [4 /*yield*/, 1];
362-
case 1:
362+
case 0: return [4 /*yield*/, __await(1)];
363+
case 1: return [4 /*yield*/, _a.sent()];
364+
case 2:
363365
_a.sent();
364366
return [2 /*return*/];
365367
}

tests/baselines/reference/emitter.asyncGenerators.objectLiteralMethods.es2015.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
119119
const o3 = {
120120
f() {
121121
return __asyncGenerator(this, arguments, function* f_1() {
122-
const x = yield 1;
122+
const x = yield yield __await(1);
123123
});
124124
}
125125
};
@@ -193,7 +193,7 @@ var __asyncDelegator = (this && this.__asyncDelegator) || function (o) {
193193
const o5 = {
194194
f() {
195195
return __asyncGenerator(this, arguments, function* f_1() {
196-
const x = yield __await(yield* __asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function* () { yield 1; }); })())));
196+
const x = yield __await(yield* __asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function* () { yield yield __await(1); }); })())));
197197
});
198198
}
199199
};

tests/baselines/reference/emitter.asyncGenerators.objectLiteralMethods.es5.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,9 @@ var o3 = {
214214
var x;
215215
return __generator(this, function (_a) {
216216
switch (_a.label) {
217-
case 0: return [4 /*yield*/, 1];
218-
case 1:
217+
case 0: return [4 /*yield*/, __await(1)];
218+
case 1: return [4 /*yield*/, _a.sent()];
219+
case 2:
219220
x = _a.sent();
220221
return [2 /*return*/];
221222
}
@@ -381,8 +382,9 @@ var o5 = {
381382
switch (_a.label) {
382383
case 0: return [5 /*yield**/, __values(__asyncDelegator(__asyncValues((function () { return __asyncGenerator(this, arguments, function () { return __generator(this, function (_a) {
383384
switch (_a.label) {
384-
case 0: return [4 /*yield*/, 1];
385-
case 1:
385+
case 0: return [4 /*yield*/, __await(1)];
386+
case 1: return [4 /*yield*/, _a.sent()];
387+
case 2:
386388
_a.sent();
387389
return [2 /*return*/];
388390
}

tests/baselines/reference/emitter.forAwait.es2015.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function f3() {
135135
let y;
136136
try {
137137
for (var y_1 = __asyncValues(y), y_1_1; y_1_1 = yield __await(y_1.next()), !y_1_1.done;) {
138-
const x = yield __await(y_1_1.value);
138+
const x = yield yield __await(__await(y_1_1.value));
139139
}
140140
}
141141
catch (e_1_1) { e_1 = { error: e_1_1 }; }
@@ -178,7 +178,7 @@ function f4() {
178178
let x, y;
179179
try {
180180
for (var y_1 = __asyncValues(y), y_1_1; y_1_1 = yield __await(y_1.next()), !y_1_1.done;) {
181-
x = yield __await(y_1_1.value);
181+
x = yield yield __await(__await(y_1_1.value));
182182
}
183183
}
184184
catch (e_1_1) { e_1 = { error: e_1_1 }; }
@@ -256,7 +256,7 @@ function f6() {
256256
let y;
257257
try {
258258
outer: for (var y_1 = __asyncValues(y), y_1_1; y_1_1 = yield __await(y_1.next()), !y_1_1.done;) {
259-
const x = yield __await(y_1_1.value);
259+
const x = yield yield __await(__await(y_1_1.value));
260260
continue outer;
261261
}
262262
}

tests/baselines/reference/emitter.forAwait.es2017.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ function f3() {
115115
let y;
116116
try {
117117
for (var y_1 = __asyncValues(y), y_1_1; y_1_1 = yield __await(y_1.next()), !y_1_1.done;) {
118-
const x = yield __await(y_1_1.value);
118+
const x = yield yield __await(__await(y_1_1.value));
119119
}
120120
}
121121
catch (e_1_1) { e_1 = { error: e_1_1 }; }
@@ -158,7 +158,7 @@ function f4() {
158158
let x, y;
159159
try {
160160
for (var y_1 = __asyncValues(y), y_1_1; y_1_1 = yield __await(y_1.next()), !y_1_1.done;) {
161-
x = yield __await(y_1_1.value);
161+
x = yield yield __await(__await(y_1_1.value));
162162
}
163163
}
164164
catch (e_1_1) { e_1 = { error: e_1_1 }; }
@@ -226,7 +226,7 @@ function f6() {
226226
let y;
227227
try {
228228
outer: for (var y_1 = __asyncValues(y), y_1_1; y_1_1 = yield __await(y_1.next()), !y_1_1.done;) {
229-
const x = yield __await(y_1_1.value);
229+
const x = yield yield __await(__await(y_1_1.value));
230230
continue outer;
231231
}
232232
}

0 commit comments

Comments
 (0)