Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug(Printer): LF newline in template strings is escaped when it shouldn't be #59150

Closed
maxpatiiuk opened this issue Jul 5, 2024 · 0 comments Β· Fixed by #60303
Closed

bug(Printer): LF newline in template strings is escaped when it shouldn't be #59150

maxpatiiuk opened this issue Jul 5, 2024 · 0 comments Β· Fixed by #60303
Assignees
Labels
Bug A bug in TypeScript Domain: Transforms Relates to the public transform API Fix Available A PR has been opened for this issue

Comments

@maxpatiiuk
Copy link

maxpatiiuk commented Jul 5, 2024

πŸ”Ž 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

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;

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')
// -> 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

@andrewbranch andrewbranch added the Needs Investigation This issue needs a team member to investigate its status. label Jul 5, 2024
@andrewbranch andrewbranch added this to the TypeScript 5.6.0 milestone Jul 5, 2024
@typescript-bot typescript-bot added Fix Available A PR has been opened for this issue labels Oct 21, 2024
@rbuckton rbuckton added Bug A bug in TypeScript Domain: Transforms Relates to the public transform API and removed Needs Investigation This issue needs a team member to investigate its status. labels Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Transforms Relates to the public transform API Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants