Fix class emit in converted loop body (#36795)

This commit is contained in:
Ron Buckton
2020-02-24 10:55:13 -08:00
committed by GitHub
parent fcd55c21a1
commit fd8000dd59
5 changed files with 110 additions and 1 deletions

View File

@@ -2002,10 +2002,16 @@ namespace ts {
return visitEachChild(node, visitor, context);
}
function isVariableStatementOfTypeScriptClassWrapper(node: VariableStatement) {
return node.declarationList.declarations.length === 1
&& !!node.declarationList.declarations[0].initializer
&& !!(getEmitFlags(node.declarationList.declarations[0].initializer) & EmitFlags.TypeScriptClassWrapper);
}
function visitVariableStatement(node: VariableStatement): Statement | undefined {
const ancestorFacts = enterSubtree(HierarchyFacts.None, hasModifier(node, ModifierFlags.Export) ? HierarchyFacts.ExportedVariableStatement : HierarchyFacts.None);
let updated: Statement | undefined;
if (convertedLoopState && (node.declarationList.flags & NodeFlags.BlockScoped) === 0) {
if (convertedLoopState && (node.declarationList.flags & NodeFlags.BlockScoped) === 0 && !isVariableStatementOfTypeScriptClassWrapper(node)) {
// we are inside a converted loop - hoist variable declarations
let assignments: Expression[] | undefined;
for (const decl of node.declarationList.declarations) {
@@ -3606,7 +3612,13 @@ namespace ts {
// The class statements are the statements generated by visiting the first statement with initializer of the
// body (1), while all other statements are added to remainingStatements (2)
const isVariableStatementWithInitializer = (stmt: Statement) => isVariableStatement(stmt) && !!first(stmt.declarationList.declarations).initializer;
// visit the class body statements outside of any converted loop body.
const savedConvertedLoopState = convertedLoopState;
convertedLoopState = undefined;
const bodyStatements = visitNodes(body.statements, visitor, isStatement);
convertedLoopState = savedConvertedLoopState;
const classStatements = filter(bodyStatements, isVariableStatementWithInitializer);
const remainingStatements = filter(bodyStatements, stmt => !isVariableStatementWithInitializer(stmt));
const varStatement = cast(first(classStatements), isVariableStatement);