Skip to content

Commit

Permalink
Fix 'var' hoisting in 'if' in SystemJS emit (#54016)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton authored Apr 25, 2023
1 parent d5d9171 commit 5ad4ac8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/compiler/transformers/module/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
hasSyntacticModifier,
Identifier,
idText,
IfStatement,
ImportCall,
ImportDeclaration,
ImportEqualsDeclaration,
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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.
*
Expand Down
29 changes: 29 additions & 0 deletions tests/baselines/reference/topLevelVarHoistingSystem.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
};
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @target: esnext
// @module: system
// @noTypesAndSymbols: true
if (false) {
var y = 1;
}

function f() {
console.log(y);
}

export { y };

0 comments on commit 5ad4ac8

Please sign in to comment.