Skip to content

Port TypeScript PR #60303: Fix template string escaping for control characters #17

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Jun 6, 2025

This ports the fix from microsoft/TypeScript#60303 which corrected template string escaping behavior for control characters.

Problem

The template string escaping logic had a subtle bug where the \u001f control character (Unit Separator) was not being escaped when it should have been. The condition ch < '\u001f' was excluding \u001f itself from the escaping logic.

Solution

Changed the condition from ch < '\u001f' to ch <= '\u001f' in the escapeStringWorker function to properly include all control characters up to and including \u001f.

Changes Made

  1. Fixed escaping condition in internal/printer/utilities.go:

    // Before
    if ch < '\u001f' || flags&getLiteralTextFlagsNeverAsciiEscape == 0 && ch > '\u007f' {
    
    // After  
    if ch <= '\u001f' || flags&getLiteralTextFlagsNeverAsciiEscape == 0 && ch > '\u007f' {
  2. Added comprehensive test cases to verify:

    • LF character (\n) is NOT escaped in template strings (backticks) ✓
    • Other control characters like \u0001, \u001f ARE escaped in template strings ✓
    • Special characters like tab (\t) and vertical tab (\v) are properly escaped ✓
  3. Updated test baselines for templateStringControlCharacterEscapes03 tests that now correctly escape control characters.

Verification

The fix was verified by:

  • All new unit tests pass
  • Existing baseline tests updated to reflect correct behavior
  • Build and full test suite passes
  • The LF character behavior remains correct (not escaped in template strings)

Fixes #15.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] Port #60303 Port TypeScript PR #60303: Fix template string escaping for control characters Jun 6, 2025
@Copilot Copilot AI requested a review from andrewbranch June 6, 2025 16:19
Copilot finished work on behalf of andrewbranch June 6, 2025 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Port #60303
2 participants