Skip to content

[WIP] 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

Draft
wants to merge 6 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: 2 additions & 0 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "NoSubstitutionTemplateLiteral#3", input: "`\u001f`", output: "`\\u001F`;"},
{title: "RegularExpressionLiteral#1", input: `/a/`, output: `/a/;`},
{title: "RegularExpressionLiteral#2", input: `/a/g`, output: `/a/g;`},
{title: "NullLiteral", input: `null`, output: `null;`},
Expand Down
17 changes: 16 additions & 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 Expand Up @@ -205,6 +205,21 @@ func canUseOriginalText(node *ast.LiteralLikeNode, flags getLiteralTextFlags) bo
}
}

// For template literals, check if they contain characters that need escaping
Copy link
Member

Choose a reason for hiding this comment

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

Is this code block necessary? Try removing it and see if tests still pass.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tested removing the code block - it causes test TestEmit/NoSubstitutionTemplateLiteral#3 to fail. The code block is necessary because it forces template literals with control characters to go through the escaping path in getLiteralText() rather than using the original source text. Without it, canUseOriginalText() returns true and template literals containing \u001f would not be properly escaped.

Copy link
Member

Choose a reason for hiding this comment

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

I think your test is wrong and it should be moved to TestEscapeString in utilities_test.go, and then delete this code. Also make sure to run all tests and update baselines; you should see some deleted diffs.

if node.Kind == ast.KindNoSubstitutionTemplateLiteral ||
node.Kind == ast.KindTemplateHead ||
node.Kind == ast.KindTemplateMiddle ||
node.Kind == ast.KindTemplateTail {
text := node.TemplateLiteralLikeData().Text
for _, ch := range text {
// Check if this character needs escaping according to the TypeScript PR #60303 fix
// Characters in range \u0000-\u001f (excluding \u000a which is handled separately) should be escaped
if ch <= '\u001f' && ch != '\n' {
return false // Force escaping path
}
}
}

// Finally, we do not use the original text of a BigInt literal
// TODO(rbuckton): The reason as to why we do not use the original text for bigints is not mentioned in the
// original compiler source. It could be that this is no longer necessary, in which case bigint literals should
Expand Down
Loading