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
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/printer/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func escapeStringWorker(s string, quoteChar QuoteChar, flags getLiteralTextFlags
escape = true
}
default:
if ch < '\u001f' || flags&getLiteralTextFlagsNeverAsciiEscape == 0 && ch > '\u007f' {
if ch <= '\u001f' || flags&getLiteralTextFlagsNeverAsciiEscape == 0 && ch > '\u007f' {
escape = true
}
}
Expand Down
28 changes: 27 additions & 1 deletion internal/printer/utilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ func TestEscapeString(t *testing.T) {
{s: "ab'c", quoteChar: QuoteCharSingleQuote, expected: `ab\'c`},
{s: "ab\"c", quoteChar: QuoteCharSingleQuote, expected: `ab"c`},
{s: "ab`c", quoteChar: QuoteCharBacktick, expected: "ab\\`c"},
// Test case for issue #59150: newlines in template strings should NOT be escaped
{s: "\n", quoteChar: QuoteCharBacktick, expected: "\n"},
{s: "ab\nc", quoteChar: QuoteCharBacktick, expected: "ab\nc"},
// Test other control characters that SHOULD be escaped in template strings
{s: "\u0001", quoteChar: QuoteCharBacktick, expected: "\\u0001"}, // control character should be escaped
{s: "\u0009", quoteChar: QuoteCharBacktick, expected: "\\t"}, // tab should be escaped
{s: "\u000b", quoteChar: QuoteCharBacktick, expected: "\\v"}, // vertical tab should be escaped
{s: "\u001f", quoteChar: QuoteCharBacktick, expected: "\\u001F"}, // unit separator should be escaped
}
for i, rec := range data {
t.Run(fmt.Sprintf("[%d] escapeString(%q, %v)", i, rec.s, rec.quoteChar), func(t *testing.T) {
Expand All @@ -51,7 +59,9 @@ func TestEscapeNonAsciiString(t *testing.T) {
{s: "ab'c", quoteChar: QuoteCharSingleQuote, expected: `ab\'c`},
{s: "ab\"c", quoteChar: QuoteCharSingleQuote, expected: `ab"c`},
{s: "ab`c", quoteChar: QuoteCharBacktick, expected: "ab\\`c"},
{s: "ab\u008fc", quoteChar: QuoteCharDoubleQuote, expected: `ab\u008Fc`},
// Test case for issue #59150: newlines in template strings should NOT be escaped
{s: "\n", quoteChar: QuoteCharBacktick, expected: "\n"},
{s: "ab\nc", quoteChar: QuoteCharBacktick, expected: "ab\nc"},
{s: "𝟘𝟙", quoteChar: QuoteCharDoubleQuote, expected: `\uD835\uDFD8\uD835\uDFD9`},
}
for i, rec := range data {
Expand Down Expand Up @@ -150,3 +160,19 @@ func TestIsRecognizedTripleSlashComment(t *testing.T) {
})
}
}

func TestSyntheticTemplateLiteral(t *testing.T) {
t.Parallel()

// Test the specific issue: LF character should not be escaped in template strings
// This mimics the TypeScript test case: ts.factory.createNoSubstitutionTemplateLiteral("\n")

result := EscapeString("\n", QuoteCharBacktick)
expected := "\n" // LF should NOT be escaped for backticks
assert.Equal(t, result, expected)

// Also test that other control characters are still escaped
result2 := EscapeString("\u0001", QuoteCharBacktick)
expected2 := "\\u0001" // Other control characters should still be escaped
assert.Equal(t, result2, expected2)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
=== templateStringControlCharacterEscapes03.ts ===
var x = `\x1F\u001f 1F 1f`;
>x : string
>`\x1F\u001f 1F 1f` : " 1F 1f"
>`\x1F\u001f 1F 1f` : "\u001F\u001F 1F 1f"

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
=== templateStringControlCharacterEscapes03_ES6.ts ===
var x = `\x1F\u001f 1F 1f`;
>x : string
>`\x1F\u001f 1F 1f` : " 1F 1f"
>`\x1F\u001f 1F 1f` : "\u001F\u001F 1F 1f"

This file was deleted.