Skip to content

Commit f5a06a8

Browse files
Copilotandrewbranch
andcommitted
Simplify template string escaping fix by removing unnecessary canUseOriginalText check
Co-authored-by: andrewbranch <3277153+andrewbranch@users.noreply.github.com>
1 parent fadf221 commit f5a06a8

7 files changed

+4
-34
lines changed

internal/printer/printer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +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`;"},
32+
3333
{title: "RegularExpressionLiteral#1", input: `/a/`, output: `/a/;`},
3434
{title: "RegularExpressionLiteral#2", input: `/a/g`, output: `/a/g;`},
3535
{title: "NullLiteral", input: `null`, output: `null;`},

internal/printer/utilities.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -205,21 +205,6 @@ 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-
223208
// Finally, we do not use the original text of a BigInt literal
224209
// TODO(rbuckton): The reason as to why we do not use the original text for bigints is not mentioned in the
225210
// original compiler source. It could be that this is no longer necessary, in which case bigint literals should

internal/printer/utilities_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func TestEscapeString(t *testing.T) {
2525
{s: "ab'c", quoteChar: QuoteCharSingleQuote, expected: `ab\'c`},
2626
{s: "ab\"c", quoteChar: QuoteCharSingleQuote, expected: `ab"c`},
2727
{s: "ab`c", quoteChar: QuoteCharBacktick, expected: "ab\\`c"},
28+
{s: "\u001f", quoteChar: QuoteCharBacktick, expected: "\\u001F"},
2829
}
2930
for i, rec := range data {
3031
t.Run(fmt.Sprintf("[%d] escapeString(%q, %v)", i, rec.s, rec.quoteChar), func(t *testing.T) {

testdata/baselines/reference/submodule/conformance/templateStringControlCharacterEscapes03.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
=== templateStringControlCharacterEscapes03.ts ===
44
var x = `\x1F\u001f 1F 1f`;
55
>x : string
6-
>`\x1F\u001f 1F 1f` : " 1F 1f"
6+
>`\x1F\u001f 1F 1f` : "\u001F\u001F 1F 1f"
77

testdata/baselines/reference/submodule/conformance/templateStringControlCharacterEscapes03.types.diff

Lines changed: 0 additions & 8 deletions
This file was deleted.

testdata/baselines/reference/submodule/conformance/templateStringControlCharacterEscapes03_ES6.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
=== templateStringControlCharacterEscapes03_ES6.ts ===
44
var x = `\x1F\u001f 1F 1f`;
55
>x : string
6-
>`\x1F\u001f 1F 1f` : " 1F 1f"
6+
>`\x1F\u001f 1F 1f` : "\u001F\u001F 1F 1f"
77

testdata/baselines/reference/submodule/conformance/templateStringControlCharacterEscapes03_ES6.types.diff

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)