@@ -333,7 +333,7 @@ namespace ts {
333333 if ( modifierToFlag ( node . kind ) & ModifierFlags . TypeScriptModifier ) {
334334 return undefined ;
335335 }
336- else if ( currentNamespace && node . kind === SyntaxKind . ExportKeyword ) {
336+ else if ( node . kind === SyntaxKind . ExportKeyword || node . kind === SyntaxKind . DefaultKeyword ) {
337337 return undefined ;
338338 }
339339
@@ -2594,29 +2594,7 @@ namespace ts {
25942594 // `containerName` is the expression used inside of the enum for assignments.
25952595 const containerName = getNamespaceContainerName ( node ) ;
25962596
2597- // `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- }
2597+ const localName = getLocalName ( node , /*allowComments*/ false , /*allowSourceMaps*/ true ) ;
26202598
26212599 // (function (x) {
26222600 // x[x["y"] = 0] = "y";
@@ -2634,18 +2612,19 @@ namespace ts {
26342612 transformEnumBody ( node , containerName )
26352613 ) ,
26362614 /*typeArguments*/ undefined ,
2637- [ moduleArg ]
2615+ [ localName ]
26382616 )
26392617 ) ;
26402618
26412619 setOriginalNode ( enumStatement , node ) ;
26422620 setTextRange ( enumStatement , node ) ;
2621+ setCommentRange ( enumStatement , node ) ;
26432622 setEmitFlags ( enumStatement , emitFlags ) ;
26442623 statements . push ( enumStatement ) ;
26452624
26462625 // Add a DeclarationMarker for the enum to preserve trailing comments and mark
26472626 // the end of the declaration.
2648- statements . push ( createEndOfDeclarationMarker ( node ) ) ;
2627+ // statements.push(createEndOfDeclarationMarker(node));
26492628 return statements ;
26502629 }
26512630
@@ -2738,18 +2717,6 @@ namespace ts {
27382717 return isInstantiatedModule ( node , compilerOptions . preserveConstEnums || compilerOptions . isolatedModules ) ;
27392718 }
27402719
2741- /**
2742- * Determines whether an exported declaration will have a qualified export name (e.g. `f.x`
2743- * or `exports.x`).
2744- */
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- }
2752-
27532720 /**
27542721 * Records that a declaration was emitted in the current scope, if it was the first
27552722 * declaration for the provided symbol.
@@ -2786,22 +2753,58 @@ namespace ts {
27862753 * Adds a leading VariableStatement for a enum or module declaration.
27872754 */
27882755 function addVarForEnumOrModuleDeclaration ( statements : Statement [ ] , node : ModuleDeclaration | EnumDeclaration ) {
2789- // Emit a variable statement for the module. We emit top-level enums as a `var`
2790- // declaration to avoid static errors in global scripts scripts due to redeclaration.
2791- // enums in any other scope are emitted as a `let` declaration.
2792- const statement = createVariableStatement (
2793- visitNodes ( node . modifiers , modifierVisitor , isModifier ) ,
2794- createVariableDeclarationList ( [
2795- createVariableDeclaration (
2796- getLocalName ( node , /*allowComments*/ false , /*allowSourceMaps*/ true )
2797- )
2798- ] , currentScope . kind === SyntaxKind . SourceFile ? NodeFlags . None : NodeFlags . Let )
2799- ) ;
2800-
2801- setOriginalNode ( statement , node ) ;
28022756
28032757 recordEmittedDeclarationInScope ( node ) ;
28042758 if ( isFirstEmittedDeclarationInScope ( node ) ) {
2759+ // Emit a local variable statement for the module. We emit top-level enums as a `var`
2760+ // declaration to avoid static errors in global scripts scripts due to redeclaration.
2761+ // enums in any other scope are emitted as a `const` declaration.
2762+ const nodeModifierFlags = getModifierFlags ( node ) ;
2763+ const nodeIsExportDefault = ( nodeModifierFlags & ModifierFlags . Export ) && ( nodeModifierFlags & ModifierFlags . Default ) ;
2764+ // function modifierVisitor(node: Node): VisitResult<Node> {
2765+ // if (modifierToFlag(node.kind) & ModifierFlags.TypeScriptModifier) {
2766+ // return undefined;
2767+ // }
2768+ // else if ((currentNamespace || nodeIsExportDefault) && node.kind === SyntaxKind.ExportKeyword) {
2769+ // return undefined;
2770+ // }
2771+ // if (node.kind === SyntaxKind.DefaultKeyword) {
2772+ // return undefined;
2773+ // }
2774+ // return node;
2775+ // }
2776+
2777+ const fileIsExternalModule = isExternalModule ( currentSourceFile ) ;
2778+ const localName = getLocalName ( node , /*allowComments*/ false , /*allowSourceMaps*/ true ) ;
2779+ let varInitializer ;
2780+ if ( fileIsExternalModule && ! currentNamespace ) {
2781+ // toplevel declaration in an external module
2782+ // cannot merge with another var
2783+ varInitializer = createObjectLiteral ( ) ;
2784+ }
2785+ else {
2786+ const exportName = hasModifier ( node , ModifierFlags . Export ) && ! nodeIsExportDefault
2787+ ? getExternalModuleOrNamespaceExportName ( currentNamespaceContainerName , node , /*allowComments*/ false , /*allowSourceMaps*/ true )
2788+ : getLocalName ( node , /*allowComments*/ false , /*allowSourceMaps*/ true ) ;
2789+ varInitializer = createLogicalOr (
2790+ exportName ,
2791+ createAssignment ( exportName , createObjectLiteral ( ) )
2792+ ) ;
2793+ }
2794+ const statement = createVariableStatement (
2795+ visitNodes ( node . modifiers , modifierVisitor , isModifier ) ,
2796+ createVariableDeclarationList ( [
2797+ createVariableDeclaration (
2798+ localName ,
2799+ /* type */ undefined ,
2800+ varInitializer
2801+ )
2802+ ] , currentScope . kind === SyntaxKind . SourceFile && ! fileIsExternalModule ? NodeFlags . None : NodeFlags . Const )
2803+ ) ;
2804+
2805+ setOriginalNode ( statement , node ) ;
2806+
2807+
28052808 // Adjust the source map emit to match the old emitter.
28062809 if ( node . kind === SyntaxKind . EnumDeclaration ) {
28072810 setSourceMapRange ( statement . declarationList , node ) ;
@@ -2817,32 +2820,28 @@ namespace ts {
28172820 // module m1 {
28182821 // function foo4Export() {
28192822 // }
2820- // } // trailing comment module
2823+ // } // trailing module comment
28212824 //
28222825 // Should emit:
28232826 //
28242827 // /** Module comment*/
2825- // var m1;
2828+ // var m1 = m1 || {} ;
28262829 // (function (m1) {
28272830 // function foo4Export() {
28282831 // }
2829- // })(m1 || (m1 = {})) ; // trailing comment module
2832+ // })(m1) ; // trailing module comment
28302833 //
28312834 setCommentRange ( statement , node ) ;
2832- setEmitFlags ( statement , EmitFlags . NoTrailingComments | EmitFlags . HasEndOfDeclarationMarker ) ;
2835+ setEmitFlags ( statement , EmitFlags . NoTrailingComments ) ;
28332836 statements . push ( statement ) ;
2837+ if ( nodeIsExportDefault ) {
2838+ statements . push ( createExportAssignment ( /* decorators */ undefined , /* modifiers */ undefined , /* isExportEquals */ false , localName ) ) ;
2839+ }
2840+ else if ( ! currentNamespace && nodeModifierFlags & ModifierFlags . Export ) {
2841+ statements . push ( createExternalModuleExport ( localName ) ) ;
2842+ }
28342843 return true ;
28352844 }
2836- else {
2837- // For an EnumDeclaration or ModuleDeclaration that merges with a preceeding
2838- // declaration we do not emit a leading variable declaration. To preserve the
2839- // begin/end semantics of the declararation and to properly handle exports
2840- // we wrap the leading variable declaration in a `MergeDeclarationMarker`.
2841- const mergeMarker = createMergeDeclarationMarker ( statement ) ;
2842- setEmitFlags ( mergeMarker , EmitFlags . NoComments | EmitFlags . HasEndOfDeclarationMarker ) ;
2843- statements . push ( mergeMarker ) ;
2844- return false ;
2845- }
28462845 }
28472846
28482847 /**
@@ -2882,29 +2881,7 @@ namespace ts {
28822881 // `containerName` is the expression used inside of the namespace for exports.
28832882 const containerName = getNamespaceContainerName ( node ) ;
28842883
2885- // `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- }
2884+ const localName = getLocalName ( node , /*allowComments*/ false , /*allowSourceMaps*/ true ) ;
29082885
29092886 // (function (x_1) {
29102887 // x_1.y = ...;
@@ -2921,18 +2898,16 @@ namespace ts {
29212898 transformModuleBody ( node , containerName )
29222899 ) ,
29232900 /*typeArguments*/ undefined ,
2924- [ moduleArg ]
2901+ [ localName ]
29252902 )
29262903 ) ;
29272904
29282905 setOriginalNode ( moduleStatement , node ) ;
2906+ setCommentRange ( moduleStatement , node ) ;
29292907 setTextRange ( moduleStatement , node ) ;
29302908 setEmitFlags ( moduleStatement , emitFlags ) ;
29312909 statements . push ( moduleStatement ) ;
29322910
2933- // Add a DeclarationMarker for the namespace to preserve trailing comments and mark
2934- // the end of the declaration.
2935- statements . push ( createEndOfDeclarationMarker ( node ) ) ;
29362911 return statements ;
29372912 }
29382913
0 commit comments