@@ -333,7 +333,7 @@ namespace ts {
333
333
if ( modifierToFlag ( node . kind ) & ModifierFlags . TypeScriptModifier ) {
334
334
return undefined ;
335
335
}
336
- else if ( currentNamespace && node . kind === SyntaxKind . ExportKeyword ) {
336
+ else if ( node . kind === SyntaxKind . ExportKeyword || node . kind === SyntaxKind . DefaultKeyword ) {
337
337
return undefined ;
338
338
}
339
339
@@ -2594,29 +2594,7 @@ namespace ts {
2594
2594
// `containerName` is the expression used inside of the enum for assignments.
2595
2595
const containerName = getNamespaceContainerName ( node ) ;
2596
2596
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 ) ;
2620
2598
2621
2599
// (function (x) {
2622
2600
// x[x["y"] = 0] = "y";
@@ -2634,18 +2612,19 @@ namespace ts {
2634
2612
transformEnumBody ( node , containerName )
2635
2613
) ,
2636
2614
/*typeArguments*/ undefined ,
2637
- [ moduleArg ]
2615
+ [ localName ]
2638
2616
)
2639
2617
) ;
2640
2618
2641
2619
setOriginalNode ( enumStatement , node ) ;
2642
2620
setTextRange ( enumStatement , node ) ;
2621
+ setCommentRange ( enumStatement , node ) ;
2643
2622
setEmitFlags ( enumStatement , emitFlags ) ;
2644
2623
statements . push ( enumStatement ) ;
2645
2624
2646
2625
// Add a DeclarationMarker for the enum to preserve trailing comments and mark
2647
2626
// the end of the declaration.
2648
- statements . push ( createEndOfDeclarationMarker ( node ) ) ;
2627
+ // statements.push(createEndOfDeclarationMarker(node));
2649
2628
return statements ;
2650
2629
}
2651
2630
@@ -2738,18 +2717,6 @@ namespace ts {
2738
2717
return isInstantiatedModule ( node , compilerOptions . preserveConstEnums || compilerOptions . isolatedModules ) ;
2739
2718
}
2740
2719
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
-
2753
2720
/**
2754
2721
* Records that a declaration was emitted in the current scope, if it was the first
2755
2722
* declaration for the provided symbol.
@@ -2786,22 +2753,58 @@ namespace ts {
2786
2753
* Adds a leading VariableStatement for a enum or module declaration.
2787
2754
*/
2788
2755
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 ) ;
2802
2756
2803
2757
recordEmittedDeclarationInScope ( node ) ;
2804
2758
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
+
2805
2808
// Adjust the source map emit to match the old emitter.
2806
2809
if ( node . kind === SyntaxKind . EnumDeclaration ) {
2807
2810
setSourceMapRange ( statement . declarationList , node ) ;
@@ -2817,32 +2820,28 @@ namespace ts {
2817
2820
// module m1 {
2818
2821
// function foo4Export() {
2819
2822
// }
2820
- // } // trailing comment module
2823
+ // } // trailing module comment
2821
2824
//
2822
2825
// Should emit:
2823
2826
//
2824
2827
// /** Module comment*/
2825
- // var m1;
2828
+ // var m1 = m1 || {} ;
2826
2829
// (function (m1) {
2827
2830
// function foo4Export() {
2828
2831
// }
2829
- // })(m1 || (m1 = {})) ; // trailing comment module
2832
+ // })(m1) ; // trailing module comment
2830
2833
//
2831
2834
setCommentRange ( statement , node ) ;
2832
- setEmitFlags ( statement , EmitFlags . NoTrailingComments | EmitFlags . HasEndOfDeclarationMarker ) ;
2835
+ setEmitFlags ( statement , EmitFlags . NoTrailingComments ) ;
2833
2836
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
+ }
2834
2843
return true ;
2835
2844
}
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
- }
2846
2845
}
2847
2846
2848
2847
/**
@@ -2882,29 +2881,7 @@ namespace ts {
2882
2881
// `containerName` is the expression used inside of the namespace for exports.
2883
2882
const containerName = getNamespaceContainerName ( node ) ;
2884
2883
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 ) ;
2908
2885
2909
2886
// (function (x_1) {
2910
2887
// x_1.y = ...;
@@ -2921,18 +2898,16 @@ namespace ts {
2921
2898
transformModuleBody ( node , containerName )
2922
2899
) ,
2923
2900
/*typeArguments*/ undefined ,
2924
- [ moduleArg ]
2901
+ [ localName ]
2925
2902
)
2926
2903
) ;
2927
2904
2928
2905
setOriginalNode ( moduleStatement , node ) ;
2906
+ setCommentRange ( moduleStatement , node ) ;
2929
2907
setTextRange ( moduleStatement , node ) ;
2930
2908
setEmitFlags ( moduleStatement , emitFlags ) ;
2931
2909
statements . push ( moduleStatement ) ;
2932
2910
2933
- // Add a DeclarationMarker for the namespace to preserve trailing comments and mark
2934
- // the end of the declaration.
2935
- statements . push ( createEndOfDeclarationMarker ( node ) ) ;
2936
2911
return statements ;
2937
2912
}
2938
2913
0 commit comments