diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 2c6f8b70a34b1..9b302a9e5222b 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -3441,7 +3441,7 @@ namespace ts { const doubleQuoteEscapedCharsRegExp = /[\\\"\u0000-\u001f\t\v\f\b\r\n\u2028\u2029\u0085]/g; const singleQuoteEscapedCharsRegExp = /[\\\'\u0000-\u001f\t\v\f\b\r\n\u2028\u2029\u0085]/g; // Template strings should be preserved as much as possible - const backtickQuoteEscapedCharsRegExp = /[\\\`]/g; + const backtickQuoteEscapedCharsRegExp = /[\\`]/g; const escapedCharsMap = createMapFromTemplate({ "\t": "\\t", "\v": "\\v", diff --git a/src/services/refactors/convertStringOrTemplateLiteral.ts b/src/services/refactors/convertStringOrTemplateLiteral.ts index 58a62a77d7e3c..bfa398109e59a 100644 --- a/src/services/refactors/convertStringOrTemplateLiteral.ts +++ b/src/services/refactors/convertStringOrTemplateLiteral.ts @@ -134,7 +134,6 @@ namespace ts.refactor.convertStringOrTemplateLiteral { index++; } - text = escapeString(text); return [index, text, indexes]; } diff --git a/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_escapedBackslashes.ts b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_escapedBackslashes.ts new file mode 100644 index 0000000000000..6025107ff0321 --- /dev/null +++ b/tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_escapedBackslashes.ts @@ -0,0 +1,15 @@ +/// + +//// console.log(/*0*/"\\[[" + text + "](" + link + ")\\]"/*1*/) + +goTo.select("0", "1"); +edit.applyRefactor({ + refactorName: "Convert to template string", + actionName: "Convert to template string", + actionDescription: ts.Diagnostics.Convert_to_template_string.message, + // Four backslashes here + // = two backslashes in the expected file content + // = one backslash in the string data sent to console.log + // (which is the same as what the user started with) + newContent: 'console.log(`\\\\[[${text}](${link})\\\\]`)', +});