@@ -2172,7 +2172,7 @@ namespace ts {
21722172 if ( ! isExported ( node ) || ( isExternalModuleExport ( node ) && isFirstDeclarationOfKind ( node , SyntaxKind . EnumDeclaration ) ) ) {
21732173 // Emit a VariableStatement if the enum is not exported, or is the first enum
21742174 // with the same name exported from an external module.
2175- addNode ( statements ,
2175+ statements . push (
21762176 createVariableStatement (
21772177 visitNodes ( node . modifiers , visitor , isModifier ) ,
21782178 createVariableDeclarationList ( [
@@ -2189,7 +2189,12 @@ namespace ts {
21892189 : getSynthesizedClone ( node . name ) ;
21902190
21912191 currentNamespaceLocalName = getGeneratedNameForNode ( node ) ;
2192- addNode ( statements ,
2192+
2193+ // (function (x) {
2194+ // x[x["y"] = 0] = "y";
2195+ // ...
2196+ // })(x || (x = {}));
2197+ statements . push (
21932198 createStatement (
21942199 createCall (
21952200 createParen (
@@ -2207,7 +2212,7 @@ namespace ts {
22072212 createObjectLiteral ( )
22082213 )
22092214 ) ]
2210- ) ,
2215+ ) ,
22112216 location
22122217 )
22132218 ) ;
@@ -2365,6 +2370,31 @@ namespace ts {
23652370 return child ;
23662371 }
23672372
2373+ /**
2374+ * Determines whether to elide a module declaration.
2375+ *
2376+ * @param node The module declaration node.
2377+ */
2378+ function shouldEmitModuleDeclaration ( node : ModuleDeclaration ) {
2379+ return isInstantiatedModule ( node , compilerOptions . preserveConstEnums || compilerOptions . isolatedModules ) ;
2380+ }
2381+
2382+ /**
2383+ * Determines whether a module declaration has a name that merges with a class declaration.
2384+ *
2385+ * @param node The module declaration node.
2386+ */
2387+ function isModuleMergedWithClass ( node : ModuleDeclaration ) {
2388+ return languageVersion === ScriptTarget . ES6
2389+ && ! ! ( resolver . getNodeCheckFlags ( getOriginalNode ( node ) ) & NodeCheckFlags . LexicalModuleMergesWithClass ) ;
2390+ }
2391+
2392+ function shouldEmitVarForModuleDeclaration ( node : ModuleDeclaration ) {
2393+ return ! isModuleMergedWithClass ( node )
2394+ && ( ! isExternalModuleExport ( node )
2395+ || isFirstDeclarationOfKind ( node , SyntaxKind . ModuleDeclaration ) ) ;
2396+ }
2397+
23682398 /**
23692399 * Visits a module declaration node.
23702400 *
@@ -2373,7 +2403,7 @@ namespace ts {
23732403 * @param node The module declaration node.
23742404 */
23752405 function visitModuleDeclaration ( node : ModuleDeclaration ) : VisitResult < Statement > {
2376- if ( shouldElideModuleDeclaration ( node ) ) {
2406+ if ( ! shouldEmitModuleDeclaration ( node ) ) {
23772407 return undefined ;
23782408 }
23792409
@@ -2382,43 +2412,37 @@ namespace ts {
23822412 enableExpressionSubstitutionForNamespaceExports ( ) ;
23832413
23842414 const savedCurrentNamespaceLocalName = currentNamespaceLocalName ;
2385- const modifiers = visitNodes ( node . modifiers , visitor , isModifier ) ;
2415+ const savedCurrentNamespace = currentNamespace ;
23862416 const statements : Statement [ ] = [ ] ;
23872417
2388- let location = node ;
2389- if ( ! isModuleMergedWithClass ( node ) ) {
2418+ if ( shouldEmitVarForModuleDeclaration ( node ) ) {
23902419 // var x;
23912420 statements . push (
23922421 createVariableStatement (
2393- modifiers ,
2422+ visitNodes ( node . modifiers , visitor , isModifier ) ,
23942423 createVariableDeclarationList ( [
23952424 createVariableDeclaration ( < Identifier > node . name )
23962425 ] ) ,
2397- location
2426+ /* location*/ node
23982427 )
23992428 ) ;
2400- location = undefined ;
24012429 }
24022430
24032431 const name = isNamespaceExport ( node )
24042432 ? getNamespaceMemberName ( node . name )
24052433 : getSynthesizedClone ( node . name ) ;
24062434
2407- let moduleParam : Expression = createLogicalOr (
2435+ currentNamespaceLocalName = getGeneratedNameForNode ( node ) ;
2436+ currentNamespace = node ;
2437+
2438+ const moduleParam = createLogicalOr (
24082439 name ,
24092440 createAssignment (
24102441 name ,
2411- createObjectLiteral ( [ ] )
2442+ createObjectLiteral ( )
24122443 )
24132444 ) ;
24142445
2415- if ( isNamespaceExport ( node ) ) {
2416- moduleParam = createAssignment ( getSynthesizedClone ( node . name ) , moduleParam ) ;
2417- }
2418-
2419- currentNamespaceLocalName = getGeneratedNameForNode ( node ) ;
2420- currentNamespace = node ;
2421-
24222446 // (function (x_1) {
24232447 // x_1.y = ...;
24242448 // })(x || (x = {}));
@@ -2435,8 +2459,13 @@ namespace ts {
24352459 transformModuleBody ( node )
24362460 )
24372461 ) ,
2438- [ moduleParam ]
2439- )
2462+ [
2463+ isNamespaceExport ( node )
2464+ ? createAssignment ( getSynthesizedClone ( < Identifier > node . name ) , moduleParam )
2465+ : moduleParam
2466+ ]
2467+ ) ,
2468+ /*location*/ node
24402469 ) ,
24412470 node
24422471 ) ,
@@ -2445,6 +2474,7 @@ namespace ts {
24452474 ) ;
24462475
24472476 currentNamespaceLocalName = savedCurrentNamespaceLocalName ;
2477+ currentNamespace = savedCurrentNamespace ;
24482478 return statements ;
24492479 }
24502480
@@ -2468,24 +2498,6 @@ namespace ts {
24682498 return createBlock ( statements , /*location*/ undefined , /*multiLine*/ true ) ;
24692499 }
24702500
2471- /**
2472- * Determines whether to elide a module declaration.
2473- *
2474- * @param node The module declaration node.
2475- */
2476- function shouldElideModuleDeclaration ( node : ModuleDeclaration ) {
2477- return ! isInstantiatedModule ( node , compilerOptions . preserveConstEnums || compilerOptions . isolatedModules ) ;
2478- }
2479-
2480- /**
2481- * Determines whether a module declaration has a name that merges with a class declaration.
2482- *
2483- * @param node The module declaration node.
2484- */
2485- function isModuleMergedWithClass ( node : ModuleDeclaration ) {
2486- return ! ! ( resolver . getNodeCheckFlags ( getOriginalNode ( node ) ) & NodeCheckFlags . LexicalModuleMergesWithClass ) ;
2487- }
2488-
24892501 /**
24902502 * Visits an import equals declaration.
24912503 *
@@ -2584,7 +2596,6 @@ namespace ts {
25842596 return isExternalModuleExport ( node )
25852597 && hasModifier ( node , ModifierFlags . Default ) ;
25862598 }
2587-
25882599 /**
25892600 * Gets a value indicating whether a node is the first declaration of its kind.
25902601 *
0 commit comments