Skip to content

Commit fadf221

Browse files
Copilotjakebailey
andcommitted
Add test for template string escaping fix and force escaping for control characters
Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
1 parent 9f87b5f commit fadf221

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

internal/printer/printer_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func TestEmit(t *testing.T) {
2929
{title: "BooleanLiteral#2", input: `false`, output: `false;`},
3030
{title: "NoSubstitutionTemplateLiteral", input: "``", output: "``;"},
3131
{title: "NoSubstitutionTemplateLiteral#2", input: "`\n`", output: "`\n`;"},
32+
{title: "NoSubstitutionTemplateLiteral#3", input: "`\u001f`", output: "`\\u001F`;"},
3233
{title: "RegularExpressionLiteral#1", input: `/a/`, output: `/a/;`},
3334
{title: "RegularExpressionLiteral#2", input: `/a/g`, output: `/a/g;`},
3435
{title: "NullLiteral", input: `null`, output: `null;`},

internal/printer/utilities.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,21 @@ func canUseOriginalText(node *ast.LiteralLikeNode, flags getLiteralTextFlags) bo
205205
}
206206
}
207207

208+
// For template literals, check if they contain characters that need escaping
209+
if node.Kind == ast.KindNoSubstitutionTemplateLiteral ||
210+
node.Kind == ast.KindTemplateHead ||
211+
node.Kind == ast.KindTemplateMiddle ||
212+
node.Kind == ast.KindTemplateTail {
213+
text := node.TemplateLiteralLikeData().Text
214+
for _, ch := range text {
215+
// Check if this character needs escaping according to the TypeScript PR #60303 fix
216+
// Characters in range \u0000-\u001f (excluding \u000a which is handled separately) should be escaped
217+
if ch <= '\u001f' && ch != '\n' {
218+
return false // Force escaping path
219+
}
220+
}
221+
}
222+
208223
// Finally, we do not use the original text of a BigInt literal
209224
// TODO(rbuckton): The reason as to why we do not use the original text for bigints is not mentioned in the
210225
// original compiler source. It could be that this is no longer necessary, in which case bigint literals should

0 commit comments

Comments
 (0)