Skip to content

Commit c3690db

Browse files
committed
fix(native-preview): guard unterminated surrogate literals
1 parent de73c0c commit c3690db

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

internal/api/encoder/encoder_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,21 @@ func TestEncodeSourceFilePreservesSurrogateEscapes(t *testing.T) {
7575
})
7676
}
7777

78+
func TestEncodeSourceFileFallsBackForUnterminatedSurrogateEscape(t *testing.T) {
79+
t.Parallel()
80+
sourceFile := parser.ParseSourceFile(ast.SourceFileParseOptions{
81+
FileName: "/test.ts",
82+
Path: "/test.ts",
83+
}, `let s = "\uD800a`, core.ScriptKindTS)
84+
85+
buf, err := encoder.EncodeSourceFile(sourceFile)
86+
assert.NilError(t, err)
87+
88+
text, ok := findExtendedNodeText(buf, ast.KindStringLiteral)
89+
assert.Assert(t, ok)
90+
assert.DeepEqual(t, text, []byte("\ufffda"))
91+
}
92+
7893
func BenchmarkEncodeSourceFile(b *testing.B) {
7994
repo.SkipIfNoTypeScriptSubmodule(b)
8095
filePath := filepath.Join(repo.TypeScriptSubmodulePath(), "src/compiler/checker.ts")

internal/api/encoder/literal_text.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func rawQuotedLiteralText(node *ast.Node, strs *stringTable) (string, bool) {
3737
}
3838
switch strs.fileText[start] {
3939
case '\'', '"', '`':
40-
if node.End()-start < 2 {
40+
if node.End()-start < 2 || strs.fileText[node.End()-1] != strs.fileText[start] {
4141
return "", false
4242
}
4343
return strs.fileText[start:node.End()], true

0 commit comments

Comments
 (0)