Skip to content

Port TypeScript PR #60303: Fix template string escaping #1142

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

Merged
merged 7 commits into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestEmit(t *testing.T) {
{title: "BooleanLiteral#1", input: `true`, output: `true;`},
{title: "BooleanLiteral#2", input: `false`, output: `false;`},
{title: "NoSubstitutionTemplateLiteral", input: "``", output: "``;"},
{title: "NoSubstitutionTemplateLiteral#2", input: "`\n`", output: "`\n`;"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was mistaken; this test passes even without this PR's change. Add a test which would have failed before this PR's fix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test case for \u001f character in template literals that would have failed before this PR's fix. The test demonstrates that \u001f is now properly escaped to \u001F while preserving the correct behavior for \n (which should not be escaped). Commit fadf221.

{title: "RegularExpressionLiteral#1", input: `/a/`, output: `/a/;`},
{title: "RegularExpressionLiteral#2", input: `/a/g`, output: `/a/g;`},
{title: "NullLiteral", input: `null`, output: `null;`},
Expand Down
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
33 changes: 33 additions & 0 deletions internal/printer/utilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,39 @@ func TestEscapeJsxAttributeString(t *testing.T) {
}
}

func TestEscapeStringTemplateStrings(t *testing.T) {
t.Parallel()
data := []struct {
s string
quoteChar QuoteChar
expected string
desc string
}{
// Test newline character (\u000a) - should NOT be escaped in template strings
{s: "\n", quoteChar: QuoteCharBacktick, expected: "\n", desc: "newline in template string"},
{s: "\n", quoteChar: QuoteCharDoubleQuote, expected: "\\n", desc: "newline in double quote string"},
{s: "\n", quoteChar: QuoteCharSingleQuote, expected: "\\n", desc: "newline in single quote string"},

// Test \u001f (Unit Separator) - should be escaped in template strings according to TypeScript PR #60303
{s: "\u001f", quoteChar: QuoteCharBacktick, expected: "\\u001F", desc: "\\u001f in template string"},
{s: "\u001f", quoteChar: QuoteCharDoubleQuote, expected: "\\u001F", desc: "\\u001f in double quote string"},
{s: "\u001f", quoteChar: QuoteCharSingleQuote, expected: "\\u001F", desc: "\\u001f in single quote string"},

// Test other control characters that should be escaped
{s: "\u0000", quoteChar: QuoteCharBacktick, expected: "\\0", desc: "\\u0000 in template string"},
{s: "\u0009", quoteChar: QuoteCharBacktick, expected: "\\t", desc: "\\u0009 (tab) in template string"},
{s: "\u000b", quoteChar: QuoteCharBacktick, expected: "\\v", desc: "\\u000b (vtab) in template string"},
{s: "\u001e", quoteChar: QuoteCharBacktick, expected: "\\u001E", desc: "\\u001e in template string"},
}
for i, rec := range data {
t.Run(fmt.Sprintf("[%d] %s", i, rec.desc), func(t *testing.T) {
t.Parallel()
actual := EscapeString(rec.s, rec.quoteChar)
assert.Equal(t, actual, rec.expected)
})
}
}

func TestIsRecognizedTripleSlashComment(t *testing.T) {
t.Parallel()
data := []struct {
Expand Down