Closed
Description
π Search Terms
template strings new line newline escape LF backslash emit print
π Version & Regression Information
- This changed in commit 635f5bd. PR: Some tweaks for backtick strings #45099
β― Playground Link
https://github.com/maxpatiiuk/typescript-bug-needless-lf-escaping/tree/main
π» Code
import ts from 'typescript';
const expression = ts.factory.createNoSubstitutionTemplateLiteral('\n');
const printer = ts.createPrinter();
// LF new line gets escaped by the printer:
const printed = printer.printNode(
ts.EmitHint.Unspecified,
expression,
ts.createSourceFile('index.ts', '', ts.ScriptTarget.Latest)
);
// Expected \n
// Received \\n
console.log(printed);
if (printed === '`\n`') console.log('Correct');
else if (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)
const backtickQuoteEscapedCharsRegExp = /\r\n|[\\`\u0000-\u001f\t\v\f\b\r\u2028\u2029\u0085]/g;
However, that RegExp has a bug - the regex has a \u0000-\u001f
character range, which includes the LF character (\u000a
)
/[\u0000-\u001f]/.test('\n')
// -> true
The 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
Activity