@@ -2653,34 +2653,12 @@ namespace ts {
2653
2653
// `containerName` is the expression used inside of the enum for assignments.
2654
2654
const containerName = getNamespaceContainerName ( node ) ;
2655
2655
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 ) ;
2679
2657
2680
2658
// (function (x) {
2681
2659
// x[x["y"] = 0] = "y";
2682
2660
// ...
2683
- // })(x || (x = {}) );
2661
+ // })(x);
2684
2662
const enumStatement = createExpressionStatement (
2685
2663
createCall (
2686
2664
createFunctionExpression (
@@ -2693,23 +2671,19 @@ namespace ts {
2693
2671
transformEnumBody ( node , containerName )
2694
2672
) ,
2695
2673
/*typeArguments*/ undefined ,
2696
- [ moduleArg ]
2674
+ [ localName ]
2697
2675
)
2698
2676
) ;
2699
2677
2700
2678
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
- }
2706
2679
setTextRange ( enumStatement , node ) ;
2680
+ setCommentRange ( enumStatement , node ) ;
2707
2681
addEmitFlags ( enumStatement , emitFlags ) ;
2708
2682
statements . push ( enumStatement ) ;
2709
2683
2710
2684
// Add a DeclarationMarker for the enum to preserve trailing comments and mark
2711
2685
// the end of the declaration.
2712
- statements . push ( createEndOfDeclarationMarker ( node ) ) ;
2686
+ // statements.push(createEndOfDeclarationMarker(node));
2713
2687
return statements ;
2714
2688
}
2715
2689
@@ -2803,18 +2777,6 @@ namespace ts {
2803
2777
return isInstantiatedModule ( node , ! ! compilerOptions . preserveConstEnums || ! ! compilerOptions . isolatedModules ) ;
2804
2778
}
2805
2779
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
-
2818
2780
/**
2819
2781
* Records that a declaration was emitted in the current scope, if it was the first
2820
2782
* declaration for the provided symbol.
@@ -2851,22 +2813,47 @@ namespace ts {
2851
2813
* Adds a leading VariableStatement for a enum or module declaration.
2852
2814
*/
2853
2815
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 ) ;
2867
2816
2868
2817
recordEmittedDeclarationInScope ( node ) ;
2869
2818
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
+
2870
2857
// Adjust the source map emit to match the old emitter.
2871
2858
if ( node . kind === SyntaxKind . EnumDeclaration ) {
2872
2859
setSourceMapRange ( statement . declarationList , node ) ;
@@ -2882,32 +2869,28 @@ namespace ts {
2882
2869
// module m1 {
2883
2870
// function foo4Export() {
2884
2871
// }
2885
- // } // trailing comment module
2872
+ // } // trailing module comment
2886
2873
//
2887
2874
// Should emit:
2888
2875
//
2889
2876
// /** Module comment*/
2890
- // var m1;
2877
+ // var m1 = m1 || {} ;
2891
2878
// (function (m1) {
2892
2879
// function foo4Export() {
2893
2880
// }
2894
- // })(m1 || (m1 = {})) ; // trailing comment module
2881
+ // })(m1) ; // trailing module comment
2895
2882
//
2896
2883
setCommentRange ( statement , node ) ;
2897
- addEmitFlags ( statement , EmitFlags . NoTrailingComments | EmitFlags . HasEndOfDeclarationMarker ) ;
2884
+ addEmitFlags ( statement , EmitFlags . NoTrailingComments ) ;
2898
2885
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
+ }
2899
2892
return true ;
2900
2893
}
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
- }
2911
2894
}
2912
2895
2913
2896
/**
@@ -2948,33 +2931,11 @@ namespace ts {
2948
2931
// `containerName` is the expression used inside of the namespace for exports.
2949
2932
const containerName = getNamespaceContainerName ( node ) ;
2950
2933
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 ) ;
2974
2935
2975
2936
// (function (x_1) {
2976
2937
// x_1.y = ...;
2977
- // })(x || (x = {}) );
2938
+ // })(x);
2978
2939
const moduleStatement = createExpressionStatement (
2979
2940
createCall (
2980
2941
createFunctionExpression (
@@ -2987,23 +2948,16 @@ namespace ts {
2987
2948
transformModuleBody ( node , containerName )
2988
2949
) ,
2989
2950
/*typeArguments*/ undefined ,
2990
- [ moduleArg ]
2951
+ [ localName ]
2991
2952
)
2992
2953
) ;
2993
2954
2994
2955
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 ) ;
3000
2957
setTextRange ( moduleStatement , node ) ;
3001
2958
addEmitFlags ( moduleStatement , emitFlags ) ;
3002
2959
statements . push ( moduleStatement ) ;
3003
2960
3004
- // Add a DeclarationMarker for the namespace to preserve trailing comments and mark
3005
- // the end of the declaration.
3006
- statements . push ( createEndOfDeclarationMarker ( node ) ) ;
3007
2961
return statements ;
3008
2962
}
3009
2963
0 commit comments