@@ -588,17 +588,54 @@ module ts {
588588 recordSourceMapSpan ( comment . end ) ;
589589 }
590590
591+ var escapedCharsRegExp = / [ \t \v \f \b \0 \r \n \" \u2028 \u2029 \u0085 ] / g;
592+ var escapedCharsMap : Map < string > = {
593+ "\t" : "\\t" ,
594+ "\v" : "\\v" ,
595+ "\f" : "\\f" ,
596+ "\b" : "\\b" ,
597+ "\0" : "\\0" ,
598+ "\r" : "\\r" ,
599+ "\n" : "\\n" ,
600+ "\"" : "\\\"" ,
601+ "\u2028" : "\\u2028" , // lineSeparator
602+ "\u2029" : "\\u2029" , // paragraphSeparator
603+ "\u0085" : "\\u0085" // nextLine
604+ } ;
605+
606+ function serializeSourceMapContents ( version : number , file : string , sourceRoot : string , sources : string [ ] , names : string [ ] , mappings : string ) {
607+ return "{\"version\":" + version + ",\"file\":\"" + escapeString ( file ) + "\",\"sourceRoot\":\"" + escapeString ( sourceRoot ) + "\",\"sources\":[" + serializeStringArray ( sources ) + "],\"names\":[" + serializeStringArray ( names ) + "],\"mappings\":\"" + escapeString ( mappings ) + "\"}" ;
608+
609+ /** This does not support the full escape characters, it only supports the subset that can be used in file names
610+ * or string literals. If the information encoded in the map changes, this needs to be revisited. */
611+ function escapeString ( s : string ) : string {
612+ return escapedCharsRegExp . test ( s ) ? s . replace ( escapedCharsRegExp , c => {
613+ return escapedCharsMap [ c ] || c ;
614+ } ) : s ;
615+ }
616+
617+ function serializeStringArray ( list : string [ ] ) : string {
618+ var output = "" ;
619+ for ( var i = 0 , n = list . length ; i < n ; i ++ ) {
620+ if ( i ) {
621+ output += "," ;
622+ }
623+ output += "\"" + escapeString ( list [ i ] ) + "\"" ;
624+ }
625+ return output ;
626+ }
627+ }
628+
591629 function writeJavaScriptAndSourceMapFile ( emitOutput : string , writeByteOrderMark : boolean ) {
592630 // Write source map file
593631 encodeLastRecordedSourceMapSpan ( ) ;
594- writeFile ( sourceMapData . sourceMapFilePath , JSON . stringify ( {
595- version : 3 ,
596- file : sourceMapData . sourceMapFile ,
597- sourceRoot : sourceMapData . sourceMapSourceRoot ,
598- sources : sourceMapData . sourceMapSources ,
599- names : sourceMapData . sourceMapNames ,
600- mappings : sourceMapData . sourceMapMappings
601- } ) , /*writeByteOrderMark*/ false ) ;
632+ writeFile ( sourceMapData . sourceMapFilePath , serializeSourceMapContents (
633+ 3 ,
634+ sourceMapData . sourceMapFile ,
635+ sourceMapData . sourceMapSourceRoot ,
636+ sourceMapData . sourceMapSources ,
637+ sourceMapData . sourceMapNames ,
638+ sourceMapData . sourceMapMappings ) , /*writeByteOrderMark*/ false ) ;
602639 sourceMapDataList . push ( sourceMapData ) ;
603640
604641 // Write sourcemap url to the js file and write the js file
0 commit comments