@@ -351,19 +351,12 @@ export function embedTextBlock(path: NamedNodePath<SyntaxType.StringLiteral>) {
351351 textToDoc : ( text : string , options : Options ) => Promise < Doc >
352352 ) => {
353353 const doc = await textToDoc ( text , { parser : language } ) ;
354- return printTextBlock ( path , escapeDocForTextBlock ( doc ) ) ;
354+ return printTextBlock ( path , [ escapeDocForTextBlock ( doc ) , hardline ] ) ;
355355 } ;
356356}
357357
358358export function textBlockContents ( node : NamedNode < SyntaxType . StringLiteral > ) {
359- const lines = node . value
360- . replace (
361- / (?< = ^ | [ ^ \\ ] ) ( (?: \\ \\ ) * ) \\ u + ( [ 0 - 9 a - f A - F ] { 4 } ) / g,
362- ( _ , backslashPairs : string , hex : string ) =>
363- backslashPairs + String . fromCharCode ( parseInt ( hex , 16 ) )
364- )
365- . split ( "\n" )
366- . slice ( 1 ) ;
359+ const lines = node . value . split ( "\n" ) . slice ( 1 ) ;
367360 const baseIndent = findBaseIndent ( lines ) ;
368361 return lines
369362 . map ( line => line . slice ( baseIndent ) )
@@ -394,39 +387,28 @@ function findEmbeddedLanguage(path: NamedNodePath) {
394387function escapeDocForTextBlock ( doc : Doc ) {
395388 return mapDoc ( doc , currentDoc =>
396389 typeof currentDoc === "string"
397- ? currentDoc . replace ( / \\ | " " " / g, match => `\\${ match } ` )
390+ ? currentDoc . replace ( / \\ | " " " / g, match =>
391+ match === "\\" ? "\\\\" : '""\\"'
392+ )
398393 : currentDoc
399394 ) ;
400395}
401396
402397function unescapeTextBlockContents ( text : string ) {
403- return text . replace (
404- / \\ (?: ( [ b s t n f r " ' \\ ] ) | \n | \r \n ? | ( [ 0 - 3 ] [ 0 - 7 ] { 0 , 2 } | [ 0 - 7 ] { 1 , 2 } ) ) / g,
405- ( _ , single , octal ) => {
406- if ( single ) {
407- switch ( single ) {
408- case "b" :
409- return "\b" ;
410- case "s" :
411- return " " ;
412- case "t" :
413- return "\t" ;
414- case "n" :
415- return "\n" ;
416- case "f" :
417- return "\f" ;
418- case "r" :
419- return "\r" ;
420- default :
421- return single ;
422- }
423- } else if ( octal ) {
424- return String . fromCharCode ( parseInt ( octal , 8 ) ) ;
425- } else {
426- return "" ;
427- }
398+ return text . replace ( / \\ (?: ( [ s t n r " ' \\ ] ) | \n | \r \n ? ) / g, ( _ , escaped ) => {
399+ switch ( escaped ) {
400+ case "s" :
401+ return " " ;
402+ case "t" :
403+ return "\t" ;
404+ case "n" :
405+ return "\n" ;
406+ case "r" :
407+ return "\r" ;
408+ default :
409+ return escaped ?? "" ;
428410 }
429- ) ;
411+ } ) ;
430412}
431413
432414export type NamedNodePrinters = {
0 commit comments