-
Notifications
You must be signed in to change notification settings - Fork 783
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
Changes from 6 commits
45314e4
3e616fb
1d3732b
942edc6
9f87b5f
fadf221
f5a06a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| } | ||
| } | ||
|
|
@@ -205,6 +205,21 @@ func canUseOriginalText(node *ast.LiteralLikeNode, flags getLiteralTextFlags) bo | |
| } | ||
| } | ||
|
|
||
| // For template literals, check if they contain characters that need escaping | ||
|
||
| 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 | ||
|
|
||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
\u001fcharacter in template literals that would have failed before this PR's fix. The test demonstrates that\u001fis now properly escaped to\u001Fwhile preserving the correct behavior for\n(which should not be escaped). Commit fadf221.