@@ -2517,29 +2517,6 @@ namespace ts {
25172517 || compilerOptions . isolatedModules ;
25182518 }
25192519
2520- function shouldEmitVarForEnumDeclaration ( node : EnumDeclaration | ModuleDeclaration ) {
2521- return isFirstEmittedDeclarationInScope ( node )
2522- && ( ! hasModifier ( node , ModifierFlags . Export )
2523- || isES6ExportedDeclaration ( node ) ) ;
2524- }
2525-
2526-
2527- /*
2528- * Adds a trailing VariableStatement for an enum or module declaration.
2529- */
2530- function addVarForEnumExportedFromNamespace ( statements : Statement [ ] , node : EnumDeclaration | ModuleDeclaration ) {
2531- const statement = createVariableStatement (
2532- /*modifiers*/ undefined ,
2533- [ createVariableDeclaration (
2534- getDeclarationName ( node ) ,
2535- /*type*/ undefined ,
2536- getExportName ( node )
2537- ) ]
2538- ) ;
2539- setSourceMapRange ( statement , node ) ;
2540- statements . push ( statement ) ;
2541- }
2542-
25432520 /**
25442521 * Visits an enum declaration.
25452522 *
@@ -2562,7 +2539,7 @@ namespace ts {
25622539 // a leading variable declaration, we should not emit leading comments for the
25632540 // enum body.
25642541 recordEmittedDeclarationInScope ( node ) ;
2565- if ( shouldEmitVarForEnumDeclaration ( node ) ) {
2542+ if ( isFirstEmittedDeclarationInScope ( node ) ) {
25662543 addVarForEnumOrModuleDeclaration ( statements , node ) ;
25672544
25682545 // We should still emit the comments if we are emitting a system module.
@@ -2580,6 +2557,25 @@ namespace ts {
25802557 // `exportName` is the expression used within this node's container for any exported references.
25812558 const exportName = getExportName ( node ) ;
25822559
2560+ // x || (x = {})
2561+ // exports.x || (exports.x = {})
2562+ let moduleArg =
2563+ createLogicalOr (
2564+ exportName ,
2565+ createAssignment (
2566+ exportName ,
2567+ createObjectLiteral ( )
2568+ )
2569+ ) ;
2570+
2571+ if ( hasModifier ( node , ModifierFlags . Export ) && ! isES6ExportedDeclaration ( node ) ) {
2572+ // `localName` is the expression used within this node's containing scope for any local references.
2573+ const localName = getLocalName ( node ) ;
2574+
2575+ // x = (exports.x || (exports.x = {}))
2576+ moduleArg = createAssignment ( localName , moduleArg ) ;
2577+ }
2578+
25832579 // (function (x) {
25842580 // x[x["y"] = 0] = "y";
25852581 // ...
@@ -2596,25 +2592,14 @@ namespace ts {
25962592 transformEnumBody ( node , containerName )
25972593 ) ,
25982594 /*typeArguments*/ undefined ,
2599- [ createLogicalOr (
2600- exportName ,
2601- createAssignment (
2602- exportName ,
2603- createObjectLiteral ( )
2604- )
2605- ) ]
2595+ [ moduleArg ]
26062596 ) ,
26072597 /*location*/ node
26082598 ) ;
26092599
26102600 setOriginalNode ( enumStatement , node ) ;
26112601 setEmitFlags ( enumStatement , emitFlags ) ;
26122602 statements . push ( enumStatement ) ;
2613-
2614- if ( isNamespaceExport ( node ) ) {
2615- addVarForEnumExportedFromNamespace ( statements , node ) ;
2616- }
2617-
26182603 return statements ;
26192604 }
26202605
@@ -2739,10 +2724,6 @@ namespace ts {
27392724 return false ;
27402725 }
27412726
2742- function shouldEmitVarForModuleDeclaration ( node : ModuleDeclaration ) {
2743- return isFirstEmittedDeclarationInScope ( node ) ;
2744- }
2745-
27462727 /**
27472728 * Adds a leading VariableStatement for a enum or module declaration.
27482729 */
@@ -2817,7 +2798,7 @@ namespace ts {
28172798 // a leading variable declaration, we should not emit leading comments for the
28182799 // module body.
28192800 recordEmittedDeclarationInScope ( node ) ;
2820- if ( shouldEmitVarForModuleDeclaration ( node ) ) {
2801+ if ( isFirstEmittedDeclarationInScope ( node ) ) {
28212802 addVarForEnumOrModuleDeclaration ( statements , node ) ;
28222803 // We should still emit the comments if we are emitting a system module.
28232804 if ( moduleKind !== ModuleKind . System || currentScope !== currentSourceFile ) {
0 commit comments