@@ -588,15 +588,40 @@ module ts {
588588 recordSourceMapSpan ( comment . end ) ;
589589 }
590590
591- function serializeStringArray ( list : string [ ] ) : string {
592- if ( list && list . length ) {
593- return "\"" + list . join ( "\",\"" ) + "\"" ;
594- }
595- return "" ;
596- }
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+ } ;
597605
598606 function serializeSourceMapContents ( version : number , file : string , sourceRoot : string , sources : string [ ] , names : string [ ] , mappings : string ) {
599- return "{\"version\":" + version + ",\"file\":\"" + file + "\",\"sourceRoot\":\"" + sourceRoot + "\",\"sources\":[" + serializeStringArray ( sources ) + "],\"names\":[" + serializeStringArray ( names ) + "],\"mappings\":\"" + mappings + "\"}" ;
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 ) output += "," ;
621+ output += "\"" + escapeString ( list [ i ] ) + "\"" ;
622+ }
623+ return output ;
624+ }
600625 }
601626
602627 function writeJavaScriptAndSourceMapFile ( emitOutput : string , writeByteOrderMark : boolean ) {
0 commit comments