Fix template string escaping (#60303)

This commit is contained in:
Ron Buckton
2024-10-21 16:18:54 -04:00
committed by GitHub
parent df9d16503f
commit e6ef279403
3 changed files with 11 additions and 1 deletions

View File

@@ -6064,7 +6064,7 @@ export function hasInvalidEscape(template: TemplateLiteral): boolean {
const doubleQuoteEscapedCharsRegExp = /[\\"\u0000-\u001f\u2028\u2029\u0085]/g;
const singleQuoteEscapedCharsRegExp = /[\\'\u0000-\u001f\u2028\u2029\u0085]/g;
// Template strings preserve simple LF newlines, still encode CRLF (or CR)
const backtickQuoteEscapedCharsRegExp = /\r\n|[\\`\u0000-\u001f\u2028\u2029\u0085]/g;
const backtickQuoteEscapedCharsRegExp = /\r\n|[\\`\u0000-\u0009\u000b-\u001f\u2028\u2029\u0085]/g;
const escapedCharsMap = new Map(Object.entries({
"\t": "\\t",
"\v": "\\v",

View File

@@ -358,5 +358,13 @@ describe("unittests:: PrinterAPI", () => {
),
ts.createSourceFile("source.ts", "", ts.ScriptTarget.ES2015),
));
// https://github.com/microsoft/TypeScript/issues/59150
printsCorrectly("template string", {}, printer =>
printer.printNode(
ts.EmitHint.Unspecified,
ts.factory.createNoSubstitutionTemplateLiteral("\n"),
ts.createSourceFile("source.ts", "", ts.ScriptTarget.ESNext),
));
});
});