🔎 Search Terms
template strings new line newline escape LF backslash emit print
🕗 Version & Regression Information
⏯ Playground Link
https://github.com/maxpatiiuk/typescript-bug-needless-lf-escaping/tree/main
💻 Code
importtsfrom'typescript';constexpression=ts.factory.createNoSubstitutionTemplateLiteral('\n');constprinter=ts.createPrinter();// LF new line gets escaped by the printer:constprinted=printer.printNode(ts.EmitHint.Unspecified,expression,ts.createSourceFile('index.ts','',ts.ScriptTarget.Latest));// Expected \n// Received \\nconsole.log(printed);if(printed==='`\n`')console.log('Correct');elseif(printed==='`\\n`')console.error('Incorrect');🙁 Actual behavior
LF new lines in template literal strings are escaped
🙂 Expected behavior
As the comment in TypeScript's source code states, LF new lines in backticks should not be escaped:
// Template strings preserve simple LF newlines, still encode CRLF (or CR)constbacktickQuoteEscapedCharsRegExp=/\r\n|[\\`\u0000-\u001f\t\v\f\b\r\u2028\u2029\u0085]/g;
https://github.com/microsoft/TypeScript/blame/3163fe7e3898c1f48cd9bc097b96e3426cd2a453/src/compiler/utilities.ts#L5925-L5926
However, that RegExp has a bug - the regex has a \u0000-\u001f character range, which includes the LF character (\u000a)
/[\u0000-\u001f]/.test('\n')// -> trueThe RegExp should be modified to not match \u000a (\n). Potentially like so:
- const backtickQuoteEscapedCharsRegExp = /\r\n|[\\`\u0000-\u001f\t\v\f\b\r\u2028\u2029\u0085]/g;+ const backtickQuoteEscapedCharsRegExp = /\r\n|[\\`\u0000-\u0009\u000b-\u001f\t\v\f\b\r\u2028\u2029\u0085]/g;
Additional information about the issue
No response
🔎 Search Terms
template strings new line newline escape LF backslash emit print
🕗 Version & Regression Information
⏯ Playground Link
https://github.com/maxpatiiuk/typescript-bug-needless-lf-escaping/tree/main
💻 Code
🙁 Actual behavior
LF new lines in template literal strings are escaped
🙂 Expected behavior
As the comment in TypeScript's source code states, LF new lines in backticks should not be escaped:
https://github.com/microsoft/TypeScript/blame/3163fe7e3898c1f48cd9bc097b96e3426cd2a453/src/compiler/utilities.ts#L5925-L5926
However, that RegExp has a bug - the regex has a
\u0000-\u001fcharacter range, which includes the LF character (\u000a)The RegExp should be modified to not match
\u000a(\n). Potentially like so:Additional information about the issue
No response