@@ -2595,28 +2595,7 @@ namespace ts {
25952595 const containerName = getNamespaceContainerName ( node ) ;
25962596
25972597 // `exportName` is the expression used within this node's container for any exported references.
2598- const exportName = hasModifier ( node , ModifierFlags . Export )
2599- ? getExternalModuleOrNamespaceExportName ( currentNamespaceContainerName , node , /*allowComments*/ false , /*allowSourceMaps*/ true )
2600- : getLocalName ( node , /*allowComments*/ false , /*allowSourceMaps*/ true ) ;
2601-
2602- // x || (x = {})
2603- // exports.x || (exports.x = {})
2604- let moduleArg =
2605- createLogicalOr (
2606- exportName ,
2607- createAssignment (
2608- exportName ,
2609- createObjectLiteral ( )
2610- )
2611- ) ;
2612-
2613- if ( hasNamespaceQualifiedExportName ( node ) ) {
2614- // `localName` is the expression used within this node's containing scope for any local references.
2615- const localName = getLocalName ( node , /*allowComments*/ false , /*allowSourceMaps*/ true ) ;
2616-
2617- // x = (exports.x || (exports.x = {}))
2618- moduleArg = createAssignment ( localName , moduleArg ) ;
2619- }
2598+ const localName = getLocalName ( node , /*allowComments*/ false , /*allowSourceMaps*/ true ) ;
26202599
26212600 // (function (x) {
26222601 // x[x["y"] = 0] = "y";
@@ -2634,7 +2613,7 @@ namespace ts {
26342613 transformEnumBody ( node , containerName )
26352614 ) ,
26362615 /*typeArguments*/ undefined ,
2637- [ moduleArg ]
2616+ [ localName ]
26382617 )
26392618 ) ;
26402619
@@ -2742,13 +2721,13 @@ namespace ts {
27422721 * Determines whether an exported declaration will have a qualified export name (e.g. `f.x`
27432722 * or `exports.x`).
27442723 */
2745- function hasNamespaceQualifiedExportName ( node : Node ) {
2746- return isExportOfNamespace ( node )
2747- || ( isExternalModuleExport ( node )
2748- && moduleKind !== ModuleKind . ES2015
2749- && moduleKind !== ModuleKind . ESNext
2750- && moduleKind !== ModuleKind . System ) ;
2751- }
2724+ // function hasNamespaceQualifiedExportName(node: Node) {
2725+ // return isExportOfNamespace(node)
2726+ // || (isExternalModuleExport(node)
2727+ // && moduleKind !== ModuleKind.ES2015
2728+ // && moduleKind !== ModuleKind.ESNext
2729+ // && moduleKind !== ModuleKind.System);
2730+ // }
27522731
27532732 /**
27542733 * Records that a declaration was emitted in the current scope, if it was the first
@@ -2789,11 +2768,35 @@ namespace ts {
27892768 // Emit a variable statement for the module. We emit top-level enums as a `var`
27902769 // declaration to avoid static errors in global scripts scripts due to redeclaration.
27912770 // enums in any other scope are emitted as a `let` declaration.
2771+ const nodeModifierFlags = getModifierFlags ( node ) ;
2772+ const nodeIsExportDefault = ( nodeModifierFlags & ModifierFlags . Export ) && ( nodeModifierFlags & ModifierFlags . Default ) ;
2773+ function modifierVisitor ( node : Node ) : VisitResult < Node > {
2774+ if ( modifierToFlag ( node . kind ) & ModifierFlags . TypeScriptModifier ) {
2775+ return undefined ;
2776+ }
2777+ else if ( ( currentNamespace || nodeIsExportDefault ) && node . kind === SyntaxKind . ExportKeyword ) {
2778+ return undefined ;
2779+ }
2780+ if ( node . kind == SyntaxKind . DefaultKeyword ) {
2781+ return undefined ;
2782+ }
2783+ return node ;
2784+ }
2785+ const exportName = hasModifier ( node , ModifierFlags . Export )
2786+ ? getExternalModuleOrNamespaceExportName ( currentNamespaceContainerName , node , /*allowComments*/ false , /*allowSourceMaps*/ true )
2787+ : getLocalName ( node , /*allowComments*/ false , /*allowSourceMaps*/ true ) ;
2788+
2789+ const localName = getLocalName ( node , /*allowComments*/ false , /*allowSourceMaps*/ true ) ;
27922790 const statement = createVariableStatement (
27932791 visitNodes ( node . modifiers , modifierVisitor , isModifier ) ,
27942792 createVariableDeclarationList ( [
27952793 createVariableDeclaration (
2796- getLocalName ( node , /*allowComments*/ false , /*allowSourceMaps*/ true )
2794+ localName ,
2795+ undefined ,
2796+ createLogicalOr (
2797+ exportName ,
2798+ createObjectLiteral ( )
2799+ )
27972800 )
27982801 ] , currentScope . kind === SyntaxKind . SourceFile ? NodeFlags . None : NodeFlags . Let )
27992802 ) ;
@@ -2831,6 +2834,16 @@ namespace ts {
28312834 setCommentRange ( statement , node ) ;
28322835 setEmitFlags ( statement , EmitFlags . NoTrailingComments | EmitFlags . HasEndOfDeclarationMarker ) ;
28332836 statements . push ( statement ) ;
2837+ if ( nodeIsExportDefault ) {
2838+ statements . push ( createExportAssignment ( undefined , undefined , false , localName ) ) ;
2839+ }
2840+ if ( currentNamespace && ( nodeModifierFlags & ModifierFlags . Export ) ) {
2841+ statements . push (
2842+ createStatement (
2843+ createAssignment ( createPropertyAccess ( currentNamespaceContainerName , localName ) , localName )
2844+ )
2845+ ) ;
2846+ }
28342847 return true ;
28352848 }
28362849 else {
@@ -2883,28 +2896,7 @@ namespace ts {
28832896 const containerName = getNamespaceContainerName ( node ) ;
28842897
28852898 // `exportName` is the expression used within this node's container for any exported references.
2886- const exportName = hasModifier ( node , ModifierFlags . Export )
2887- ? getExternalModuleOrNamespaceExportName ( currentNamespaceContainerName , node , /*allowComments*/ false , /*allowSourceMaps*/ true )
2888- : getLocalName ( node , /*allowComments*/ false , /*allowSourceMaps*/ true ) ;
2889-
2890- // x || (x = {})
2891- // exports.x || (exports.x = {})
2892- let moduleArg =
2893- createLogicalOr (
2894- exportName ,
2895- createAssignment (
2896- exportName ,
2897- createObjectLiteral ( )
2898- )
2899- ) ;
2900-
2901- if ( hasNamespaceQualifiedExportName ( node ) ) {
2902- // `localName` is the expression used within this node's containing scope for any local references.
2903- const localName = getLocalName ( node , /*allowComments*/ false , /*allowSourceMaps*/ true ) ;
2904-
2905- // x = (exports.x || (exports.x = {}))
2906- moduleArg = createAssignment ( localName , moduleArg ) ;
2907- }
2899+ const localName = getLocalName ( node , /*allowComments*/ false , /*allowSourceMaps*/ true ) ;
29082900
29092901 // (function (x_1) {
29102902 // x_1.y = ...;
@@ -2921,7 +2913,7 @@ namespace ts {
29212913 transformModuleBody ( node , containerName )
29222914 ) ,
29232915 /*typeArguments*/ undefined ,
2924- [ moduleArg ]
2916+ [ localName ]
29252917 )
29262918 ) ;
29272919
0 commit comments