Skip to content

Commit 4f1d7a5

Browse files
authored
Merge pull request microsoft#14338 from Microsoft/fix14334
Fix emit for downlevel generated catch variable
2 parents 36eb1ce + 8c2bcae commit 4f1d7a5

File tree

2 files changed

+73
-66
lines changed

2 files changed

+73
-66
lines changed

src/compiler/transformers/generators.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,7 +1942,7 @@ namespace ts {
19421942
}
19431943

19441944
function substituteExpressionIdentifier(node: Identifier) {
1945-
if (renamedCatchVariables && renamedCatchVariables.has(node.text)) {
1945+
if (!isGeneratedIdentifier(node) && renamedCatchVariables && renamedCatchVariables.has(node.text)) {
19461946
const original = getOriginalNode(node);
19471947
if (isIdentifier(original) && original.parent) {
19481948
const declaration = resolver.getReferencedValueDeclaration(original);
@@ -2108,17 +2108,24 @@ namespace ts {
21082108
function beginCatchBlock(variable: VariableDeclaration): void {
21092109
Debug.assert(peekBlockKind() === CodeBlockKind.Exception);
21102110

2111-
const text = (<Identifier>variable.name).text;
2112-
const name = declareLocal(text);
2113-
2114-
if (!renamedCatchVariables) {
2115-
renamedCatchVariables = createMap<boolean>();
2116-
renamedCatchVariableDeclarations = [];
2117-
context.enableSubstitution(SyntaxKind.Identifier);
2111+
// generated identifiers should already be unique within a file
2112+
let name: Identifier;
2113+
if (isGeneratedIdentifier(variable.name)) {
2114+
name = variable.name;
2115+
hoistVariableDeclaration(variable.name);
21182116
}
2117+
else {
2118+
const text = (<Identifier>variable.name).text;
2119+
name = declareLocal(text);
2120+
if (!renamedCatchVariables) {
2121+
renamedCatchVariables = createMap<boolean>();
2122+
renamedCatchVariableDeclarations = [];
2123+
context.enableSubstitution(SyntaxKind.Identifier);
2124+
}
21192125

2120-
renamedCatchVariables.set(text, true);
2121-
renamedCatchVariableDeclarations[getOriginalNodeId(variable)] = name;
2126+
renamedCatchVariables.set(text, true);
2127+
renamedCatchVariableDeclarations[getOriginalNodeId(variable)] = name;
2128+
}
21222129

21232130
const exception = <ExceptionBlock>peekBlock();
21242131
Debug.assert(exception.state < ExceptionBlockState.Catch);

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

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -68,36 +68,36 @@ var __asyncValues = (this && this.__asyncIterator) || function (o) {
6868
};
6969
function f1() {
7070
return __awaiter(this, void 0, void 0, function () {
71-
var y, y_1, y_1_1, x, _a, e_1, _b;
72-
return __generator(this, function (_c) {
73-
switch (_c.label) {
71+
var y, y_1, y_1_1, x, e_1_1, e_1, _a;
72+
return __generator(this, function (_b) {
73+
switch (_b.label) {
7474
case 0:
75-
_c.trys.push([0, 6, 7, 12]);
75+
_b.trys.push([0, 6, 7, 12]);
7676
y_1 = __asyncValues(y);
7777
return [4 /*yield*/, y_1.next()];
7878
case 1:
79-
y_1_1 = _c.sent();
80-
_c.label = 2;
79+
y_1_1 = _b.sent();
80+
_b.label = 2;
8181
case 2:
8282
if (!!y_1_1.done) return [3 /*break*/, 5];
8383
x = y_1_1.value;
84-
_c.label = 3;
84+
_b.label = 3;
8585
case 3: return [4 /*yield*/, y_1.next()];
8686
case 4:
87-
y_1_1 = _c.sent();
87+
y_1_1 = _b.sent();
8888
return [3 /*break*/, 2];
8989
case 5: return [3 /*break*/, 12];
9090
case 6:
91-
_a = _c.sent();
91+
e_1_1 = _b.sent();
9292
e_1 = { error: e_1_1 };
9393
return [3 /*break*/, 12];
9494
case 7:
95-
_c.trys.push([7, , 10, 11]);
96-
if (!(y_1_1 && !y_1_1.done && (_b = y_1.return))) return [3 /*break*/, 9];
97-
return [4 /*yield*/, _b.call(y_1)];
95+
_b.trys.push([7, , 10, 11]);
96+
if (!(y_1_1 && !y_1_1.done && (_a = y_1.return))) return [3 /*break*/, 9];
97+
return [4 /*yield*/, _a.call(y_1)];
9898
case 8:
99-
_c.sent();
100-
_c.label = 9;
99+
_b.sent();
100+
_b.label = 9;
101101
case 9: return [3 /*break*/, 11];
102102
case 10:
103103
if (e_1) throw e_1.error;
@@ -151,36 +151,36 @@ var __asyncValues = (this && this.__asyncIterator) || function (o) {
151151
};
152152
function f2() {
153153
return __awaiter(this, void 0, void 0, function () {
154-
var x, y, y_1, y_1_1, _a, e_1, _b;
155-
return __generator(this, function (_c) {
156-
switch (_c.label) {
154+
var x, y, y_1, y_1_1, e_1_1, e_1, _a;
155+
return __generator(this, function (_b) {
156+
switch (_b.label) {
157157
case 0:
158-
_c.trys.push([0, 6, 7, 12]);
158+
_b.trys.push([0, 6, 7, 12]);
159159
y_1 = __asyncValues(y);
160160
return [4 /*yield*/, y_1.next()];
161161
case 1:
162-
y_1_1 = _c.sent();
163-
_c.label = 2;
162+
y_1_1 = _b.sent();
163+
_b.label = 2;
164164
case 2:
165165
if (!!y_1_1.done) return [3 /*break*/, 5];
166166
x = y_1_1.value;
167-
_c.label = 3;
167+
_b.label = 3;
168168
case 3: return [4 /*yield*/, y_1.next()];
169169
case 4:
170-
y_1_1 = _c.sent();
170+
y_1_1 = _b.sent();
171171
return [3 /*break*/, 2];
172172
case 5: return [3 /*break*/, 12];
173173
case 6:
174-
_a = _c.sent();
174+
e_1_1 = _b.sent();
175175
e_1 = { error: e_1_1 };
176176
return [3 /*break*/, 12];
177177
case 7:
178-
_c.trys.push([7, , 10, 11]);
179-
if (!(y_1_1 && !y_1_1.done && (_b = y_1.return))) return [3 /*break*/, 9];
180-
return [4 /*yield*/, _b.call(y_1)];
178+
_b.trys.push([7, , 10, 11]);
179+
if (!(y_1_1 && !y_1_1.done && (_a = y_1.return))) return [3 /*break*/, 9];
180+
return [4 /*yield*/, _a.call(y_1)];
181181
case 8:
182-
_c.sent();
183-
_c.label = 9;
182+
_b.sent();
183+
_b.label = 9;
184184
case 9: return [3 /*break*/, 11];
185185
case 10:
186186
if (e_1) throw e_1.error;
@@ -239,36 +239,36 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
239239
};
240240
function f3() {
241241
return __asyncGenerator(this, arguments, function f3_1() {
242-
var y, y_1, y_1_1, x, _a, e_1, _b;
243-
return __generator(this, function (_c) {
244-
switch (_c.label) {
242+
var y, y_1, y_1_1, x, e_1_1, e_1, _a;
243+
return __generator(this, function (_b) {
244+
switch (_b.label) {
245245
case 0:
246-
_c.trys.push([0, 6, 7, 12]);
246+
_b.trys.push([0, 6, 7, 12]);
247247
y_1 = __asyncValues(y);
248248
return [4 /*yield*/, ["await", y_1.next()]];
249249
case 1:
250-
y_1_1 = _c.sent();
251-
_c.label = 2;
250+
y_1_1 = _b.sent();
251+
_b.label = 2;
252252
case 2:
253253
if (!!y_1_1.done) return [3 /*break*/, 5];
254254
x = y_1_1.value;
255-
_c.label = 3;
255+
_b.label = 3;
256256
case 3: return [4 /*yield*/, ["await", y_1.next()]];
257257
case 4:
258-
y_1_1 = _c.sent();
258+
y_1_1 = _b.sent();
259259
return [3 /*break*/, 2];
260260
case 5: return [3 /*break*/, 12];
261261
case 6:
262-
_a = _c.sent();
262+
e_1_1 = _b.sent();
263263
e_1 = { error: e_1_1 };
264264
return [3 /*break*/, 12];
265265
case 7:
266-
_c.trys.push([7, , 10, 11]);
267-
if (!(y_1_1 && !y_1_1.done && (_b = y_1.return))) return [3 /*break*/, 9];
268-
return [4 /*yield*/, ["await", _b.call(y_1)]];
266+
_b.trys.push([7, , 10, 11]);
267+
if (!(y_1_1 && !y_1_1.done && (_a = y_1.return))) return [3 /*break*/, 9];
268+
return [4 /*yield*/, ["await", _a.call(y_1)]];
269269
case 8:
270-
_c.sent();
271-
_c.label = 9;
270+
_b.sent();
271+
_b.label = 9;
272272
case 9: return [3 /*break*/, 11];
273273
case 10:
274274
if (e_1) throw e_1.error;
@@ -327,36 +327,36 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
327327
};
328328
function f4() {
329329
return __asyncGenerator(this, arguments, function f4_1() {
330-
var x, y, y_1, y_1_1, _a, e_1, _b;
331-
return __generator(this, function (_c) {
332-
switch (_c.label) {
330+
var x, y, y_1, y_1_1, e_1_1, e_1, _a;
331+
return __generator(this, function (_b) {
332+
switch (_b.label) {
333333
case 0:
334-
_c.trys.push([0, 6, 7, 12]);
334+
_b.trys.push([0, 6, 7, 12]);
335335
y_1 = __asyncValues(y);
336336
return [4 /*yield*/, ["await", y_1.next()]];
337337
case 1:
338-
y_1_1 = _c.sent();
339-
_c.label = 2;
338+
y_1_1 = _b.sent();
339+
_b.label = 2;
340340
case 2:
341341
if (!!y_1_1.done) return [3 /*break*/, 5];
342342
x = y_1_1.value;
343-
_c.label = 3;
343+
_b.label = 3;
344344
case 3: return [4 /*yield*/, ["await", y_1.next()]];
345345
case 4:
346-
y_1_1 = _c.sent();
346+
y_1_1 = _b.sent();
347347
return [3 /*break*/, 2];
348348
case 5: return [3 /*break*/, 12];
349349
case 6:
350-
_a = _c.sent();
350+
e_1_1 = _b.sent();
351351
e_1 = { error: e_1_1 };
352352
return [3 /*break*/, 12];
353353
case 7:
354-
_c.trys.push([7, , 10, 11]);
355-
if (!(y_1_1 && !y_1_1.done && (_b = y_1.return))) return [3 /*break*/, 9];
356-
return [4 /*yield*/, ["await", _b.call(y_1)]];
354+
_b.trys.push([7, , 10, 11]);
355+
if (!(y_1_1 && !y_1_1.done && (_a = y_1.return))) return [3 /*break*/, 9];
356+
return [4 /*yield*/, ["await", _a.call(y_1)]];
357357
case 8:
358-
_c.sent();
359-
_c.label = 9;
358+
_b.sent();
359+
_b.label = 9;
360360
case 9: return [3 /*break*/, 11];
361361
case 10:
362362
if (e_1) throw e_1.error;

0 commit comments

Comments
 (0)