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
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: 2 additions & 0 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
@@ -28,6 +28,8 @@ 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;`},
2 changes: 1 addition & 1 deletion internal/printer/utilities.go
Original file line number Diff line number Diff line change
@@ -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
}
}
1 change: 1 addition & 0 deletions internal/printer/utilities_test.go
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@ 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"},
{s: "\u001f", quoteChar: QuoteCharBacktick, expected: "\\u001F"},
}
for i, rec := range data {
t.Run(fmt.Sprintf("[%d] escapeString(%q, %v)", i, rec.s, rec.quoteChar), func(t *testing.T) {
Original file line number Diff line number Diff line change
@@ -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
@@ -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.