Skip to content

Commit 00191c5

Browse files
Copilotandrewbranchjakebailey
authored
Port TypeScript PR #60303: Fix template string escaping (#1142)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: andrewbranch <[email protected]> Co-authored-by: jakebailey <[email protected]>
1 parent c999a87 commit 00191c5

7 files changed

+6
-19
lines changed

internal/printer/printer_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ func TestEmit(t *testing.T) {
2828
{title: "BooleanLiteral#1", input: `true`, output: `true;`},
2929
{title: "BooleanLiteral#2", input: `false`, output: `false;`},
3030
{title: "NoSubstitutionTemplateLiteral", input: "``", output: "``;"},
31+
{title: "NoSubstitutionTemplateLiteral#2", input: "`\n`", output: "`\n`;"},
32+
3133
{title: "RegularExpressionLiteral#1", input: `/a/`, output: `/a/;`},
3234
{title: "RegularExpressionLiteral#2", input: `/a/g`, output: `/a/g;`},
3335
{title: "NullLiteral", input: `null`, output: `null;`},

internal/printer/utilities.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func escapeStringWorker(s string, quoteChar QuoteChar, flags getLiteralTextFlags
103103
escape = true
104104
}
105105
default:
106-
if ch < '\u001f' || flags&getLiteralTextFlagsNeverAsciiEscape == 0 && ch > '\u007f' {
106+
if ch <= '\u001f' || flags&getLiteralTextFlagsNeverAsciiEscape == 0 && ch > '\u007f' {
107107
escape = true
108108
}
109109
}

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)