@@ -2653,34 +2653,12 @@ namespace ts {
26532653 // `containerName` is the expression used inside of the enum for assignments.
26542654 const containerName = getNamespaceContainerName ( node ) ;
26552655
2656- // `exportName` is the expression used within this node's container for any exported references.
2657- const exportName = hasModifier ( node , ModifierFlags . Export )
2658- ? getExternalModuleOrNamespaceExportName ( currentNamespaceContainerName , node , /*allowComments*/ false , /*allowSourceMaps*/ true )
2659- : getLocalName ( node , /*allowComments*/ false , /*allowSourceMaps*/ true ) ;
2660-
2661- // x || (x = {})
2662- // exports.x || (exports.x = {})
2663- let moduleArg =
2664- createLogicalOr (
2665- exportName ,
2666- createAssignment (
2667- exportName ,
2668- createObjectLiteral ( )
2669- )
2670- ) ;
2671-
2672- if ( hasNamespaceQualifiedExportName ( node ) ) {
2673- // `localName` is the expression used within this node's containing scope for any local references.
2674- const localName = getLocalName ( node , /*allowComments*/ false , /*allowSourceMaps*/ true ) ;
2675-
2676- // x = (exports.x || (exports.x = {}))
2677- moduleArg = createAssignment ( localName , moduleArg ) ;
2678- }
2656+ const localName = getLocalName ( node , /*allowComments*/ false , /*allowSourceMaps*/ true ) ;
26792657
26802658 // (function (x) {
26812659 // x[x["y"] = 0] = "y";
26822660 // ...
2683- // })(x || (x = {}) );
2661+ // })(x);
26842662 const enumStatement = createExpressionStatement (
26852663 createCall (
26862664 createFunctionExpression (
@@ -2693,23 +2671,19 @@ namespace ts {
26932671 transformEnumBody ( node , containerName )
26942672 ) ,
26952673 /*typeArguments*/ undefined ,
2696- [ moduleArg ]
2674+ [ localName ]
26972675 )
26982676 ) ;
26992677
27002678 setOriginalNode ( enumStatement , node ) ;
2701- if ( varAdded ) {
2702- // If a variable was added, synthetic comments are emitted on it, not on the moduleStatement.
2703- setSyntheticLeadingComments ( enumStatement , undefined ) ;
2704- setSyntheticTrailingComments ( enumStatement , undefined ) ;
2705- }
27062679 setTextRange ( enumStatement , node ) ;
2680+ setCommentRange ( enumStatement , node ) ;
27072681 addEmitFlags ( enumStatement , emitFlags ) ;
27082682 statements . push ( enumStatement ) ;
27092683
27102684 // Add a DeclarationMarker for the enum to preserve trailing comments and mark
27112685 // the end of the declaration.
2712- statements . push ( createEndOfDeclarationMarker ( node ) ) ;
2686+ // statements.push(createEndOfDeclarationMarker(node));
27132687 return statements ;
27142688 }
27152689
@@ -2803,18 +2777,6 @@ namespace ts {
28032777 return isInstantiatedModule ( node , ! ! compilerOptions . preserveConstEnums || ! ! compilerOptions . isolatedModules ) ;
28042778 }
28052779
2806- /**
2807- * Determines whether an exported declaration will have a qualified export name (e.g. `f.x`
2808- * or `exports.x`).
2809- */
2810- function hasNamespaceQualifiedExportName ( node : Node ) {
2811- return isExportOfNamespace ( node )
2812- || ( isExternalModuleExport ( node )
2813- && moduleKind !== ModuleKind . ES2015
2814- && moduleKind !== ModuleKind . ESNext
2815- && moduleKind !== ModuleKind . System ) ;
2816- }
2817-
28182780 /**
28192781 * Records that a declaration was emitted in the current scope, if it was the first
28202782 * declaration for the provided symbol.
@@ -2851,22 +2813,47 @@ namespace ts {
28512813 * Adds a leading VariableStatement for a enum or module declaration.
28522814 */
28532815 function addVarForEnumOrModuleDeclaration ( statements : Statement [ ] , node : ModuleDeclaration | EnumDeclaration ) {
2854- // Emit a variable statement for the module. We emit top-level enums as a `var`
2855- // declaration to avoid static errors in global scripts scripts due to redeclaration.
2856- // enums in any other scope are emitted as a `let` declaration.
2857- const statement = createVariableStatement (
2858- visitNodes ( node . modifiers , modifierVisitor , isModifier ) ,
2859- createVariableDeclarationList ( [
2860- createVariableDeclaration (
2861- getLocalName ( node , /*allowComments*/ false , /*allowSourceMaps*/ true )
2862- )
2863- ] , currentLexicalScope . kind === SyntaxKind . SourceFile ? NodeFlags . None : NodeFlags . Let )
2864- ) ;
2865-
2866- setOriginalNode ( statement , node ) ;
28672816
28682817 recordEmittedDeclarationInScope ( node ) ;
28692818 if ( isFirstEmittedDeclarationInScope ( node ) ) {
2819+ // Emit a local variable statement for the module. We emit top-level enums as a `var`
2820+ // declaration to avoid static errors in global scripts scripts due to redeclaration.
2821+ // enums in any other scope are emitted as a `const` declaration.
2822+ const nodeModifierFlags = getModifierFlags ( node ) ;
2823+ const nodeIsExportDefault = ( nodeModifierFlags & ModifierFlags . Export ) && ( nodeModifierFlags & ModifierFlags . Default ) ;
2824+
2825+ const fileIsExternalModule = isExternalModule ( currentSourceFile ) ;
2826+ const localName = getLocalName ( node , /*allowComments*/ false , /*allowSourceMaps*/ true ) ;
2827+ let varInitializer ;
2828+ if ( fileIsExternalModule && ! currentNamespace ) {
2829+ // toplevel declaration in an external module
2830+ // cannot merge with another var
2831+ varInitializer = createObjectLiteral ( ) ;
2832+ }
2833+ else {
2834+ const exportName = hasModifier ( node , ModifierFlags . Export ) && ! nodeIsExportDefault
2835+ ? getExternalModuleOrNamespaceExportName ( currentNamespaceContainerName , node , /*allowComments*/ false , /*allowSourceMaps*/ true )
2836+ : getLocalName ( node , /*allowComments*/ false , /*allowSourceMaps*/ true ) ;
2837+ varInitializer = createLogicalOr (
2838+ exportName ,
2839+ createAssignment ( exportName , createObjectLiteral ( ) )
2840+ ) ;
2841+ }
2842+
2843+ const statement = createVariableStatement (
2844+ visitNodes ( node . modifiers , modifierVisitor , isModifier ) ,
2845+ createVariableDeclarationList ( [
2846+ createVariableDeclaration (
2847+ localName ,
2848+ /* type */ undefined ,
2849+ varInitializer
2850+ )
2851+ ] , currentLexicalScope . kind === SyntaxKind . SourceFile && ! fileIsExternalModule ? NodeFlags . None : NodeFlags . Const )
2852+ ) ;
2853+
2854+ setOriginalNode ( statement , node ) ;
2855+
2856+
28702857 // Adjust the source map emit to match the old emitter.
28712858 if ( node . kind === SyntaxKind . EnumDeclaration ) {
28722859 setSourceMapRange ( statement . declarationList , node ) ;
@@ -2882,32 +2869,28 @@ namespace ts {
28822869 // module m1 {
28832870 // function foo4Export() {
28842871 // }
2885- // } // trailing comment module
2872+ // } // trailing module comment
28862873 //
28872874 // Should emit:
28882875 //
28892876 // /** Module comment*/
2890- // var m1;
2877+ // var m1 = m1 || {} ;
28912878 // (function (m1) {
28922879 // function foo4Export() {
28932880 // }
2894- // })(m1 || (m1 = {})) ; // trailing comment module
2881+ // })(m1) ; // trailing module comment
28952882 //
28962883 setCommentRange ( statement , node ) ;
2897- addEmitFlags ( statement , EmitFlags . NoTrailingComments | EmitFlags . HasEndOfDeclarationMarker ) ;
2884+ addEmitFlags ( statement , EmitFlags . NoTrailingComments ) ;
28982885 statements . push ( statement ) ;
2886+ if ( nodeIsExportDefault ) {
2887+ statements . push ( createExportAssignment ( /* decorators */ undefined , /* modifiers */ undefined , /* isExportEquals */ false , localName ) ) ;
2888+ }
2889+ else if ( ! currentNamespace && nodeModifierFlags & ModifierFlags . Export ) {
2890+ statements . push ( createExternalModuleExport ( localName ) ) ;
2891+ }
28992892 return true ;
29002893 }
2901- else {
2902- // For an EnumDeclaration or ModuleDeclaration that merges with a preceeding
2903- // declaration we do not emit a leading variable declaration. To preserve the
2904- // begin/end semantics of the declararation and to properly handle exports
2905- // we wrap the leading variable declaration in a `MergeDeclarationMarker`.
2906- const mergeMarker = createMergeDeclarationMarker ( statement ) ;
2907- setEmitFlags ( mergeMarker , EmitFlags . NoComments | EmitFlags . HasEndOfDeclarationMarker ) ;
2908- statements . push ( mergeMarker ) ;
2909- return false ;
2910- }
29112894 }
29122895
29132896 /**
@@ -2948,33 +2931,11 @@ namespace ts {
29482931 // `containerName` is the expression used inside of the namespace for exports.
29492932 const containerName = getNamespaceContainerName ( node ) ;
29502933
2951- // `exportName` is the expression used within this node's container for any exported references.
2952- const exportName = hasModifier ( node , ModifierFlags . Export )
2953- ? getExternalModuleOrNamespaceExportName ( currentNamespaceContainerName , node , /*allowComments*/ false , /*allowSourceMaps*/ true )
2954- : getLocalName ( node , /*allowComments*/ false , /*allowSourceMaps*/ true ) ;
2955-
2956- // x || (x = {})
2957- // exports.x || (exports.x = {})
2958- let moduleArg =
2959- createLogicalOr (
2960- exportName ,
2961- createAssignment (
2962- exportName ,
2963- createObjectLiteral ( )
2964- )
2965- ) ;
2966-
2967- if ( hasNamespaceQualifiedExportName ( node ) ) {
2968- // `localName` is the expression used within this node's containing scope for any local references.
2969- const localName = getLocalName ( node , /*allowComments*/ false , /*allowSourceMaps*/ true ) ;
2970-
2971- // x = (exports.x || (exports.x = {}))
2972- moduleArg = createAssignment ( localName , moduleArg ) ;
2973- }
2934+ const localName = getLocalName ( node , /*allowComments*/ false , /*allowSourceMaps*/ true ) ;
29742935
29752936 // (function (x_1) {
29762937 // x_1.y = ...;
2977- // })(x || (x = {}) );
2938+ // })(x);
29782939 const moduleStatement = createExpressionStatement (
29792940 createCall (
29802941 createFunctionExpression (
@@ -2987,23 +2948,16 @@ namespace ts {
29872948 transformModuleBody ( node , containerName )
29882949 ) ,
29892950 /*typeArguments*/ undefined ,
2990- [ moduleArg ]
2951+ [ localName ]
29912952 )
29922953 ) ;
29932954
29942955 setOriginalNode ( moduleStatement , node ) ;
2995- if ( varAdded ) {
2996- // If a variable was added, synthetic comments are emitted on it, not on the moduleStatement.
2997- setSyntheticLeadingComments ( moduleStatement , undefined ) ;
2998- setSyntheticTrailingComments ( moduleStatement , undefined ) ;
2999- }
2956+ setCommentRange ( moduleStatement , node ) ;
30002957 setTextRange ( moduleStatement , node ) ;
30012958 addEmitFlags ( moduleStatement , emitFlags ) ;
30022959 statements . push ( moduleStatement ) ;
30032960
3004- // Add a DeclarationMarker for the namespace to preserve trailing comments and mark
3005- // the end of the declaration.
3006- statements . push ( createEndOfDeclarationMarker ( node ) ) ;
30072961 return statements ;
30082962 }
30092963
0 commit comments