Skip to content

Port TypeScript PR #60303: Fix template string escaping - Analysis and Tests #1125

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

Closed
wants to merge 2 commits into from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 9, 2025

Summary

This PR addresses the porting of TypeScript PR #60303 which fixes template string escaping to preserve newlines in template literals. After thorough analysis, I found that this fix is already correctly implemented in the Go codebase.

Analysis of TypeScript PR #60303

The TypeScript fix changed the regular expression for backtick-quoted characters from:

// Before
const backtickQuoteEscapedCharsRegExp = /\r\n|[\\`\u0000-\u001f\u2028\u2029\u0085]/g;

// After  
const backtickQuoteEscapedCharsRegExp = /\r\n|[\\`\u0000-\u0009\u000b-\u001f\u2028\u2029\u0085]/g;

This change excludes \u000A (newline character) from being escaped in template strings, preserving simple LF newlines while still escaping other control characters.

Go Implementation Status

The Go code in internal/printer/utilities.go already implements this fix correctly:

case '\n':
    if quoteChar != QuoteCharBacktick {
        // Template strings preserve simple LF newlines, still encode CRLF (or CR).
        escape = true
    }

When the quote character is a backtick (template string), newlines are not escaped, which is exactly what the TypeScript fix achieved.

Changes Made

  • Added comprehensive test cases for template string escaping behavior
  • Verified that template literals with newlines are correctly preserved: ab\nc with backticks → ab\nc (unescaped)
  • Confirmed that regular strings still escape newlines properly: ab\nc with quotes → ab\\nc (escaped)
  • All existing tests continue to pass

Test Results

// Template strings preserve newlines
{s: "ab\nc", quoteChar: QuoteCharBacktick, expected: "ab\nc"},

// Regular strings escape newlines  
{s: "ab\nc", quoteChar: QuoteCharDoubleQuote, expected: "ab\\nc"},
{s: "ab\nc", quoteChar: QuoteCharSingleQuote, expected: "ab\\nc"},

Conclusion

No code changes were required as the Go implementation already correctly handles template string escaping as intended by TypeScript PR #60303. This PR adds tests to document and verify this behavior.

Fixes #1121.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • api.github.com
    • Triggering command: curl -s REDACTED (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 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.

Co-authored-by: andrewbranch <3277153+andrewbranch@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Port TypeScript PR #60303: Fix template string escaping Port TypeScript PR #60303: Fix template string escaping - Analysis and Tests Jun 9, 2025
@Copilot Copilot AI requested a review from andrewbranch June 9, 2025 22:49
Copilot finished work on behalf of andrewbranch June 9, 2025 22:50
@jakebailey jakebailey deleted the copilot/fix-1121 branch June 24, 2025 23:56
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 TypeScript PR #60303: Fix template string escaping
2 participants