@@ -2629,76 +2629,41 @@ namespace ts {
2629
2629
return node ;
2630
2630
}
2631
2631
2632
- interface UnscopedEmitHelpersWithLines {
2633
- helper : UnscopedEmitHelpers ;
2634
- lines : ReadonlyArray < string > ;
2635
- }
2636
-
2637
- let allUnscopedEmitHelpers : ReadonlyArray < UnscopedEmitHelpersWithLines > | undefined ;
2632
+ let allUnscopedEmitHelpers : ReadonlyMap < UnscopedEmitHelpers > | undefined ;
2638
2633
function getAllUnscopedEmitHelpers ( ) {
2639
- return allUnscopedEmitHelpers ||
2640
- ( allUnscopedEmitHelpers = [
2641
- getUnscopedEmitHelperWithLines ( valuesHelper ) ,
2642
- getUnscopedEmitHelperWithLines ( readHelper ) ,
2643
- getUnscopedEmitHelperWithLines ( spreadHelper ) ,
2644
- getUnscopedEmitHelperWithLines ( restHelper ) ,
2645
- getUnscopedEmitHelperWithLines ( decorateHelper ) ,
2646
- getUnscopedEmitHelperWithLines ( metadataHelper ) ,
2647
- getUnscopedEmitHelperWithLines ( paramHelper ) ,
2648
- getUnscopedEmitHelperWithLines ( awaiterHelper ) ,
2649
- getUnscopedEmitHelperWithLines ( assignHelper ) ,
2650
- getUnscopedEmitHelperWithLines ( awaitHelper ) ,
2651
- getUnscopedEmitHelperWithLines ( asyncGeneratorHelper ) ,
2652
- getUnscopedEmitHelperWithLines ( asyncDelegator ) ,
2653
- getUnscopedEmitHelperWithLines ( asyncValues ) ,
2654
- getUnscopedEmitHelperWithLines ( extendsHelper ) ,
2655
- getUnscopedEmitHelperWithLines ( templateObjectHelper ) ,
2656
- getUnscopedEmitHelperWithLines ( generatorHelper ) ,
2657
- getUnscopedEmitHelperWithLines ( importStarHelper ) ,
2658
- getUnscopedEmitHelperWithLines ( importDefaultHelper )
2659
- ] ) ;
2660
- }
2661
-
2662
- function getUnscopedEmitHelperWithLines ( helper : UnscopedEmitHelpers ) : UnscopedEmitHelpersWithLines {
2663
- const helperLines = helper . text . split ( / \r \n ? | \n / g) ;
2664
- const indentation = guessIndentation ( helperLines ) ;
2665
- const lines : string [ ] = [ ] ;
2666
- for ( const lineText of helperLines ) {
2667
- const line = indentation ? lineText . slice ( indentation ) : lineText ;
2668
- if ( line . length ) {
2669
- lines . push ( line ) ;
2670
- }
2671
- }
2672
- return { helper, lines } ;
2673
- }
2674
-
2675
- function tryGetUnscopedEmitHelper ( text : string , pos : number ) {
2676
- const allHelpers = getAllUnscopedEmitHelpers ( ) ;
2677
- if ( pos >= text . length ) return undefined ;
2678
- for ( const { helper, lines } of allHelpers ) {
2679
- let newPos = pos ;
2680
- for ( const line of lines ) {
2681
- const startIndex = text . indexOf ( line , newPos ) ;
2682
- if ( startIndex !== newPos ) {
2683
- newPos = - 1 ;
2684
- break ;
2685
- }
2686
- newPos = skipTrivia ( text , newPos + line . length , /*stopAfterLineBreak*/ true ) ;
2687
- }
2688
-
2689
- // Found match
2690
- if ( newPos !== - 1 ) {
2691
- return { helper, newPos } ;
2692
- }
2693
- }
2694
- return undefined ;
2634
+ return allUnscopedEmitHelpers || ( allUnscopedEmitHelpers = arrayToMap ( [
2635
+ valuesHelper ,
2636
+ readHelper ,
2637
+ spreadHelper ,
2638
+ restHelper ,
2639
+ decorateHelper ,
2640
+ metadataHelper ,
2641
+ paramHelper ,
2642
+ awaiterHelper ,
2643
+ assignHelper ,
2644
+ awaitHelper ,
2645
+ asyncGeneratorHelper ,
2646
+ asyncDelegator ,
2647
+ asyncValues ,
2648
+ extendsHelper ,
2649
+ templateObjectHelper ,
2650
+ generatorHelper ,
2651
+ importStarHelper ,
2652
+ importDefaultHelper
2653
+ ] , helper => helper . name ) ) ;
2695
2654
}
2696
2655
2697
2656
export function createUnparsedSourceFile ( text : string ) : UnparsedSource ;
2698
2657
export function createUnparsedSourceFile ( inputFile : InputFiles , type : "js" | "dts" ) : UnparsedSource ;
2699
2658
export function createUnparsedSourceFile ( text : string , mapPath : string | undefined , map : string | undefined ) : UnparsedSource ;
2700
2659
export function createUnparsedSourceFile ( textOrInputFiles : string | InputFiles , mapPathOrType ?: string , map ?: string ) : UnparsedSource {
2701
2660
const node = < UnparsedSource > createNode ( SyntaxKind . UnparsedSource ) ;
2661
+ node . pos = 0 ;
2662
+ let prologues : UnparsedPrologue [ ] | undefined ;
2663
+ let helpers : UnscopedEmitHelpers [ ] | undefined ;
2664
+ let referencedFiles : FileReference [ ] | undefined ;
2665
+ let typeReferenceDirectives : string [ ] | undefined ;
2666
+ let libReferenceDirectives : FileReference [ ] | undefined ;
2702
2667
if ( ! isString ( textOrInputFiles ) ) {
2703
2668
Debug . assert ( mapPathOrType === "js" || mapPathOrType === "dts" ) ;
2704
2669
node . fileName = ( mapPathOrType === "js" ? textOrInputFiles . javascriptPath : textOrInputFiles . declarationPath ) || "" ;
@@ -2707,54 +2672,58 @@ namespace ts {
2707
2672
text : { get ( ) { return mapPathOrType === "js" ? textOrInputFiles . javascriptText : textOrInputFiles . declarationText ; } } ,
2708
2673
sourceMapText : { get ( ) { return mapPathOrType === "js" ? textOrInputFiles . javascriptMapText : textOrInputFiles . declarationMapText ; } } ,
2709
2674
} ) ;
2675
+
2676
+ const sections = textOrInputFiles . bundleInfo ? mapPathOrType === "js" ? textOrInputFiles . bundleInfo . js : textOrInputFiles . bundleInfo . dts : undefined ;
2677
+ if ( sections ) {
2678
+ for ( const section of sections ) {
2679
+ switch ( section . kind ) {
2680
+ case BundleFileSectionKind . Prologue :
2681
+ const unparsedPrologue = < UnparsedPrologue > createNode ( SyntaxKind . UnparsedPrologue , section . pos , section . end ) ;
2682
+ unparsedPrologue . parent = node ;
2683
+ unparsedPrologue . text = section . text ;
2684
+ ( prologues || ( prologues = [ ] ) ) . push ( unparsedPrologue ) ;
2685
+ break ;
2686
+ case BundleFileSectionKind . EmitHelpers :
2687
+ ( helpers || ( helpers = [ ] ) ) . push ( getAllUnscopedEmitHelpers ( ) . get ( section . name ) ! ) ;
2688
+ break ;
2689
+ case BundleFileSectionKind . NoDefaultLib :
2690
+ node . hasNoDefaultLib = true ;
2691
+ break ;
2692
+ case BundleFileSectionKind . Reference :
2693
+ ( referencedFiles || ( referencedFiles = [ ] ) ) . push ( { pos : - 1 , end : - 1 , fileName : section . fileName } ) ;
2694
+ break ;
2695
+ case BundleFileSectionKind . Type :
2696
+ ( typeReferenceDirectives || ( typeReferenceDirectives = [ ] ) ) . push ( section . fileName ) ;
2697
+ break ;
2698
+ case BundleFileSectionKind . Lib :
2699
+ ( libReferenceDirectives || ( libReferenceDirectives = [ ] ) ) . push ( { pos : - 1 , end : - 1 , fileName : section . fileName } ) ;
2700
+ break ;
2701
+ case BundleFileSectionKind . Text :
2702
+ // For now just set node.pos to this
2703
+ node . pos = section . pos ;
2704
+ break ;
2705
+ default :
2706
+ Debug . assertNever ( section ) ;
2707
+ }
2708
+ }
2709
+ }
2710
2710
}
2711
2711
else {
2712
2712
node . fileName = "" ;
2713
2713
node . text = textOrInputFiles ;
2714
2714
node . sourceMapPath = mapPathOrType ;
2715
2715
node . sourceMapText = map ;
2716
2716
}
2717
- const text = node . text ;
2718
- node . languageVersion = ScriptTarget . Latest ;
2719
-
2720
- // Shebang
2721
- let pos = isShebangTrivia ( text , 0 ) ? skipTrivia ( text , 0 , /*stopAfterLineBreak*/ true ) : 0 ;
2722
-
2723
- // Prologue
2724
- const scanner = createScanner ( ScriptTarget . Latest , /*skipTrivia*/ true , /*languageVariant*/ undefined , text , /*onError*/ undefined , pos ) ;
2725
- let prologues : UnparsedPrologue [ ] | undefined ;
2726
- while ( scanner . scan ( ) === SyntaxKind . StringLiteral ) {
2727
- const start = pos ;
2728
- const prologueText = scanner . getTokenValue ( ) ;
2729
- scanner . tryScan ( ( ) => scanner . scan ( ) === SyntaxKind . SemicolonToken ) ;
2730
- pos = skipTrivia ( text , scanner . getTextPos ( ) , /*stopAfterLineBreak*/ true ) ;
2731
-
2732
- const prologue = < UnparsedPrologue > createNode ( SyntaxKind . UnparsedPrologue , start , pos ) ;
2733
- prologue . parent = node ;
2734
- prologue . text = prologueText ;
2735
- ( prologues || ( prologues = [ ] ) ) . push ( prologue ) ;
2736
- }
2737
-
2738
- // Helpers
2739
- let helpers : UnscopedEmitHelpers [ ] | undefined ;
2740
- while ( true ) {
2741
- const helperInfo = tryGetUnscopedEmitHelper ( text , pos ) ;
2742
- if ( ! helperInfo ) break ;
2743
- pos = helperInfo . newPos ;
2744
- ( helpers || ( helpers = [ ] ) ) . push ( helperInfo . helper ) ;
2745
- }
2746
-
2747
- // triple slash directives
2748
- const newPos = processCommentPragmas ( node as { } as PragmaContext , text ) ;
2749
- processPragmasIntoFields ( node as { } as PragmaContext , noop ) ;
2750
-
2751
- // Rest of the text
2752
- node . pos = newPos > pos ? newPos : pos ;
2753
2717
node . prologues = prologues || emptyArray ;
2754
2718
node . helpers = helpers ;
2719
+ node . referencedFiles = referencedFiles || emptyArray ;
2720
+ node . typeReferenceDirectives = typeReferenceDirectives ;
2721
+ node . libReferenceDirectives = libReferenceDirectives || emptyArray ;
2722
+
2755
2723
node . getLineAndCharacterOfPosition = pos => getLineAndCharacterOfPosition ( node , pos ) ;
2756
2724
return node ;
2757
2725
}
2726
+
2758
2727
export function createInputFiles (
2759
2728
javascriptText : string ,
2760
2729
declarationText : string
@@ -2765,6 +2734,7 @@ namespace ts {
2765
2734
javascriptMapPath : string | undefined ,
2766
2735
declarationPath : string ,
2767
2736
declarationMapPath : string | undefined ,
2737
+ bundleInfoPath : string | undefined
2768
2738
) : InputFiles ;
2769
2739
export function createInputFiles (
2770
2740
javascriptText : string ,
@@ -2780,7 +2750,7 @@ namespace ts {
2780
2750
javascriptMapPath ?: string ,
2781
2751
javascriptMapTextOrDeclarationPath ?: string ,
2782
2752
declarationMapPath ?: string ,
2783
- declarationMapText ?: string
2753
+ declarationMapTextOrBundleInfoPath ?: string
2784
2754
) : InputFiles {
2785
2755
const node = < InputFiles > createNode ( SyntaxKind . InputFiles ) ;
2786
2756
if ( ! isString ( javascriptTextOrReadFileText ) ) {
@@ -2798,15 +2768,21 @@ namespace ts {
2798
2768
const result = textGetter ( path ) ;
2799
2769
return result !== undefined ? result : `/* Input file ${ path } was missing */\r\n` ;
2800
2770
} ;
2771
+ const jsonGetter = ( path : string | undefined ) => {
2772
+ const result = textGetter ( path ) ;
2773
+ return result !== undefined ? JSON . parse ( result ) as BundleInfo : undefined ;
2774
+ } ;
2801
2775
node . javascriptPath = declarationTextOrJavascriptPath ;
2802
2776
node . javascriptMapPath = javascriptMapPath ;
2803
2777
node . declarationPath = Debug . assertDefined ( javascriptMapTextOrDeclarationPath ) ;
2804
2778
node . declarationMapPath = declarationMapPath ;
2779
+ node . bundleInfoPath = declarationMapTextOrBundleInfoPath ;
2805
2780
Object . defineProperties ( node , {
2806
2781
javascriptText : { get ( ) { return definedTextGetter ( declarationTextOrJavascriptPath ) ; } } ,
2807
2782
javascriptMapText : { get ( ) { return textGetter ( javascriptMapPath ) ; } } , // TODO:: if there is inline sourceMap in jsFile, use that
2808
2783
declarationText : { get ( ) { return definedTextGetter ( Debug . assertDefined ( javascriptMapTextOrDeclarationPath ) ) ; } } ,
2809
- declarationMapText : { get ( ) { return textGetter ( declarationMapPath ) ; } } // TODO:: if there is inline sourceMap in dtsFile, use that
2784
+ declarationMapText : { get ( ) { return textGetter ( declarationMapPath ) ; } } , // TODO:: if there is inline sourceMap in dtsFile, use that
2785
+ bundleInfo : { get ( ) { return jsonGetter ( declarationMapTextOrBundleInfoPath ) ; } }
2810
2786
} ) ;
2811
2787
}
2812
2788
else {
@@ -2815,7 +2791,7 @@ namespace ts {
2815
2791
node . javascriptMapText = javascriptMapTextOrDeclarationPath ;
2816
2792
node . declarationText = declarationTextOrJavascriptPath ;
2817
2793
node . declarationMapPath = declarationMapPath ;
2818
- node . declarationMapText = declarationMapText ;
2794
+ node . declarationMapText = declarationMapTextOrBundleInfoPath ;
2819
2795
}
2820
2796
return node ;
2821
2797
}
0 commit comments