Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fd8000d

Browse filesBrowse files
authored
Fix class emit in converted loop body (microsoft#36795)
1 parent fcd55c2 commit fd8000d
Copy full SHA for fd8000d

5 files changed

+110-1Lines changed: 110 additions & 1 deletion

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/compiler/transformers/es2015.ts‎

Copy file name to clipboardExpand all lines: src/compiler/transformers/es2015.ts
+13-1Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2002,10 +2002,16 @@ namespace ts {
20022002
return visitEachChild(node, visitor, context);
20032003
}
20042004

2005+
function isVariableStatementOfTypeScriptClassWrapper(node: VariableStatement) {
2006+
return node.declarationList.declarations.length === 1
2007+
&& !!node.declarationList.declarations[0].initializer
2008+
&& !!(getEmitFlags(node.declarationList.declarations[0].initializer) & EmitFlags.TypeScriptClassWrapper);
2009+
}
2010+
20052011
function visitVariableStatement(node: VariableStatement): Statement | undefined {
20062012
const ancestorFacts = enterSubtree(HierarchyFacts.None, hasModifier(node, ModifierFlags.Export) ? HierarchyFacts.ExportedVariableStatement : HierarchyFacts.None);
20072013
let updated: Statement | undefined;
2008-
if (convertedLoopState && (node.declarationList.flags & NodeFlags.BlockScoped) === 0) {
2014+
if (convertedLoopState && (node.declarationList.flags & NodeFlags.BlockScoped) === 0 && !isVariableStatementOfTypeScriptClassWrapper(node)) {
20092015
// we are inside a converted loop - hoist variable declarations
20102016
let assignments: Expression[] | undefined;
20112017
for (const decl of node.declarationList.declarations) {
@@ -3606,7 +3612,13 @@ namespace ts {
36063612
// The class statements are the statements generated by visiting the first statement with initializer of the
36073613
// body (1), while all other statements are added to remainingStatements (2)
36083614
const isVariableStatementWithInitializer = (stmt: Statement) => isVariableStatement(stmt) && !!first(stmt.declarationList.declarations).initializer;
3615+
3616+
// visit the class body statements outside of any converted loop body.
3617+
const savedConvertedLoopState = convertedLoopState;
3618+
convertedLoopState = undefined;
36093619
const bodyStatements = visitNodes(body.statements, visitor, isStatement);
3620+
convertedLoopState = savedConvertedLoopState;
3621+
36103622
const classStatements = filter(bodyStatements, isVariableStatementWithInitializer);
36113623
const remainingStatements = filter(bodyStatements, stmt => !isVariableStatementWithInitializer(stmt));
36123624
const varStatement = cast(first(classStatements), isVariableStatement);
Collapse file
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [classInConvertedLoopES5.ts]
2+
const classesByRow: Record<string, object> = {};
3+
for (const row of ['1', '2', '3', '4', '5']) {
4+
class RowClass {
5+
row = row;
6+
static factory = () => new RowClass();
7+
}
8+
9+
classesByRow[row] = RowClass;
10+
}
11+
12+
//// [classInConvertedLoopES5.js]
13+
var classesByRow = {};
14+
var _loop_1 = function (row) {
15+
var RowClass = /** @class */ (function () {
16+
function RowClass() {
17+
this.row = row;
18+
}
19+
RowClass.factory = function () { return new RowClass(); };
20+
return RowClass;
21+
}());
22+
classesByRow[row] = RowClass;
23+
};
24+
for (var _i = 0, _a = ['1', '2', '3', '4', '5']; _i < _a.length; _i++) {
25+
var row = _a[_i];
26+
_loop_1(row);
27+
}
Collapse file
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/compiler/classInConvertedLoopES5.ts ===
2+
const classesByRow: Record<string, object> = {};
3+
>classesByRow : Symbol(classesByRow, Decl(classInConvertedLoopES5.ts, 0, 5))
4+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
5+
6+
for (const row of ['1', '2', '3', '4', '5']) {
7+
>row : Symbol(row, Decl(classInConvertedLoopES5.ts, 1, 10))
8+
9+
class RowClass {
10+
>RowClass : Symbol(RowClass, Decl(classInConvertedLoopES5.ts, 1, 46))
11+
12+
row = row;
13+
>row : Symbol(RowClass.row, Decl(classInConvertedLoopES5.ts, 2, 18))
14+
>row : Symbol(row, Decl(classInConvertedLoopES5.ts, 1, 10))
15+
16+
static factory = () => new RowClass();
17+
>factory : Symbol(RowClass.factory, Decl(classInConvertedLoopES5.ts, 3, 14))
18+
>RowClass : Symbol(RowClass, Decl(classInConvertedLoopES5.ts, 1, 46))
19+
}
20+
21+
classesByRow[row] = RowClass;
22+
>classesByRow : Symbol(classesByRow, Decl(classInConvertedLoopES5.ts, 0, 5))
23+
>row : Symbol(row, Decl(classInConvertedLoopES5.ts, 1, 10))
24+
>RowClass : Symbol(RowClass, Decl(classInConvertedLoopES5.ts, 1, 46))
25+
}
Collapse file
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
=== tests/cases/compiler/classInConvertedLoopES5.ts ===
2+
const classesByRow: Record<string, object> = {};
3+
>classesByRow : Record<string, object>
4+
>{} : {}
5+
6+
for (const row of ['1', '2', '3', '4', '5']) {
7+
>row : string
8+
>['1', '2', '3', '4', '5'] : string[]
9+
>'1' : "1"
10+
>'2' : "2"
11+
>'3' : "3"
12+
>'4' : "4"
13+
>'5' : "5"
14+
15+
class RowClass {
16+
>RowClass : RowClass
17+
18+
row = row;
19+
>row : string
20+
>row : string
21+
22+
static factory = () => new RowClass();
23+
>factory : () => RowClass
24+
>() => new RowClass() : () => RowClass
25+
>new RowClass() : RowClass
26+
>RowClass : typeof RowClass
27+
}
28+
29+
classesByRow[row] = RowClass;
30+
>classesByRow[row] = RowClass : typeof RowClass
31+
>classesByRow[row] : object
32+
>classesByRow : Record<string, object>
33+
>row : string
34+
>RowClass : typeof RowClass
35+
}
Collapse file
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @target: es5
2+
const classesByRow: Record<string, object> = {};
3+
for (const row of ['1', '2', '3', '4', '5']) {
4+
class RowClass {
5+
row = row;
6+
static factory = () => new RowClass();
7+
}
8+
9+
classesByRow[row] = RowClass;
10+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.