From 5ad4ac8d111830267ab6b2dd950385f019e3c5b5 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Tue, 25 Apr 2023 18:03:39 -0400 Subject: [PATCH] Fix 'var' hoisting in 'if' in SystemJS emit (#54016) --- src/compiler/transformers/module/system.ts | 18 ++++++++++++ .../reference/topLevelVarHoistingSystem.js | 29 +++++++++++++++++++ .../topLevelVarHoistingSystem.ts | 12 ++++++++ 3 files changed, 59 insertions(+) create mode 100644 tests/baselines/reference/topLevelVarHoistingSystem.js create mode 100644 tests/cases/conformance/es6/moduleExportsSystem/topLevelVarHoistingSystem.ts diff --git a/src/compiler/transformers/module/system.ts b/src/compiler/transformers/module/system.ts index f87307b5f31..4e2d9b954e6 100644 --- a/src/compiler/transformers/module/system.ts +++ b/src/compiler/transformers/module/system.ts @@ -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 { + 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. * diff --git a/tests/baselines/reference/topLevelVarHoistingSystem.js b/tests/baselines/reference/topLevelVarHoistingSystem.js new file mode 100644 index 00000000000..9cbad7f1401 --- /dev/null +++ b/tests/baselines/reference/topLevelVarHoistingSystem.js @@ -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); + } + } + }; +}); diff --git a/tests/cases/conformance/es6/moduleExportsSystem/topLevelVarHoistingSystem.ts b/tests/cases/conformance/es6/moduleExportsSystem/topLevelVarHoistingSystem.ts new file mode 100644 index 00000000000..496ee04ef66 --- /dev/null +++ b/tests/cases/conformance/es6/moduleExportsSystem/topLevelVarHoistingSystem.ts @@ -0,0 +1,12 @@ +// @target: esnext +// @module: system +// @noTypesAndSymbols: true +if (false) { + var y = 1; +} + +function f() { + console.log(y); +} + +export { y }; \ No newline at end of file