Fix 'var' hoisting in 'if' in SystemJS emit (#54016)

This commit is contained in:
Ron Buckton
2023-04-25 18:03:39 -04:00
committed by GitHub
parent d5d9171909
commit 5ad4ac8d11
3 changed files with 59 additions and 0 deletions
@@ -45,6 +45,7 @@ import {
hasSyntacticModifier,
Identifier,
idText,
IfStatement,
ImportCall,
ImportDeclaration,
ImportEqualsDeclaration,
@@ -1210,6 +1211,9 @@ export function transformSystemModule(context: TransformationContext): (x: Sourc
case SyntaxKind.WithStatement:
return visitWithStatement(node as WithStatement);
case SyntaxKind.IfStatement:
return visitIfStatement(node as IfStatement);
case SyntaxKind.SwitchStatement:
return visitSwitchStatement(node as SwitchStatement);
@@ -1383,6 +1387,20 @@ export function transformSystemModule(context: TransformationContext): (x: Sourc
);
}
/**
* Visits the body of a IfStatement to hoist declarations.
*
* @param node The node to visit.
*/
function visitIfStatement(node: IfStatement): VisitResult<Statement> {
return factory.updateIfStatement(
node,
visitNode(node.expression, visitor, isExpression),
Debug.checkDefined(visitNode(node.thenStatement, topLevelNestedVisitor, isStatement, factory.liftToBlock)),
visitNode(node.elseStatement, topLevelNestedVisitor, isStatement, factory.liftToBlock)
);
}
/**
* Visits the body of a SwitchStatement to hoist declarations.
*
@@ -0,0 +1,29 @@
//// [topLevelVarHoistingSystem.ts]
if (false) {
var y = 1;
}
function f() {
console.log(y);
}
export { y };
//// [topLevelVarHoistingSystem.js]
System.register([], function (exports_1, context_1) {
"use strict";
var y;
var __moduleName = context_1 && context_1.id;
function f() {
console.log(y);
}
return {
setters: [],
execute: function () {
if (false) {
y = 1;
exports_1("y", y);
}
}
};
});
@@ -0,0 +1,12 @@
// @target: esnext
// @module: system
// @noTypesAndSymbols: true
if (false) {
var y = 1;
}
function f() {
console.log(y);
}
export { y };