Skip to content

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

Merged
merged 7 commits into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ 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: "RegularExpressionLiteral#1", input: `/a/`, output: `/a/;`},
{title: "RegularExpressionLiteral#2", input: `/a/g`, output: `/a/g;`},
{title: "NullLiteral", input: `null`, output: `null;`},
Expand Down
40 changes: 40 additions & 0 deletions internal/printer/template_string_fix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package printer_test

import (
"testing"

"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/testutil/emittestutil"
"github.com/microsoft/typescript-go/internal/testutil/parsetestutil"
)

// TestTemplateStringEscapingFix tests the fix for TypeScript issue #59150
// This test mirrors the test case added in TypeScript PR #60303
func TestTemplateStringEscapingFix(t *testing.T) {
t.Parallel()

// https://github.com/microsoft/TypeScript/issues/59150
// Replicates: printsCorrectly("template string", {}, printer =>
// printer.printNode(
// ts.EmitHint.Unspecified,
// ts.factory.createNoSubstitutionTemplateLiteral("\n"),
// ts.createSourceFile("source.ts", "", ts.ScriptTarget.ESNext),
// ));

var factory ast.NodeFactory

// Create a synthetic NoSubstitutionTemplateLiteral with just a newline character
templateLiteral := factory.NewNoSubstitutionTemplateLiteral("\n")

// Create a synthetic source file
file := factory.NewSourceFile("/source.ts", "/source.ts", "/source.ts", factory.NewNodeList([]*ast.Node{
factory.NewExpressionStatement(templateLiteral),
}))
ast.SetParentInChildren(file)
parsetestutil.MarkSyntheticRecursive(file)

// The fix ensures that LF newlines are NOT escaped in template literals
// Expected: `\n` (with literal newline character, not escaped)
// Bug would produce: `\\n` (with escaped newline)
emittestutil.CheckEmit(t, nil, file.AsSourceFile(), "`\n`;")
}
14 changes: 12 additions & 2 deletions internal/printer/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,18 @@ func escapeStringWorker(s string, quoteChar QuoteChar, flags getLiteralTextFlags
escape = true
}
default:
if ch < '\u001f' || flags&getLiteralTextFlagsNeverAsciiEscape == 0 && ch > '\u007f' {
escape = true
// For template strings, exclude \u000a (LF/newline) from the character range that gets escaped
// This matches the TypeScript fix in PR #60303 that changed the regex from \u0000-\u001f to \u0000-\u0009\u000b-\u001f
if quoteChar == QuoteCharBacktick {
// For template strings: escape \u0000-\u0009 and \u000b-\u001f (excluding \u000a which is LF)
if (ch >= '\u0000' && ch <= '\u0009') || (ch >= '\u000b' && ch <= '\u001f') || flags&getLiteralTextFlagsNeverAsciiEscape == 0 && ch > '\u007f' {
escape = true
}
} else {
// For regular strings: escape \u0000-\u001f as before
if ch < '\u001f' || flags&getLiteralTextFlagsNeverAsciiEscape == 0 && ch > '\u007f' {
escape = true
}
}
}

Expand Down
Loading